-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSA.java
186 lines (155 loc) · 6.15 KB
/
LSA.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
/*
* Managing LSA vectors for the Vector Similarity feature. The LSA matrix
* was courtesy of Emanuele Bastianelli.
*
* Copyright (C) 2013 Lisa Vitolo <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Creative Commons
* Attribution-NonCommercial-ShareAlike 3.0 license.
* You should have received a copy of the license with this product.
* Otherwise, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
/*
* Vectors are stored in an external text file. The matrix is completely stored in RAM in the
* constructor, since vectors are not sorted by word and it's faster than doing consecutive lookups
* in an external file.
*/
public class LSA
{
private Map<POSTaggedToken, double[] > lsa;
public LSA()
{
lsa = new HashMap<>();
try {
BufferedReader br = new BufferedReader( new FileReader( Constants.getLSAMatrixPath() ) );
parseFile(br);
br.close();
} catch (IOException e) {
System.err.println("Could not read \"" + Constants.getLSAMatrixPath() + "\": " + e.getLocalizedMessage());
System.exit(-1);
}
}
/*
* To avoid the overhead of multiple calls to split() or other string parsing functions, I perform
* the parsing while reading the file. The BufferedReader class takes care of internal buffering, and
* so this function takes less than 30 seconds on a common laptop.
*
* The parsing is performed with a little DFA, so we expect every line to comply to a very
* restricted format. See lsa_matrix.txt .
*/
void parseFile(BufferedReader br) throws IOException
{
int c;
String currentToken = "";
POSTaggedToken tt = new POSTaggedToken();
double[] currentVector = new double[ Constants.getLSAVectorSize() ];
int currentVectorIndex = 0;
int countTab = 0;
int state = 0;
/* Little DFA to parse the matrix line character by character */
while ((c = br.read()) != -1) {
char ch = (char)c;
switch (state) {
case 0:
/* We reached the end of the word */
if (ch == ':') {
tt.token = currentToken; /* save it in the current tagged token object */
br.read(); /* discards the second ":" */
tt.tag = "";
char next = (char)br.read(); /* next character is the POS tag */
/* Handles little discrepancies between lines */
if (next == '\t') {
countTab++;
} else {
tt.tag += translateTag(next);
}
currentToken = "";
state = 1; /* Next step! */
} else {
currentToken += ch;
}
break;
case 1:
/* Here we simply count 3 tabs before we go on, ignoring everything else */
if (ch == '\t') {
countTab++;
if (countTab == 3) {
currentToken = "";
state = 2;
}
}
break;
/* Here we read the 100 elements of the vector, separated by commas */
case 2:
if (ch == ',') {
currentVector[currentVectorIndex++] = Double.parseDouble(currentToken);
currentToken = "";
} else if (ch == '\n') { /* end of line */
currentVector[currentVectorIndex++] = Double.parseDouble(currentToken);
lsa.put(tt, currentVector);
/* Restores state variables to the initial state for next line */
currentToken = "";
tt = new POSTaggedToken();
countTab = 0;
currentVectorIndex = 0;
currentVector = new double[ Constants.getLSAVectorSize() ];
state = 0;
} else {
currentToken += ch;
}
}
}
}
public double[] getWordVector(POSTaggedToken tt)
{
double[] vector;
tt.token = tt.token.toLowerCase();
if (!lsa.containsKey(tt)) {
vector = new double[ Constants.getLSAVectorSize() ]; /* a null vector */
} else {
vector = Arrays.copyOf(lsa.get(tt), Constants.getLSAVectorSize()); /* returns a copy */
}
return vector;
}
/*
* Another internal tagset, translated to our own
*/
private char translateTag(char tag)
{
switch (tag)
{
case 'p':
tag = 'O';
break;
case 'c':
tag = '&';
break;
case 'm':
tag = 'V';
break;
case 'w':
tag = 'R';
break;
case 'i':
tag = 'P';
break;
case 'j':
tag = 'A';
break;
case '#':
tag = ',';
break;
default:
tag = Character.toUpperCase(tag);
break;
}
return tag;
}
}