-
Notifications
You must be signed in to change notification settings - Fork 0
/
POS Example.py
40 lines (28 loc) · 1003 Bytes
/
POS Example.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
import nltk
from nltk.corpus import brown
data=brown.tagged_sents(categories=["adventure"],tagset="universal")
#This example to see how NaiveBayes with only one feature works. (The only feature is word by itself)
from NaiveBayes import NaiveBayesModel
model=NaiveBayesModel()
model.train(data)
first_sent=data[0]
first_sent_words=[w for w,l in first_sent]
prediction=model.predict(first_sent_words)
print(prediction)
# This example to see how Naive Bayes with different features works
# So it will have a feature extractor
from NaiveBayesUpdate import NaiveBayesModel_v2
import collections
model=NaiveBayesModel_v2()
def feature_extractor(word):
feature_set={}
feature_set["word"]=word
return feature_set
formated_data=[]
for sent in data:
tmp=[(feature_extractor(w),l) for w,l in sent]
formated_data.extend(tmp)
model.train(formated_data)
first_sent_feature_set=[feature_extractor(w) for w,l in data[0]]
prediction=model.predict(first_sent_feature_set)
print(prediction)