-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive.py
38 lines (27 loc) · 1.03 KB
/
naive.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
__author__ = 'sagar, chitesh'
from probabilities import Probabilities
import result_cache
'''
POS tagger using Naive-Bayes Inference
Uses nave bayes formula to calculate best possible tag for given word.
P(S|W) = P(W|S) * P(S)
Si = arg max si P(Si = si|W):
'''
def get_part_of_speech(sentence):
pos = []
for word in sentence:
best_prob = 0
best_speech = ""
for speech in Probabilities.speech_prob.keys():
word_prob = Probabilities.get_naive_word_probability(word, speech) * Probabilities.speech_prob[speech]
# choose best possible tag for speech
if word_prob > best_prob:
best_prob = word_prob
best_speech = speech
# choose best speech by heuristic if no occurrences found
if best_prob == 0:
best_speech = Probabilities.get_best_possible_speech(word)
pos.append(best_speech)
# store result in result cache for future use
result_cache.naive_result = pos
return [[pos], []]