/
ThriftClient.java
187 lines (167 loc) · 5.48 KB
/
ThriftClient.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.TSSLTransportFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;
public class ThriftClient
{
String ip;
int port;
TTransport transport = null;
PyInterface.Client client = null;
TProtocol protocol = null;
static Pattern hashtagPattern = Pattern.compile("#(\\w+|\\W+)");
public ThriftClient(String s, int p){
this.ip= s;
this.port = p;
if (transport == null)
transport = new TSocket(this.ip, this.port);
if (protocol == null)
{
protocol = new TBinaryProtocol(transport);
client = new PyInterface.Client(protocol);
try {
transport.open();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
//public String getSentimentScore(String mainText,String textType)
public String getSentimentScore(String mainText)
{
try {
SentiRequestObject obj = new SentiRequestObject();
obj.setMainText(mainText);
//obj.setTextType(textType);
int senti = client.getSentimentScore(obj);
return ("" + senti);
} catch (TTransportException e) {
e.printStackTrace();
transport.close();
} catch (TException e) {
e.printStackTrace();
transport.close();
}
transport.close();
return "Connection to " + this.ip + ":" + this.port + " failed!";
}
public String getSentimentScore(String mainText,String title,String topDomain,String subDomain)
{
try {
SentiRequestObject obj = new SentiRequestObject();
obj.setMainText(mainText);
obj.setTextType("reviews");
obj.setTitle(title);
obj.setTopDomain(topDomain);
obj.setSubDomain(subDomain);
int senti = client.getSentimentScore(obj);
return ("" + senti);
} catch (TTransportException e) {
e.printStackTrace();
transport.close();
} catch (TException e) {
e.printStackTrace();
transport.close();
}
transport.close();
return "Connection to " + this.ip + ":" + this.port + " failed!";
}
public String getSentimentScore(String mainText,String title,String middleParas,String lastPara, int diffBlog)
{
// "diffBlog" parameter can be set with any integer (added as a dummy parameter to support method overloading)
try {
SentiRequestObject obj = new SentiRequestObject();
obj.setMainText(mainText);
obj.setTextType("blogs_news");
obj.setTitle(title);
obj.setMiddleParas(middleParas);
obj.setLastPara(lastPara);
int senti = client.getSentimentScore(obj);
return ("" + senti);
} catch (TTransportException e) {
e.printStackTrace();
transport.close();
} catch (TException e) {
e.printStackTrace();
transport.close();
}
transport.close();
return "Connection to " + this.ip + ":" + this.port + " failed!";
}
public List<String> getTopics(String mainText)
{
try
{
List<String> topics = client.getTopics(mainText);
return topics;
}catch (TTransportException e) {
e.printStackTrace();
transport.close();
} catch (TException e) {
e.printStackTrace();
transport.close();
}
transport.close();
return null;
}
public String getAcronyms(String mainText)
{
try
{
String Acronyms=client.ExAcro(mainText);
return Acronyms;
}catch (TTransportException e) {
e.printStackTrace();
transport.close();
} catch (TException e) {
e.printStackTrace();
transport.close();
}
transport.close();
return null;
}
public List<String> extractHashtags(String text)
{
Matcher mat = hashtagPattern.matcher(text);
List<String> hashtags=new ArrayList<String>();
while (mat.find())
{
hashtags.add(mat.group());
}
return hashtags;
}
public String getEmoticons(String mainText)
{
try
{
String Emoticons=client.ExEmo(mainText);
return Emoticons;
}catch (TTransportException e) {
e.printStackTrace();
transport.close();
} catch (TException e) {
e.printStackTrace();
transport.close();
}
transport.close();
return null;
}
public static void main(String [] Args)
{
ThriftClient c = new ThriftClient("localhost",8002);
String text="hey lol lysm idiot #health ,hey bro :) long tym :p no #tweet c :( ";
System.out.println(c.getSentimentScore(text));
System.out.println(c.getTopics(text));
System.out.println(c.getAcronyms(text));
System.out.println(c.getEmoticons(text));
System.out.println(c.extractHashtags(text));
}
}