-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
84 lines (67 loc) · 2.42 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
import re
import time
import unicodedata
data = {}
with open("/data/fr_FR.json", "r") as read_file:
data = json.load(read_file)
def timer(arg):
return round(time.monotonic()-arg, 9)
def strip_accents(string):
return "".join(char for char in unicodedata.normalize("NFD", string) if unicodedata.category(char) != "Mn")
# reverse word into drow for a more effective lookup?
# pick a word like "aidées"
# checks if the end of the word is a known suffix (currently merged with next check)
# if so, proceed to the replacement to the infinitive form
# checks if the word exists
# if so, proceed to the replacement in string
def solution_1(string):
timestamp = time.monotonic()
for word in re.findall("\w+", string):
for v in data["words"]:
for suffix in v["suffixes"]:
if re.sub("(\w+)"+suffix+"$", "\\1"+v["infinitive"], word) in v["verbs"]:
string = string.replace(word, re.sub("(\w+)"+suffix+"$", "\\1"+v["infinitive"], word), 1)
print("Time: ", timer(timestamp))
return string
# pick a word like "aidées"
# is it in dictionary? Note: Dictionary is mainly a list of tuples - sort of - like ("aidées", "aider") generated from json
# if found, word is replaced
def solution_2(string):
timestamp = time.monotonic()
dic_ = {}
for v in data["words"]:
for verb in v["verbs"]:
for suffix in v["suffixes"]:
dic_.update( {re.sub("^(.*)"+v["infinitive"], "\\1"+suffix, verb) : verb} )
for word in re.findall("\w+", string):
if word in dic_.keys():
string = string.replace(word, dic_[word], 1)
print("Time: ", timer(timestamp))
return string
heredoc = """
Ceci est un texte d'exemple.
Le but est de remplacer des verbes conjugués par leur forme infinitive.
Mais également de simplifier les chaînes de caractères accentués.
"""
print("TEXTE ORIGINAL")
print("========================================")
print(heredoc)
print("")
print("TEXTE SANS ACCENT")
print("========================================")
heredoc = strip_accents(heredoc)
print(heredoc)
print("")
print("TEXTE AVEC VERBE SOUS FORME INFINITIVE")
print("Solution 1")
print("========================================")
s1 = solution_1(heredoc)
print(s1)
print("")
print("TEXTE AVEC VERBE SOUS FORME INFINITIVE")
print("Solution 2")
print("========================================")
s2 = solution_2(heredoc)
print(s2)
print("")