This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
language.py
141 lines (111 loc) · 3.51 KB
/
language.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
136
137
138
139
140
141
import nltk, spacy
from nltk.tokenize import sent_tokenize
from manager import PictoManager
class PictoWord:
POS = [
'ADJ', # adjective
'ADP', # adposition
'ADV', # adverb
'AUX', # auxiliary
'CONJ', # conjuction
'CCONJ', # coordinating conjunction
'DET', # determiner
'INTJ', # interjection
'NOUN', # noun
'NUM', # numeral
'PART', # particle
'PRON', # pronoun
'PROPN', # proper noun
'PUNCT', # punctuation
'SCONJ', # subordinating conjunction
'SYM', # symbol
'VERB', # verb
'X', # other
'SPACE' # space -- note: tokenizer deletes this
]
MAP = {
'ADJ' : "ADJ",
'ADP' : "PREP",
'ADV' : "ADVERB",
'AUX' : "OTHER",
'CONJ' : "CONJ",
'CCONJ' : "CCONJ",
'DET' : "DET",
'INTJ' : "INTERJ",
'NOUN' : "SUST",
'NUM' : "PRON",
'PART' : "OTHER",
'PRON' : "PRON",
'PROPN' : "SUST",
'PUNCT' : "OTHER",
'SCONJ' : "CONJ",
'SYM' : "OTHER",
'VERB' : "VERB",
'X' : "OTHER",
'SPACE' : "OTHER" # space -- note: tokenizer deletes this
}
def __init__(self, print, lemma, pos, picto=""):
self.__print = print
self.__lemma = lemma
self.__pos = pos
self.__picto = picto
def get_print(self):
return self.__print
def set_print(self, print):
self.__print = print
def get_lemma(self):
return self.__lemma
def set_lemma(self, lemma):
self.__lemma = lemma
def get_pos(self):
return PictoWord.MAP[self.__pos]
def set_pos(self, pos):
if pos not in PictoWord.POS:
pos = 'X'
self.__pos = pos
class PictoLanguage:
def __init__(self):
self.__NLP = spacy.load("es_core_news_sm") # WIP load before
def tokenize(self, sentence):
"""
Tokenizes sentence into an array of PictoWords:
"""
# iterate over words
nlp = self.__NLP(sentence)
print_arr = sentence.split()
lemma_arr = []
pos_arr = []
tokens = []
for token in nlp:
tokens.append((token.text, token.lemma_, token.pos_))
print(tokens)
i = 0
for p in print_arr:
lemma = ""
pos = "X"
iterate = True
while iterate:
if tokens[i][0] in p:
if tokens[i][2] != "PUNCT":
pos = tokens[i][2]
if pos == "VERB" or tokens[i][1][-2:] in ['ar','er','ir']:
lemma += tokens[i][1]
else:
lemma += tokens[i][0]
i += 1
else:
iterate = False
if i >= len(tokens):
iterate = False
lemma_arr.append(lemma)
pos_arr.append(pos)
tokens = []
for i in range(0, len(print_arr)):
word = PictoWord(print_arr[i], lemma_arr[i], pos_arr[i])
tokens.append(word)
# WIP word grouping using (( ... ))
return tokens
"""test
l = PictoLanguage()
print(l.tokenize("El pequeño ((Miguel Ángel)) quiere ir a su casa."))
"""