-
Notifications
You must be signed in to change notification settings - Fork 48
/
lemmatizer.py
executable file
·196 lines (179 loc) · 5.14 KB
/
lemmatizer.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
# coding: utf-8
import subprocess
import requests
import json
def tag_ud(port, text="Do not forget to pass some text as a string!", lang="english"):
# UDPipe tagging for any language you have a model for.
# Demands UDPipe REST server (https://ufal.mff.cuni.cz/udpipe/users-manual#udpipe_server)
# running on a port defined in webvectors.cfg
# Start the server with something like:
# udpipe_server --daemon 46666 english english /opt/my.model UD
# Sending user query to the server:
ud_reply = requests.post(
"http://localhost:%s/process" % port,
data={"tokenizer": "", "tagger": "", "data": text, "model": lang},
).content
# Getting the result in the CONLLU format:
processed = json.loads(ud_reply.decode("utf-8"))["result"]
# Skipping technical lines:
content = [line for line in processed.split("\n") if not line.startswith("#")]
# Extracting lemmas and tags from the processed queries:
tagged = [w.split("\t")[1] + '_' + w.split("\t")[2].lower() + "_" + w.split("\t")[3] for w in content if w]
poses = [t.split("_")[2] for t in tagged]
tokens = [t.split("_")[0] for t in tagged]
lemmas = [t.split("_")[1] for t in tagged]
return tokens, lemmas, poses
def tagword(word, return_tokens=False):
# Stanford CoreNLP tagging for English (and other languages)
# Demands Stanford Core NLP server running on a defined port
# Start server with something like:
# java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer --port 9999
port = 29999
corenlp = requests.post(
"http://localhost:%s/?properties="
'{"annotators": "tokenize, pos, lemma", "outputFormat": "json"}' % port,
data=word.encode("utf-8"),
).content
tagged = json.loads(corenlp.decode("utf-8"), strict=False)
if len(tagged["sentences"]) < 1:
return "Error!"
poses = []
tokens = []
lemmas = []
for el in tagged["sentences"][0]["tokens"]:
pos = el["pos"]
token = el["word"]
lemma = el["lemma"]
tokens.append(token)
lemmas.append(lemma)
poses.append(ptb2upos[pos])
if return_tokens:
return tokens, lemmas, poses
return poses
def freeling_lemmatizer(word):
# Freeling tagging for Russian
# Queries Freeling service at localhost port 50006
freeling = subprocess.Popen(
[u"/usr/local/bin/analyzer_client", u"50006"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
tagged = freeling.communicate(word.encode("utf-8").strip())
tagged = tagged[0].decode("utf-8").split("\n")
tagged = [line for line in tagged if len(line) > 0]
if len(tagged) < 1:
return "Error!"
poses = []
for line in tagged:
tag = line.split()[2]
freeling_pos = tag[0]
if freeling_pos in freeling2upos:
universal_pos = freeling2upos[freeling_pos]
if universal_pos == "NOUN":
noun_type = tag[1]
if noun_type == "P":
universal_pos = "PROPN"
if len(tag) > 5:
freeling_info = tag[6]
if (
freeling_info == "G"
or freeling_info == "N"
or freeling_info == "S"
or freeling_info == "F"
):
universal_pos = "PROPN"
else:
universal_pos = "X"
poses.append(universal_pos)
return poses
# Mappings from Freeling tags to Universal tags.
freeling2upos = {
"A": "ADJ",
"N": "NOUN",
"V": "VERB",
"Q": "NOUN",
"D": "ADV",
"E": "PRON",
"P": "ADV",
"Y": "ADJ",
"R": "DET",
"C": "CCONJ",
"J": "INTJ",
"Z": "NUM",
"T": "PART",
"B": "ADP",
}
# Mappings from Penn Treebank tagset to Universal PoS tags
ptb2upos = {
"!": "PUNCT",
"#": "PUNCT",
"$": "PUNCT",
"''": "PUNCT",
"(": "PUNCT",
")": "PUNCT",
",": "PUNCT",
"-LRB-": "PUNCT",
"-RRB-": "PUNCT",
".": "PUNCT",
":": "PUNCT",
"?": "PUNCT",
"CC": "CCONJ",
"CD": "NUM",
"CD|RB": "X",
"DT": "DET",
"DT.": "DET",
"EX": "DET",
"FW": "X",
"IN": "ADP",
"IN|RP": "ADP",
"JJ": "ADJ",
"JJR": "ADJ",
"JJRJR": "ADJ",
"JJS": "ADJ",
"JJ|RB": "ADJ",
"JJ|VBG": "ADJ",
"LS": "X",
"MD": "AUX",
"NN": "NOUN",
"NNP": "PROPN",
"NNPS": "PROPN",
"NNS": "NOUN",
"NN|NNS": "NOUN",
"NN|SYM": "NOUN",
"NN|VBG": "NOUN",
"NP": "NOUN",
"PDT": "DET",
"POS": "PART",
"PRP": "PRON",
"PRP$": "PRON",
"PRP|VBP": "PRON",
"PRT": "PART",
"RB": "ADV",
"RBR": "ADV",
"RBS": "ADV",
"RB|RP": "ADV",
"RB|VBG": "ADV",
"RN": "X",
"RP": "PART",
"SYM": "SYM",
"TO": "PART",
"UH": "INTJ",
"VB": "VERB",
"VBD": "VERB",
"VBD|VBN": "VERB",
"VBG": "VERB",
"VBG|NN": "VERB",
"VBN": "VERB",
"VBP": "VERB",
"VBP|TO": "VERB",
"VBZ": "VERB",
"VP": "VERB",
"V": "VERB",
"WDT": "DET",
"WH": "X",
"WP": "PRON",
"WP$": "PRON",
"WRB": "ADV",
"``": "PUNCT",
}