-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsynonym_extraction.py
72 lines (63 loc) · 2.39 KB
/
synonym_extraction.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
import os
import re
input_nlu_file = './data/nlu/synonyms.md'
user_nlu_file = './data/nlu/user_nlu.md'
synonym_list = []
all_synonyms = []
def detect_synonym(lines):
""" Finds the lines in nlu.md that include the keyword "synonym:" """
synonym_list = []
for num, line in enumerate(lines, 1):
if 'synonym:' in line:
synonym_list.append(num)
return synonym_list
def find_synonym(file_link, lines, i, num):
""" Finds all synonyms in the file, strips them, and adds it all to a single list """
j = num[i]
while j < len(open(file_link).readlines( )):
if (re.match('\r?\n', lines[j])):
#print('line is empty:', j)
i = i + 1
return i
else:
text = lines[j]
#print('line contains synonym:', text)
all_synonyms.append(text[2:-1])
j = j + 1
def collect_synonym(file_link):
""" Takes the nlu.md files and collects the list of all synonyms and returns it """
with open(file_link, 'r') as f:
lines = []
lines = f.readlines()
num = []
num = detect_synonym(lines)
for i in range(len(num)):
find_synonym(file_link, lines, i, num)
f.close()
return all_synonyms
def write_synonym_to_file(lines, new_synonym, num):
""" Takes the new synonym given by the user and writes it to a secondary file """
with open(user_nlu_file, 'w') as f:
lines.insert(num, new_synonym)
lines = "".join(lines)
f.write(lines)
f.close()
def add_synonym(synonym_category, new_synonym):
""" Decides where in the file that the new synonym should be written and then calls the function to write there"""
with open(user_nlu_file, 'r') as f:
lines = f.readlines()
num = detect_synonym(lines)
f.close()
new_synonym = '- ' + new_synonym + '\n'
if synonym_category == 'find':
write_synonym_to_file(lines, new_synonym, num[0])
elif synonym_category == 'move':
write_synonym_to_file(lines, new_synonym, num[1])
elif synonym_category == 'pick up':
write_synonym_to_file(lines, new_synonym, num[2])
#if __name__ == "__main__":
#all_synonyms = collect_synonym(input_nlu_file) + collect_synonym(user_nlu_file)
#print(all_synonyms)
#synonym_category = 'move'
#new_synonym = 'TESTattachTEST'
#add_synonym(synonym_category, new_synonym)