-
Notifications
You must be signed in to change notification settings - Fork 0
/
top10words.py
54 lines (52 loc) · 2.24 KB
/
top10words.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
import sys
import os
import pickle
from getWordDict import getAbsolutePath
from calculateParameterDict import loadWordMap
from predictDocumentClassLabel import loadParameterDict
reload ( sys )
sys.setdefaultencoding ( 'utf-8' )
def calculateTopK ( parameterDict , topK ):
relativeProbability = {};
topKDict ={};
for classLabel in parameterDict [ "prior" ]:
relativeProbability [ classLabel ] = {}
for wordId in parameterDict [ "maxwordlikelihood" ][ classLabel ]:
temp = 0 ;
for notclassLabel in parameterDict [ "prior" ]:
if notclassLabel != classLabel:
if wordId in parameterDict [ "maxwordlikelihood" ][ notclassLabel ]:
temp += parameterDict [ "maxwordlikelihood" ][ notclassLabel ][ wordId ]
temp = parameterDict [ "maxwordlikelihood" ][ classLabel ][ wordId ]/temp
relativeProbability [ classLabel ][ wordId ] = temp
sortedWords = sorted ( relativeProbability [ classLabel ].items() , key = lambda x:x[ 1 ] , reverse = True )
topKDict [ classLabel ] = []
k = 0
for item in sortedWords:
k += 1
topKDict[ classLabel ].append ( item )
if topK == k:
break
return topKDict
def storeTopK ( filename , topKDict , wordIdMap ):
f = open ( filename , "w" )
for classLabel in topKDict:
f.write ( "ClassLabel" + str ( classLabel ) )
f.write ( "\n" )
for item in topKDict [ classLabel ]:
f.write ( wordIdMap [ item [ 0 ] ] + " " + str ( item[ 1 ] ) + "\n" )
f.close()
if __name__ == "__main__":
textPath = sys.argv[ 1 ]
topK =int ( sys.argv [ 2 ] )
wordMapFilename = getAbsolutePath ( textPath , "store/wordDictByThreshold.pickle" )
wordMap = loadWordMap ( wordMapFilename )
parameterFilename = getAbsolutePath ( textPath , "store/parameterDict.pickle" )
parameterDict = loadParameterDict ( parameterFilename )
topKDict = calculateTopK ( parameterDict , topK )
#print ( topKDict )
wordIdMap = {} ;
for word in wordMap:
wordIdMap [ wordMap [ word ] ] = word
topKFilename = getAbsolutePath ( textPath , "topK.txt" )
storeTopK ( topKFilename , topKDict , wordIdMap )