-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpos_feature.py
More file actions
27 lines (25 loc) · 816 Bytes
/
pos_feature.py
File metadata and controls
27 lines (25 loc) · 816 Bytes
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
#!/usr/bin/env python
import nltk
from nltk import pos_tag, word_tokenize
from nltk.corpus import brown
brown_tagged = brown.tagged_sents()
unigram_tagger = nltk.UnigramTagger(brown_tagged)
def pos_tagger(context,position,dictionary):
list_tags = pos_tag(word_tokenize(context[position]))
x,y = list_tags[0]
dictionary["pos"] = y
return dictionary
def unigram_tagger(context,position):
list_tags = unigram_tagger.tag(word_tokenize(context[position]))
x,y = list_tags[0]
dictionary["unigram pos"] = y
return dictionary
def bigram_tagger(context, position):
if position == 0:
input = context[position]
else:
input = context[position] + " " + context[position-1]
list_tags = bigram_tagger.tag(word_tokenize(input))
x,y = list_tags[1]
dictionary["bigram pos"] = y
return dictionary