forked from SmoDav/mpesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverb-multi.py
84 lines (77 loc) · 1.85 KB
/
reverb-multi.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
78
79
80
81
82
83
###This file is used to use reverb function on given datasets###
###it creates text file and run in terminal then delete that file####
import os
import pandas as pd
import numpy as np
from nltk import tokenize
import re
from sentiment import parse_1
def reverb():
ticker, df=parse_1()
news=df.content
confidence=[]
subject=[]
object_=[]
verb=[]
single_news = "single_news_" + ticker + ".txt"
ticker_output = "output_" + ticker + ".txt"
for each_news in news:
news=each_news+'.'
print news
f=open(single_news,'w')
f.write(news)
f.close()
javaosarg = "java -Xmx512m -jar reverb-latest.jar " + single_news + " > " + ticker_output
#os.system("java -Xmx512m -jar reverb-latest.jar single_news.txt > output.txt")
os.system(javaosarg)
output=open(ticker_output,'r')
info=output.read()
output.close()
print "reverb finished, next parse"
confid,subj,obje,verb_=reverb_parse(info)
confidence.append(confid)
subject.append(subj)
object_.append(obje)
verb.append(verb_)
osrmarg1 = "rm " + single_news
osrmarg2 = "rm " + ticker_output
os.system(osrmarg1)
os.system(osrmarg2)
df['confidence']=confidence
df['subject']=subject
df['object']=object_
df['verb']=verb
file_='../data/reverb/'+ticker+'reverb.csv'
df.to_csv(file_)
def reverb_parse(info):
reverb_list=info.split('\n')
confi=[]
sub=[]
obj=[]
verb=[]
if len(reverb_list)==0:
confidence=0
subject=''
verb_=''
object_=''
else:
for i in range(len(reverb_list)-1):
buf=reverb_list[i].split('\t')
confi.append(float(buf[11]))
sub.append(buf[-3])
verb.append(buf[-2])
obj.append(buf[-1])
if len(confi)==0:
confidence=0
subject=''
verb_=''
object_=''
else:
idx=np.argmax(confi)
confidence=confi[idx]
subject=sub[idx]
object_=obj[idx]
verb_=verb[idx]
print "finish this news"
return confidence,subject,object_,verb_
reverb()