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
|
import gnu.trove.TIntArrayList;
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
public class Corpus
{
private Lexicon<String> tokenLexicon = new Lexicon<String>();
private Lexicon<TIntArrayList> ngramLexicon = new Lexicon<TIntArrayList>();
private List<Edge> edges = new ArrayList<Edge>();
private Map<Ngram,List<Edge>> phraseToContext = new HashMap<Ngram,List<Edge>>();
private Map<Ngram,List<Edge>> contextToPhrase = new HashMap<Ngram,List<Edge>>();
public class Ngram
{
private Ngram(int id)
{
ngramId = id;
}
public int getId()
{
return ngramId;
}
public TIntArrayList getTokenIds()
{
return ngramLexicon.lookup(ngramId);
}
public String toString()
{
StringBuffer b = new StringBuffer();
for (int tid: getTokenIds().toNativeArray())
{
if (b.length() > 0)
b.append(" ");
b.append(tokenLexicon.lookup(tid));
}
return b.toString();
}
public int hashCode()
{
return ngramId;
}
public boolean equals(Object other)
{
return other instanceof Ngram && ngramId == ((Ngram) other).ngramId;
}
private int ngramId;
}
public class Edge
{
Edge(Ngram phrase, Ngram context, int count)
{
this.phrase = phrase;
this.context = context;
this.count = count;
}
public Ngram getPhrase()
{
return phrase;
}
public Ngram getContext()
{
return context;
}
public int getCount()
{
return count;
}
private Ngram phrase;
private Ngram context;
private int count;
}
List<Edge> getEdges()
{
return edges;
}
int numEdges()
{
return edges.size();
}
Set<Ngram> getPhrases()
{
return phraseToContext.keySet();
}
List<Edge> getEdgesForPhrase(Ngram phrase)
{
return phraseToContext.get(phrase);
}
Set<Ngram> getContexts()
{
return contextToPhrase.keySet();
}
List<Edge> getEdgesForContext(Ngram context)
{
return contextToPhrase.get(context);
}
int numTokens()
{
return tokenLexicon.size();
}
static Corpus readFromFile(Reader in) throws IOException
{
Corpus c = new Corpus();
// read in line-by-line
BufferedReader bin = new BufferedReader(in);
String line;
Pattern separator = Pattern.compile(" \\|\\|\\| ");
while ((line = bin.readLine()) != null)
{
// split into phrase and contexts
StringTokenizer st = new StringTokenizer(line, "\t");
assert (st.hasMoreTokens());
String phraseToks = st.nextToken();
assert (st.hasMoreTokens());
String rest = st.nextToken();
assert (!st.hasMoreTokens());
// process phrase
st = new StringTokenizer(phraseToks, " ");
TIntArrayList ptoks = new TIntArrayList();
while (st.hasMoreTokens())
ptoks.add(c.tokenLexicon.insert(st.nextToken()));
int phraseId = c.ngramLexicon.insert(ptoks);
Ngram phrase = c.new Ngram(phraseId);
// process contexts
String[] parts = separator.split(rest);
assert (parts.length % 2 == 0);
for (int i = 0; i < parts.length; i += 2)
{
// process pairs of strings - context and count
TIntArrayList ctx = new TIntArrayList();
String ctxString = parts[i];
String countString = parts[i + 1];
StringTokenizer ctxStrtok = new StringTokenizer(ctxString, " ");
while (ctxStrtok.hasMoreTokens())
{
String token = ctxStrtok.nextToken();
if (!token.equals("<PHRASE>"))
ctx.add(c.tokenLexicon.insert(token));
}
int contextId = c.ngramLexicon.insert(ctx);
Ngram context = c.new Ngram(contextId);
assert (countString.startsWith("C="));
Edge e = c.new Edge(phrase, context, Integer.parseInt(countString.substring(2).trim()));
c.edges.add(e);
// index the edge for fast phrase lookup
List<Edge> edges = c.phraseToContext.get(phrase);
if (edges == null)
{
edges = new ArrayList<Edge>();
c.phraseToContext.put(phrase, edges);
}
edges.add(e);
// index the edge for fast context lookup
edges = c.contextToPhrase.get(context);
if (edges == null)
{
edges = new ArrayList<Edge>();
c.contextToPhrase.put(context, edges);
}
edges.add(e);
}
}
return c;
}
}
|