-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
135 lines (126 loc) · 4.77 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# http://tokipona.alinome.net/dic_tp_raices.es.html
import random
def append_to_pos(key, pos, word, list):
if key == pos:
list.append(word)
return list
#TODO preprocess sentence to get pos
#TODO beautify
def get_possible_syntax():
pro_vi = ['pro','vi']
pro_vt = ['pro','vt', 'e', 'n']
pro_vt_adj = ['pro','vt', 'e', 'n', 'adj']
pro_vt_art = ['pro','vt', 'e', 'n', 'poss']
n_vi = ['n','li','vi']
n_adj_vi = ['n','adj','li','vi']
n_art_vi = ['n','poss','li','vi']
n_vt = ['n','li','vt','e', 'n']
n_vt_adj = ['n','li','vt','e', 'n', 'adj']
n_vt_art = ['n','li','vt', 'e', 'n', 'poss']
possible_syntax = [pro_vi, pro_vt, pro_vt_adj, pro_vt_art, n_vi, n_adj_vi, n_art_vi, n_vt, n_vt_adj,n_vt_art]
return possible_syntax
def get_sentence_syntax():
possible_syntax = get_possible_syntax()
sentence_syntax = random.choice(possible_syntax)
return sentence_syntax
def load_dict():
dict_list = open('dict.txt','r').read().splitlines()
words = list()
processed_dict = dict()
nouns = list()
adjs = list()
vts = list()
vis = list()
pro = ['mi','sina']
poss = ['mi', 'sina', 'jan', 'ona']
for entry in dict_list:
word = entry.split(' --> ')[0]
words.append(word)
meanings = entry.split(' --> ')[1].split('[')[1:]
meanings_dict = dict()
for each_entry in meanings:
meanings_dict[each_entry.split(']')[0]] = each_entry.split(']')[1]
processed_dict[word] = meanings_dict
for key in meanings_dict.keys():
nouns = append_to_pos(key, 'n', word, nouns)
if word not in ['sina','mi']:
adjs = append_to_pos(key, 'm', word, adjs)
vts = append_to_pos(key, 'vt', word, vts)
vis = append_to_pos(key, 'vi', word, vis)
return words, processed_dict, nouns, adjs, vts, vis, poss, pro
def get_sentence(sentence_syntax, nouns, vis, vts, adjs, poss, pro):
sentence = list()
for pos in sentence_syntax:
if pos == 'pro':
sentence.append(random.choice(pro))
sentence_syntax[0] = 'n'
if pos == 'n':
sentence.append(random.choice(nouns))
if pos == 'vt':
sentence.append(random.choice(vts))
if pos == 'vi':
sentence.append(random.choice(vis))
if pos == 'adj':
sentence.append(random.choice(adjs))
if pos == 'poss':
sentence.append(random.choice(poss))
if pos == 'li':
sentence.append('li')
if pos == 'e':
sentence.append('e')
sentence_str = ' '.join(sentence)
return sentence_str, sentence_syntax
def adj_or_poss(words, index, pos):
if words[index] in ['mi', 'sina', 'jan', 'ona']:
pos.extend(['poss'])
else:
pos.extend(['adj'])
return pos
def guess_possible_syntax(sentence, meanings_dict):
possible_syntax = get_possible_syntax()
words = sentence.split()
n_words = len(words)
possible_sentence_syntax = [syn for syn in possible_syntax if len(syn) == n_words]
if len(possible_sentence_syntax) == 1:
pos = possible_sentence_syntax[0]
else: #if there are cases where more than one syntax is possible
pos = ['n']
if words[1] == 'li':
pos.extend(['li','vt','e','n'])
if len(words) == 6:
pos = adj_or_poss(words,5,pos)
elif 'li' in words:
pos = adj_or_poss(words,1,pos)
pos.extend(['li','vi'])
elif 'vt' in meanings_dict[words[1]].keys():
pos.extend(['vt','e','n'])
if len(words) == 5:
pos = adj_or_poss(words,3,pos)
return pos
def analyse_sentence(sentence, meanings_dict, pos):
words_meanings = list()
if pos == '':
pos = guess_possible_syntax(sentence, meanings_dict)
word_index = 0
for word in sentence.split():
if word in meanings_dict.keys() and word not in ['li','e']:
word_pos = pos[word_index]
if word_pos in ['adj', 'poss']:
word_pos = 'm'
sense = meanings_dict[word][word_pos]
words_meanings.append('\t'.join([word,word_pos, sense]))
word_index += 1
return(sentence, words_meanings)
if __name__ == "__main__":
mode = input("Vols crear una frase (1) o interpretar una (2)?\n")
words, meanings_dict, nouns, adjs, vts, vis, poss, pro = load_dict()
if mode == '1':
sentence_syntax = get_sentence_syntax()
sentence, pos = get_sentence(sentence_syntax, nouns, vis, vts, adjs, poss, pro)
if mode == '2':
sentence = input("Escriu la teva frase:\n")
pos = ''
sentence, words_meanings = analyse_sentence(sentence, meanings_dict, pos)
print(sentence)
for word_sense in words_meanings:
print(word_sense)