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
|
import re
import os
import sys
dict = {'Dict1': {"Pomme": "Poire"},
'Dict2': {"Banane": "Tomate"},
'Dict3': {"fleur": "Arbre"},}
def find_END(line):
if re.search(r'\bFIN\b', line):
return True
else:
return False
def replacer_factory1(dictionary):
def replacing(match):
if len(dictionary) > 0:
word = match.group()
exchange = dictionary.get(word, word)
return exchange
else:
return ""
return replacing
def replacing1(text, dict):
regex_patt_list = r'\b(?:' + '|'.join(dict) + r')\b'
replacer = replacer_factory1(dict)
return re.sub(regex_patt_list, replacer, text)
def folder(input,output):
with open(sys.argv[1] + input) as fin:
with open(sys.argv[1] + output, 'w') as fout:
key = 0
sous_dict = dict["Dict3"]
cache_mem = []
for line in fin:
cache_mem.append(line)
if find_END(line):
#print(cache_mem)
for x in cache_mem:
if x.strip() in dict.keys():
key = x.strip()
sous_dict = dict[key]
fout.write(replacing1(x,sous_dict))
cache_mem = []
print(sys.argv[0])
result = os.listdir(sys.argv[1])
for x in result:
folder(x, x + "V")
print(result) |