8
8
import os .path
9
9
import argparse
10
10
import json
11
+ import itertools
11
12
import re
12
13
import traceback
13
14
14
15
import grammalecte
15
16
import grammalecte .text as txt
17
+ import grammalecte .graphspell .str_transform as strt
16
18
from grammalecte .graphspell .echo import echo
17
19
18
20
27
29
!word spelling suggestion
28
30
>word draw path of word in the word graph
29
31
=[filter1][=[filter2]] show entries which fit to filters (filter1 for word, filter2 for morphology)
30
- $some_text show sentences and tokens of text
32
+ ≠word|word|… show distance between words
33
+ $some_text show sentences and tokens of text
31
34
32
35
Other commands:
33
36
/help /h show this text
@@ -129,6 +132,10 @@ def getCommand ():
129
132
130
133
def main ():
131
134
"launch the CLI (command line interface)"
135
+ if sys .version_info < (3 , 5 ):
136
+ print ("Python 3.5+ required" )
137
+ return
138
+
132
139
xParser = argparse .ArgumentParser ()
133
140
xParser .add_argument ("-f" , "--file" , help = "parse file (UTF-8 required!) [on Windows, -f is similar to -ff]" , type = str )
134
141
xParser .add_argument ("-ff" , "--file_to_file" , help = "parse file (UTF-8 required!) and create a result file (*.res.txt)" , type = str )
@@ -153,7 +160,6 @@ def main ():
153
160
154
161
oGrammarChecker = grammalecte .GrammarChecker ("fr" )
155
162
oSpellChecker = oGrammarChecker .getSpellChecker ()
156
- oLexicographer = oGrammarChecker .getLexicographer ()
157
163
oTextFormatter = oGrammarChecker .getTextFormatter ()
158
164
if xArgs .personal_dict :
159
165
oJSON = loadDictionary (xArgs .personal_dict )
@@ -271,8 +277,10 @@ def main ():
271
277
for sWord in sText [1 :].strip ().split ():
272
278
if sWord :
273
279
echo ("* " + sWord )
274
- for sMorph in oSpellChecker .getMorph (sWord ):
275
- echo (" {:<32} {}" .format (sMorph , oLexicographer .formatTags (sMorph )))
280
+ for sElem , aRes in oSpellChecker .analyze (sWord ):
281
+ echo (" - " + sElem )
282
+ for sMorph , sMeaning in aRes :
283
+ echo (" {:<40} {}" .format (sMorph , sMeaning ))
276
284
elif sText .startswith ("!" ):
277
285
for sWord in sText [1 :].strip ().split ():
278
286
if sWord :
@@ -291,6 +299,11 @@ def main ():
291
299
sTagsPattern = ""
292
300
for aRes in oSpellChecker .select (sFlexPattern , sTagsPattern ):
293
301
echo ("{:<30} {:<30} {}" .format (* aRes ))
302
+ elif sText .startswith ("≠" ):
303
+ lWords = sText [1 :].split ("|" )
304
+ for s1 , s2 in itertools .combinations (lWords , 2 ):
305
+ nDist = strt .distanceDamerauLevenshtein (s1 , s2 )
306
+ print (f"{ s1 } ≠ { s2 } : { nDist } " )
294
307
elif sText .startswith ("/o+ " ):
295
308
oGrammarChecker .gce .setOptions ({ opt :True for opt in sText [3 :].strip ().split () if opt in oGrammarChecker .gce .getOptions () })
296
309
echo ("done" )
@@ -329,18 +342,31 @@ def main ():
329
342
if xArgs .textformatter :
330
343
sParagraph = oTextFormatter .formatText (sParagraph )
331
344
lParagraphErrors , lSentences = oGrammarChecker .gce .parse (sParagraph , bDebug = xArgs .debug , bFullInfo = True )
332
- echo (txt .getReadableErrors (lParagraphErrors , xArgs .width ))
345
+ # echo(txt.getReadableErrors(lParagraphErrors, xArgs.width))
333
346
for dSentence in lSentences :
334
- echo ("{nStart}:{nEnd}" .format (** dSentence ))
335
- echo (" <" + dSentence ["sSentence" ]+ ">" )
336
- for dToken in dSentence ["lToken" ]:
337
- echo (" {0[nStart]:>3}:{0[nEnd]:<3} {1} {0[sType]:<14} {2} {0[sValue]:<16} {3:<10} {4}" .format (dToken , \
338
- "×" if dToken .get ("bToRemove" , False ) else " " ,
339
- "!" if dToken ["sType" ] == "WORD" and not dToken .get ("bValidToken" , False ) else " " ,
340
- " " .join (dToken .get ("lMorph" , "" )), \
341
- "·" .join (dToken .get ("aTags" , "" )) ) )
342
- echo (txt .getReadableErrors (dSentence ["lGrammarErrors" ], xArgs .width ))
347
+ echo ("{nStart}:{nEnd} <{sSentence}>" .format (** dSentence ))
348
+ for dToken in dSentence ["lTokens" ]:
349
+ if dToken ["sType" ] == "INFO" or "bMerged" in dToken :
350
+ continue
351
+ echo (" {0[nStart]:>3}:{0[nEnd]:<3} {1} {0[sType]:<14} {2} {0[sValue]:<16} {3}" .format (dToken , \
352
+ "×" if dToken .get ("bToRemove" , False ) else " " ,
353
+ "!" if dToken ["sType" ] == "WORD" and not dToken .get ("bValidToken" , False ) else " " ,
354
+ " " .join (dToken .get ("aTags" , "" )) ) )
355
+ if "lMorph" in dToken :
356
+ for sMorph , sLabel in zip (dToken ["lMorph" ], dToken ["aLabels" ]):
357
+ echo (" {0:40} {1}" .format (sMorph , sLabel ))
358
+ if "lSubTokens" in dToken :
359
+ for dSubToken in dToken ["lSubTokens" ]:
360
+ if dSubToken ["sValue" ]:
361
+ echo (" · {0:20}" .format (dSubToken ["sValue" ]))
362
+ for sMorph , sLabel in zip (dSubToken ["lMorph" ], dSubToken ["aLabels" ]):
363
+ echo (" {0:40} {1}" .format (sMorph , sLabel ))
364
+ #echo(txt.getReadableErrors(dSentence["lGrammarErrors"], xArgs.width))
343
365
else :
366
+ if sText .startswith ("TEST: " ):
367
+ sText = sText [6 :]
368
+ sText = sText .replace ("{{" , "" ).replace ("}}" , "" )
369
+ sText = re .sub (" ->> .*$" , "" , sText ).rstrip ()
344
370
for sParagraph in txt .getParagraph (sText ):
345
371
if xArgs .textformatter :
346
372
sParagraph = oTextFormatter .formatText (sParagraph )
0 commit comments