This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scorer.py
77 lines (65 loc) · 2.29 KB
/
scorer.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import division
import os
import re
import math
import time
from phrase_extractor import PhraseExtractor
from search import Search
class Scorer:
phrasesRoot = os.getcwd() + "/phrases_big/"
# Get unscored phrase list file
# and return scored phrase file
def score_phrases(input_file,output_file):
# open files
fi = open(Scorer.phrasesRoot+input_file, 'r')
fo = open(Scorer.phrasesRoot+output_file, 'w')
# get hits of "poor" and "excellent"
hits_poor = Search.delayed_hits("poor")
hits_exc = Search.delayed_hits("excellent")
# iterate input file
for phrase in fi:
phrase = re.sub("\s+$","", phrase)
hits_ph_poor = Search.delayed_hits("\""+phrase+"\" NEAR poor")
hits_ph_exc = Search.delayed_hits("\""+phrase+"\" NEAR excellent")
so = 0
if(hits_ph_poor > 0 and hits_exc > 0 and hits_ph_exc > 0 and hits_poor > 0):
so = math.log((hits_ph_exc * hits_poor)/(hits_ph_poor * hits_exc),2)
fo.write(phrase+" "+str(so)+"\n")
fo.flush()
fi.close()
fo.close()
score_phrases = staticmethod(score_phrases)
def __init__(self, filename="scored.txt"):
self.filename = os.path.join(Scorer.phrasesRoot, filename)
self.scores = {}
f = open(self.filename, "r")
for line in f:
m = re.search("(.+\s.+)\s(.+)", line)
self.scores[m.groups()[0]] = float(m.groups()[1])
f.close()
# Takes an array or hash of phrases
# and calculates their average semantic orientation
def semantic_orientation(self, phrases):
so = 0
length = 0
for phrase in phrases:
if phrase in self.scores:
so = so + self.scores[phrase]
length = length + 1
if so > 0:
so = so / length
return so
# Reads a file, extracts relevant phrases and
# returns their average semantic orientation
def file_semantic_orientation(self,filename):
file_phrases = {}
PhraseExtractor.fillHash(file_phrases, filename)
return self.semantic_orientation(file_phrases)
def main():
print "In progress..."
#scorer = Scorer()
#print scorer.scores
#print scorer.semantic_orientation({"weird cover":3,"popular radio":2, "new trend":1})
Scorer.score_phrases("unscored.txt", "scored.txt")
if __name__ == "__main__":
main()