File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
Filter options
Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
Original file line number Diff line number Diff line change 23
23
def tokenize (text : str ) -> Iterator [str ]: # <1>
24
24
"""return iterable of uppercased words"""
25
25
for match in RE_WORD .finditer (text ):
26
+ # for each word in phrase, group goes to string from re.Match object
26
27
yield match .group ().upper ()
27
28
28
29
def name_index (start : int = 32 , end : int = STOP_CODE ) -> dict [str , set [str ]]:
29
30
index : dict [str , set [str ]] = {} # <2>
30
31
for char in (chr (i ) for i in range (start , end )):
32
+ # for each unicode character
31
33
if name := unicodedata .name (char , '' ): # <3>
34
+ # lookup English word name of character
32
35
for word in tokenize (name ):
36
+ # for each word in the name, add to dict with symbol in value set
33
37
index .setdefault (word , set ()).add (char )
34
38
return index
35
39
# end::CHARINDEX[]
40
+
41
+ if __name__ == '__main__' :
42
+ index = name_index (32 , 65 )
43
+ print (f"""
44
+ SIGN: { sorted (index ['SIGN' ]) }
45
+ DIGIT: { sorted (index ['DIGIT' ]) }
46
+ DIGIT & EIGHT: { index ['DIGIT' ] & index ['EIGHT' ] }
47
+ # & is a set operation
48
+ """ )
You can’t perform that action at this time.
0 commit comments