-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOStagging.py
48 lines (37 loc) · 1.13 KB
/
POStagging.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
# Install by running:
# pip install spacy
# python -m spacy download en_core_web_sm
import spacy
nlp = spacy.load('en_core_web_sm')
def postagger(nlquestion):
'''
Input: Natural language sentence
Output: A list of tuples (a, b),
where a is a word in the sentence
and b is the coarse-grained part-of-speech tag of the word.
'''
doc = nlp(nlquestion)
ans = []
for token in doc:
ans.append((token.text, token.pos_))
return ans
def postagger2(nlquestion):
'''
Input: Natural language sentence
Output: A list of tuples (a, b),
where a is a word in the sentence
and b is the fine-grained part-of-speech tag of the word.
'''
doc = nlp(nlquestion)
ans = []
for token in doc:
ans.append((token.text, token.tag_))
return ans
def to_tags(question):
return " ".join(map(lambda x: x[1], postagger(question)))
def _test():
print(postagger("The quick brown fox jumps over the lazy dog"))
print(postagger2("The quick brown fox jumps over the lazy dog"))
print(to_tags("The quick brown fox jumps over the lazy dog"))
if __name__ == "__main__":
_test()