-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearnDecl.py
More file actions
172 lines (148 loc) · 5.68 KB
/
learnDecl.py
File metadata and controls
172 lines (148 loc) · 5.68 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python
# -*- coding: utf-8 -*-
import csv
import random
import sys
import unicodedata
from SPARQLWrapper import SPARQLWrapper, JSON
def strip_accents(word): # TODO: buggy
return ''.join(w for w in unicodedata.normalize('NFD', word)
if unicodedata.category(w) != 'Mn')
def get_results(endpoint_url, query):
sparql = SPARQLWrapper(endpoint_url)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query().convert()
def generate_lemmas(TARGET, LANG):
query = """SELECT ?lemma ?representation ?numberLabel ?caseLabel {
?lexeme <http://purl.org/dc/terms/language> wd:""" + TARGET + """.
?lexeme wikibase:lemma ?lemma .
?lexeme ontolex:lexicalForm ?form .
?form ontolex:representation ?representation .
?form wikibase:grammaticalFeature ?number, ?case .
?number wdt:P31 wd:Q104083 .
?case wdt:P31 wd:Q128234 .
SERVICE wikibase:label {
bd:serviceParam wikibase:language \"""" + LANG + """, en"
}
} order by ?lexeme"""
endpoint_url = "https://query.wikidata.org/sparql"
data = get_results(endpoint_url, query)
lemmas = []
for n in range(len(data["results"]["bindings"])):
lemma = data["results"]["bindings"][n]
lemmas.append({
"lemma": lemma["lemma"]["value"],
"case": lemma["caseLabel"]["value"],
"number": lemma["numberLabel"]["value"],
"repr": lemma["representation"]["value"]
})
return lemmas
LANG = "it"
loopLang = True
loopGame = True
loopMenu = True
while(loopLang):
loopLang = False
try:
targlang = input("Language code: ")
if (targlang == "de"):
TARGET = "Q188"
elif (targlang == "et"):
TARGET = "Q9072"
elif (targlang == "fi"):
TARGET = "Q1412"
elif (targlang == "la"):
TARGET = "Q397"
elif (targlang == "ru"):
TARGET = "Q7737"
elif (targlang == "sv"):
TARGET = "Q9027"
else:
print("Code not recognized")
loopLang = True
except IOError as e:
print(e)
loopMenu = False
points = [0, 0, 0]
cols = ["lemma", "case", "number", "repr"]
while loopMenu:
try:
menu = input("1. Play\n2. Update words\n3. Exit\nChoose: ")
if menu == "1":
# controlla se c'è il file relativo alle parole
# usa la versione online se non c'è
lemmas = []
csvfile = TARGET + ".csv"
with open(csvfile, "r") as output:
reader = csv.DictReader(output, fieldnames=cols)
for word in reader:
if TARGET == "la":
lemmas.append({
"lemma": word[cols[0]],
"case": word[cols[1]],
"number": word[cols[2]],
"repr": strip_accents(word[cols[3]])
})
else:
lemmas.append({
"lemma": word[cols[0]],
"case": word[cols[1]],
"number": word[cols[2]],
"repr": word[cols[3]]
})
word = []
for n in range(len(lemmas)):
if lemmas[n][cols[0]] not in word:
word.append(lemmas[n][cols[0]])
print("Numero di lemmi: " + str(len(word)))
print("Numero di parole: " + str(len(lemmas)))
while loopGame:
n = random.randint(0, len(lemmas) - 1)
word = []
for i in range(len(lemmas)):
if (lemmas[i][cols[0]] == lemmas[n][cols[0]]
and lemmas[i][cols[1]] == lemmas[n][cols[1]]
and lemmas[i][cols[2]] == lemmas[n][cols[2]]):
word.append(lemmas[i][cols[3]])
answer = input(lemmas[n][cols[0]] + " ("
+ lemmas[n][cols[1]] + ", " + lemmas[n][cols[2]] + "): ")
if (answer in word):
print("Giusto!")
if (len(word) > 1):
print("Le risposte valide erano: ")
for i in range(len(word)):
print(word[i])
points[0] += 1
elif (answer == ""):
print("Le risposte valide erano: ")
for i in range(len(word)):
print(word[i])
points[1] += 1
elif (answer == "exit"):
print("Numero di parole indovinate: " + str(points[0]))
print("Numero di parole lasciate in bianco: " +
str(points[1]))
print("Numero di parole sbagliate: " + str(points[2]))
print("Addio!")
loopGame = False
loopMenu = False
else:
points[2] += 1
print("Sbagliato! Le risposte valide erano: ")
for i in range(len(word)):
print(word[i])
elif menu == "2":
lemmas = generate_lemmas(TARGET, LANG)
csvfile = TARGET + ".csv"
with open(csvfile, 'w') as output:
writer = csv.DictWriter(output, fieldnames=cols)
for data in lemmas:
writer.writerow(data)
elif menu == "3":
print("Goodbye!")
loopMenu = False
else:
print("Wrong choice")
except IOError as e:
print(e)