-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVectorWritter3.java
108 lines (99 loc) · 3.19 KB
/
VectorWritter3.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
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 VectorWritter3 extends JCasConsumer_ImplBase {
List<List<String>> terms;
public VectorWritter3() {
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[40];
double[][] tf = new double[document_size][40];
int p_index = 0;
for(Paragraph p : JCasUtil.select(jcas, Paragraph.class)) {
int term_index = 0;
for(List<String> term: this.terms) {
int tf_times = 0;
for(Lemma l : JCasUtil.selectCovered(Lemma.class, p)) {
for(String s : term) {
if((l.getCoveredText().toLowerCase()).contains(s)){
tf_times++;
}
}
}
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++;
}
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[40];
out.write(String.valueOf(Double.parseDouble(c.getCoveredText())*2)+"\t");
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");
}
out.write("\n");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}