Repository navigation
Expand file tree
/
Copy pathEmotionJudge.java
More file actions
235 lines (198 loc) · 8.12 KB
/
Copy pathEmotionJudge.java
File metadata and controls
235 lines (198 loc) · 8.12 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.geekgao.one;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class EmotionJudge {
private double priorPositive;//积极先验概率
private double priorNegative;//消极先验概率
private double priorUnsure;//不确定先验概率
private Map<String,Double> backPositive;//词语的后验概率
private Map<String,Double> backNegative;//同上
private Map<String,Double> backUnsure;//同上
private boolean isGroup = false;
private String strTemp;
private Map<String,Integer> articleWordMap;
//这两个是词典的位置
private final String posiDictPath = "/home/geekgao/朴素贝叶斯/台湾大学情感词典/ntusd-positive.txt";
private final String negaDictPath = "/home/geekgao/朴素贝叶斯/台湾大学情感词典/ntusd-negative.txt";
//这两个存储词典中的词语
private Set<String> positiveDict;
private Set<String> negativeDict;
public static void main(String [] args) {
new EmotionJudge().launch();
}
public void launch() {
getPrior();
getBack();
positiveDict = new HashSet<String>();
negativeDict = new HashSet<String>();
readEmotionWord(positiveDict, posiDictPath);
readEmotionWord(negativeDict, negaDictPath);
calc();
}
//获得先验概率
public void getPrior() {
SAXReader sax = new SAXReader();
try {
//从这读取doc的值
Document document = sax.read(new File("/home/geekgao/doc.xml"));
Element root = document.getRootElement();
List<Element> prior = root.elements();
priorPositive = Double.valueOf(prior.get(0).attributeValue("pPositive"));
priorNegative = Double.valueOf(prior.get(0).attributeValue("pNegative"));
priorUnsure = Double.valueOf(prior.get(0).attributeValue("pUnsure"));
} catch (DocumentException e) {
e.printStackTrace();
}
}
//获得后验概率
public void getBack() {
SAXReader sax = new SAXReader();
try {
//从这读取weight的值
Document document = sax.read(new File("/home/geekgao/weight.xml"));
Element root = document.getRootElement();
List<Element> back = root.elements();
backNegative = new HashMap<String, Double>();
backPositive = new HashMap<String, Double>();
backUnsure = new HashMap<String, Double>();
double backPos;//积极后验概率
double backNeg;//消极后验概率
double backUns;//不确定后验概率
String word;
for (int i = 0;i < back.size();i++) {
backPos = Double.valueOf(back.get(i).attributeValue("pPositive"));
backNeg = Double.valueOf(back.get(i).attributeValue("pNegative"));
backUns = Double.valueOf(back.get(i).attributeValue("pUnsure"));
word = back.get(i).attributeValue("data");
backPositive.put(word,backPos);
backNegative.put(word,backNeg);
backUnsure.put(word,backUns);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public void calc() {
articleWordMap = new HashMap<String, Integer>();
//读取文章
calcFreauency(articleWordMap,new File("/home/geekgao/朴素贝叶斯/500trainblogxml/positiveout/1377331000713.txt"));
keepEmotionWord(articleWordMap);
double allBackPos = 1;
double allBackNeg = 1;
double allBackUns = 1;
Set<String> word = articleWordMap.keySet();
for (Iterator it = word.iterator();it.hasNext();) {
String tmp = (String)it.next();
double back;
if (backPositive.containsKey(tmp)) {
back = backPositive.get(tmp);
allBackPos = Math.pow(back,articleWordMap.get(tmp)) * allBackPos;
}
if (backNegative.containsKey(tmp)) {
back = backNegative.get(tmp);
allBackNeg= Math.pow(back,articleWordMap.get(tmp)) * allBackNeg;
}
if (backUnsure.containsKey(tmp)) {
back = backUnsure.get(tmp);
allBackUns = Math.pow(back,articleWordMap.get(tmp)) * allBackUns;
}
}
double resultPositive;
double resultNegative;
double resultUnsure;
resultPositive = priorPositive * allBackPos;
resultNegative = priorNegative * allBackNeg;
resultUnsure = priorUnsure * allBackUns;
System.out.println("积极:" + resultPositive);
System.out.println("消极:" + resultNegative);
System.out.println("不确定:" + resultUnsure);
}
//解析出文章中的词语,并且映射上频数
public void calcFreauency(Map<String,Integer> wordMap,File article) {
try {
//取得dom4j的解析器
SAXReader reader = new SAXReader();
//取得代表文档的Document对象
Document document = reader.read(article);
//取得根结点
Element root = document.getRootElement();//取得根节点<document>
List<?> list1 = root.elements();//取得<document>的子节点
List<?> sentence_list = ((Element)list1.get(0)).elements();//<content>下的<sentence>集合
List<?> tok_list;//<sentence>下的<tok>集合
//遍历<sentence>节点
for (int i = 0; i < sentence_list.size(); i++) {
tok_list = ((Element)sentence_list.get(i)).elements();//获得每个sentence的tok集合
for (int j = 0;j < tok_list.size();j++) {
setWordMap((Element)tok_list.get(j),wordMap);
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public void setWordMap(Element tok,Map<String,Integer> wordMap) {
String type,text;
List<?> list;
if (!(tok.getName().equals("tok"))) {//如果不是tok节点,那么就不用处理了
return ;
}
//获取属性type
type = tok.attributeValue("type");
//只访问原子节点
if (type.equals("atom") && isGroup) {
text = tok.getText();
text = text.replace("\t", "");
text = text.replace("\n", "");
/*System.out.print(text + " ");*/
strTemp = strTemp + text;
} else if (type.equals("group")) {
isGroup = true;
strTemp = "";
list = tok.elements();
for (int k = 0,size3 = list.size();k < size3;k++) {
tok = (Element)list.get(k);
setWordMap(tok,wordMap);
}
Integer count = wordMap.get(strTemp);//计算当前map里面的当前text对应的次数
wordMap.put(strTemp,count == null?1:count + 1);
isGroup = false;
}
}
public void keepEmotionWord(Map<String,Integer> wordMap) {
Set<String> word = wordMap.keySet();
for (Iterator it = word.iterator();it.hasNext();) {
String tmp = (String)it.next();
//两个情感词典都不包含这个词语,那么就把这个词语去掉
if (!positiveDict.contains(tmp) && !negativeDict.contains(tmp)) {
it.remove();
}
}
}
public void readEmotionWord(Set<String> Dict, String dictPath) {
File file = new File(dictPath);
BufferedReader reader = null;
try {
String t;
reader = new BufferedReader(new FileReader(file));
while ((t = reader.readLine()) != null) {
Dict.add(t);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
}