-
Notifications
You must be signed in to change notification settings - Fork 0
/
np.py
64 lines (50 loc) · 2.1 KB
/
np.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
import importlib.util
from reynir import Greynir
""" Determine which parts of sentences are noun phrases """
spec = importlib.util.spec_from_file_location("random_sentence_txt", "./random_sentence_txt.py")
random_sentence_txt_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(random_sentence_txt_module)
def get_random_sentence():
return random_sentence_txt_module.get_random_sentence_from_directory('./generated_data/noun')
def get_noun_phrases(sentence):
g = Greynir()
parsed_sentence = g.parse_single(sentence)
noun_phrases = []
if parsed_sentence.tree is None:
return noun_phrases
def traverse_tree(node):
if node.is_terminal:
return
if node.tag == "NP":
noun_phrases.append(node.text)
for child in node.children:
traverse_tree(child)
traverse_tree(parsed_sentence.tree)
return noun_phrases
"""
def main():
sentence = random_sentence_module.get_random_sentence_from_directory('./data/2019')
if not sentence:
print("Ekki fannst lögleg setning.")
return
print("Í nafnlið er aðalorðið nafnorð eða sérstætt fornafn. Finnið nafnliðina í eftirfarandi setningu: ")
print("Setning: ", sentence)
correct_noun_phrases = get_noun_phrases(sentence)
print("Skráið nafnliðina í setningunni (aðskiliði með kommu), eða ýtið á Enter ef engir nafnliðir eru:")
user_input = input().strip()
if user_input:
user_noun_phrases = [phrase.strip() for phrase in user_input.split(',')]
else:
user_noun_phrases = []
if set(user_noun_phrases) == set(correct_noun_phrases):
if correct_noun_phrases != []:
print("Rétt! Nafnliðurinn er:", ', '.join(correct_noun_phrases))
else:
print("Rétt! Setningin inniheldur engan nafnlið")
else:
if correct_noun_phrases != []:
print("Rangt. Nafnliðurinn í setningunni er:", ', '.join(correct_noun_phrases))
else:
print("Rangt! Setningin inniheldur engan nafnlið")
if __name__ == "__main__":
main()"""