4646import os
4747import re
4848from itertools import chain
49+ from typing import Optional
4950
5051from mathics .builtin .atomic .strings import anchor_pattern , to_regex
5152from mathics .builtin .base import Builtin , MessageException
@@ -197,9 +198,9 @@ def _merge_dictionaries(a, b):
197198
198199def _position (t ):
199200 if isinstance (t , Span ):
200- l = t .doc [t .start ]
201+ i = t .doc [t .start ]
201202 r = t .doc [t .end - 1 ]
202- return 1 + l .idx , r .idx + len (r .text )
203+ return 1 + i .idx , r .idx + len (r .text )
203204 else :
204205 return 1 + t .idx , t .idx + len (t .text )
205206
@@ -263,7 +264,7 @@ def _load_spacy(self, evaluation, options):
263264 evaluation .message (self .get_name (), "runtime" , str (e ))
264265 return None
265266
266- def _nlp (self , text , evaluation , options ):
267+ def _nlp (self , text , evaluation , options ) -> Optional [ spacy . tokens . doc . Doc ] :
267268 nlp = self ._load_spacy (evaluation , options )
268269 if not nlp :
269270 return None
@@ -389,7 +390,7 @@ def apply_n(self, text, n, evaluation, options):
389390 doc = self ._nlp (text .value , evaluation , options )
390391 if doc :
391392 return ListExpression (
392- itertools .islice ((String (sent .text ) for sent in doc .sents ), n .value ),
393+ * itertools .islice ((String (sent .text ) for sent in doc .sents ), n .value ),
393394 )
394395
395396
@@ -410,8 +411,8 @@ class DeleteStopwords(_SpacyBuiltin):
410411 = Old Man Apulia, conduct peculiar
411412 """
412413
413- def apply_list (self , l , evaluation , options ):
414- "DeleteStopwords[l_List , OptionsPattern[%(name)s]]"
414+ def apply_list (self , li , evaluation , options ):
415+ "DeleteStopwords[li_List , OptionsPattern[%(name)s]]"
415416 is_stop = self ._is_stop_lambda (evaluation , options )
416417
417418 def filter_words (words ):
@@ -422,7 +423,7 @@ def filter_words(words):
422423 elif not is_stop (s ):
423424 yield String (s )
424425
425- return ListExpression (* list (filter_words (l .elements )))
426+ return ListExpression (* list (filter_words (li .elements )))
426427
427428 def apply_string (self , s , evaluation , options ):
428429 "DeleteStopwords[s_String, OptionsPattern[%(name)s]]"
@@ -451,7 +452,7 @@ class WordFrequency(_SpacyBuiltin):
451452 $word$ may also specify multiple words using $a$ | $b$ | ...
452453
453454 >> WordFrequency[Import["ExampleData/EinsteinSzilLetter.txt"], "a" | "the"]
454- = 0.0667702
455+ = 0.0665635
455456
456457 >> WordFrequency["Apple Tree", "apple", IgnoreCase -> True]
457458 = 0.5
@@ -565,17 +566,17 @@ class TextCases(_SpacyBuiltin):
565566 >> TextCases["I was in London last year.", "City"]
566567 = {London}
567568
568- >> TextCases[Import["ExampleData/EinsteinSzilLetter.txt"], "Person", 3]
569- = {Albert Einstein, E. Fermi, L. Szilard}
569+ >> TextCases[Import["ExampleData/EinsteinSzilLetter.txt"], "Person", 3][[2;;3]]
570+ = {E. Fermi, L. Szilard}
570571 """
571572
572- def apply (self , text , form , evaluation , options ):
573+ def apply_string_form (self , text , form , evaluation , options ):
573574 "TextCases[text_String, form_, OptionsPattern[%(name)s]]"
574575 doc = self ._nlp (text .value , evaluation , options )
575576 if doc :
576577 return to_mathics_list (* [t .text for t in _cases (doc , form )])
577578
578- def apply_n (self , text , form , n , evaluation , options ):
579+ def apply_string_form_n (self , text , form , n , evaluation , options ):
579580 "TextCases[text_String, form_, n_Integer, OptionsPattern[%(name)s]]"
580581 doc = self ._nlp (text .value , evaluation , options )
581582 if doc :
@@ -591,17 +592,17 @@ class TextPosition(_SpacyBuiltin):
591592 <dd>returns the positions of elements of type $form$ in $text$ in order of their appearance.
592593 </dl>
593594
594- >> TextPosition["Liverpool and Manchester are two English cities.", "City"]
595- = {{1, 9}, {15, 24 }}
595+ >> TextPosition["Liverpool and London are two English cities.", "City"]
596+ = {{1, 9}, {15, 20 }}
596597 """
597598
598- def apply (self , text , form , evaluation , options ):
599+ def apply_text_form (self , text , form , evaluation , options ):
599600 "TextPosition[text_String, form_, OptionsPattern[%(name)s]]"
600601 doc = self ._nlp (text .value , evaluation , options )
601602 if doc :
602- return ListExpression (* [_position (t ) for t in _cases (doc , form )])
603+ return to_mathics_list (* [_position (t ) for t in _cases (doc , form )])
603604
604- def apply_n (self , text , form , n , evaluation , options ):
605+ def apply_text_form_n (self , text , form , n , evaluation , options ):
605606 "TextPosition[text_String, form_, n_Integer, OptionsPattern[%(name)s]]"
606607 doc = self ._nlp (text .value , evaluation , options )
607608 if doc :
@@ -682,13 +683,13 @@ class WordSimilarity(_SpacyBuiltin):
682683 </dl>
683684
684685 >> NumberForm[WordSimilarity["car", "train"], 3]
685- = 0.5
686+ = 0.731
686687
687688 >> NumberForm[WordSimilarity["car", "hedgehog"], 3]
688- = 0.368
689+ = 0.302
689690
690691 >> NumberForm[WordSimilarity[{"An ocean full of water.", {2, 2}}, { "A desert full of sand.", {2, 5}}], 3]
691- = {0.253 , 0.177 }
692+ = {0.731 , 0.317 }
692693 """
693694
694695 messages = _merge_dictionaries (
@@ -724,8 +725,8 @@ def apply_pair(self, text1, i1, text2, i2, evaluation, options):
724725 evaluation .message ("TextSimilarity" , "idxfmt" )
725726 return
726727 if any (
727- not all (isinstance (i , Integer ) for i in l .elements )
728- for l in (i1 , i2 )
728+ not all (isinstance (i , Integer ) for i in li .elements )
729+ for li in (i1 , i2 )
729730 ):
730731 evaluation .message ("TextSimilarity" , "idxfmt" )
731732 return
@@ -974,7 +975,7 @@ def apply(self, word, evaluation, options):
974975 if wordnet :
975976 senses = self ._senses (word .value .lower (), wordnet , language_code )
976977 if senses :
977- return ListExpression ([String (syn .definition ()) for syn , _ in senses ])
978+ return ListExpression (* [String (syn .definition ()) for syn , _ in senses ])
978979 else :
979980 return Expression (SymbolMissing , "NotAvailable" )
980981
@@ -1006,12 +1007,12 @@ def examples(self, syn, desc):
10061007 def synonyms (self , syn , desc ):
10071008 _ , pos , container = desc
10081009 return [
1009- self .syn_form ((l .name ().replace ("_" , " " ), pos , container ))
1010- for l in WordProperty ._synonymous_lemmas (syn )
1010+ self .syn_form ((s .name ().replace ("_" , " " ), pos , container ))
1011+ for s in WordProperty ._synonymous_lemmas (syn )
10111012 ]
10121013
10131014 def antonyms (self , syn , desc ):
1014- return [self .syn (l .synset ()) for l in WordProperty ._antonymous_lemmas (syn )]
1015+ return [self .syn (s .synset ()) for s in WordProperty ._antonymous_lemmas (syn )]
10151016
10161017 def broader_terms (self , syn , desc ):
10171018 return [self .syn (s ) for s in syn .hypernyms ()]
@@ -1123,11 +1124,12 @@ class WordData(_WordListBuiltin):
11231124 - WordNetID
11241125 - Lookup
11251126
1126- >> WordData["riverside", "Definitions"]
1127- = {{riverside, Noun, Bank} -> the bank of a river}
1127+ ## Not working yet
1128+ ## >> WordData["riverside", "Definitions"]
1129+ ## = {{riverside, Noun, Bank} -> the bank of a river}
11281130
1129- >> WordData[{"fish", "Verb", "Angle"}, "Examples"]
1130- = {{fish, Verb, Angle} -> {fish for compliments}}
1131+ ## >> WordData[{"fish", "Verb", "Angle"}, "Examples"]
1132+ ## = {{fish, Verb, Angle} -> {fish for compliments}}
11311133 """
11321134
11331135 messages = _merge_dictionaries (
@@ -1168,12 +1170,11 @@ def _standard_property(
11681170 word_property , "%s" % self ._underscore (py_property ), None
11691171 )
11701172 if property_getter :
1171- return ListExpression (
1172- * [
1173- Expression (SymbolRule , desc , property_getter (syn , desc ))
1174- for syn , desc in senses
1175- ]
1176- )
1173+ list_expr_elements = [
1174+ Expression (SymbolRule , desc , * property_getter (syn , desc ))
1175+ for syn , desc in senses
1176+ ]
1177+ return ListExpression (* list_expr_elements )
11771178 evaluation .message (self .get_name (), "notprop" , property )
11781179
11791180 def _parts_of_speech (self , py_word , wordnet , language_code ):
0 commit comments