-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVectorWritter5.java
135 lines (126 loc) · 4.29 KB
/
VectorWritter5.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
package finalProject.fp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasConsumer_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Compound;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Paragraph;
public class VectorWritter5 extends JCasConsumer_ImplBase {
List<List<String>> terms;
public VectorWritter5() {
File termFile = new File("src/main/resources/feature_word/compress words with similar meaning.dat");
terms = new ArrayList<>();
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(termFile)));
String line = "";
while((line=reader.readLine())!=null) {
List<String> list = new ArrayList<>();
for(String s : line.split(" ")) {
list.add(s);
}
terms.add(list);
System.out.println(line);
}
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
Compound c = JCasUtil.select(jcas, Compound.class).iterator().next();
int document_size = JCasUtil.select(jcas, Paragraph.class).size();
int[] df = new int[6];
double[][] tf = new double[document_size][6];
double[] posRatio = new double[document_size];
int p_index = 0;
double positive = 0;
double negative = 0;
for(Paragraph p : JCasUtil.select(jcas, Paragraph.class)) {
int term_index = 0;
for(List<String> term: this.terms) {
int tf_times = 0;
String lastWord = null;
for(Lemma l : JCasUtil.selectCovered(Lemma.class, p)) {
for(String s : term) {
if((l.getCoveredText().toLowerCase()).contains(s)){
if(lastWord!=null&&(lastWord.equals("not")||lastWord.endsWith("n't")||lastWord.equals("only")||lastWord.equals("no")||lastWord.equals("without")||lastWord.equals("dont"))) {
if(tf_times>0) {
tf_times--;
lastWord = l.getCoveredText().toLowerCase();
break;
}
}
tf_times++;
if(term_index==0||term_index==1||term_index==2||term_index==3) negative++;
else if(term_index==4||term_index==5) positive++;
lastWord = l.getCoveredText().toLowerCase();
break;
}
}
lastWord = l.getCoveredText().toLowerCase();
}
if(tf_times>0)
tf[p_index][term_index] = 1+ Math.log10(tf_times);
else
tf[p_index][term_index] = 0;
if(tf_times>0) {
df[term_index]++;
}
term_index++;
}
if(positive>0||negative>0) posRatio[p_index]=positive/(positive+negative);
else posRatio[p_index] = 0.5;
// System.out.println("posRatio= "+posRatio[p_index]);
p_index++;
}
File vector_file = new File("src/main/resources/feature_word/vector.dat");
if(!vector_file.exists()) {
try {
vector_file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileWriter out = new FileWriter(vector_file,true);
double[] vector = new double[7];
out.write(c.getCoveredText()+"\t");
double posRatioAvg = 0.0;
for(int j=0;j<tf[0].length; j++) {
for(int i=0;i<tf.length;i++) {
vector[j] += tf[i][j];
}
if(df[j]>0)
vector[j] = vector[j]* Math.log10((double)document_size/df[j]);
else
vector[j] =0;
out.write(j+"\t"+String.valueOf(vector[j]*100/document_size)+"\t");
}
for(int i=0;i<document_size;i++) {
posRatioAvg += posRatio[i];
}
posRatioAvg = posRatioAvg/document_size;
vector[6] = posRatioAvg;
System.out.println("posRatioAvg= "+posRatioAvg);
out.write(6+"\t"+String.valueOf(vector[6]*10)+"\t");
out.write("\n");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}