-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CommonCrawlToNgram.java
230 lines (209 loc) · 8.55 KB
/
CommonCrawlToNgram.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
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
/* LanguageTool, a natural language style checker
* Copyright (C) 2015 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool.dev.bigdata;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.jetbrains.annotations.NotNull;
import org.languagetool.Language;
import org.languagetool.Languages;
import org.languagetool.dev.eval.SimpleCorpusEvaluator;
import org.languagetool.tokenizers.Tokenizer;
import org.tukaani.xz.XZInputStream;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Indexing the CommonCrawl-based data from http://data.statmt.org/ngrams/
* to ngrams.
*
* @since 3.2
*/
class CommonCrawlToNgram implements AutoCloseable {
private final Language language;
private final File input;
private final File indexTopDir;
private final File evalFile;
private final Map<String, Long> unigramToCount = new HashMap<>();
private final Map<String, Long> bigramToCount = new HashMap<>();
private final Map<String, Long> trigramToCount = new HashMap<>();
private final Map<Integer, LuceneLiveIndex> indexes = new HashMap<>();
private int limit = 1000;
CommonCrawlToNgram(Language language, File input, File indexTopDir, File evalFile) throws IOException {
this.language = language;
this.input = input;
this.indexTopDir = indexTopDir;
this.evalFile = evalFile;
indexes.put(1, new LuceneLiveIndex(new File(indexTopDir, "1grams")));
indexes.put(2, new LuceneLiveIndex(new File(indexTopDir, "2grams")));
indexes.put(3, new LuceneLiveIndex(new File(indexTopDir, "3grams")));
}
@Override
public void close() throws IOException {
for (LuceneLiveIndex index : indexes.values()) {
index.close();
}
}
void setLimit(int limit) {
this.limit = limit;
}
void indexInputFile() throws IOException {
FileInputStream fin = new FileInputStream(input);
BufferedInputStream in = new BufferedInputStream(fin);
Tokenizer wordTokenizer = language.getWordTokenizer(); // TODO: use a more Google-like tokenizer
try (XZInputStream xzIn = new XZInputStream(in)) {
final byte[] buffer = new byte[8192];
int n;
while ((n = xzIn.read(buffer)) != -1) {
String buf = new String(buffer, 0, n); // TODO: not always correct, we need to wait for line end first?
String[] lines = buf.split("\n");
indexLine(wordTokenizer, lines);
}
}
}
private void indexLine(Tokenizer wordTokenizer, String[] lines) throws IOException {
for (String line : lines) {
List<String> tokens = wordTokenizer.tokenize(line);
//System.out.println("L: " + tokens);
String prevPrev = null;
String prev = null;
// TODO: add start and end tokens
for (String token : tokens) {
if (token.trim().isEmpty()) {
continue;
}
unigramToCount.compute(token, (k, v) -> v == null ? 1 : v + 1);
if (prev != null) {
String ngram = prev + " " + token;
bigramToCount.compute(ngram, (k, v) -> v == null ? 1 : v + 1);
}
if (prevPrev != null && prev != null) {
String ngram = prevPrev + " " + prev + " " + token;
trigramToCount.compute(ngram, (k, v) -> v == null ? 1 : v + 1);
if (trigramToCount.size() > limit) {
writeAndEvaluate();
}
}
prevPrev = prev;
prev = token;
}
}
writeAndEvaluate();
}
private void writeAndEvaluate() throws IOException {
writeToLucene(1, unigramToCount);
writeToLucene(2, bigramToCount);
writeToLucene(3, trigramToCount);
if (evalFile != null) {
SimpleCorpusEvaluator evaluator = new SimpleCorpusEvaluator(indexTopDir);
evaluator.run(evalFile);
} else {
System.out.println("Skipping evaluation, no evaluation file specified");
}
}
private void writeToLucene(int ngramSize, Map<String, Long> ngramToCount) throws IOException {
//System.out.println("WRITE: ");
LuceneLiveIndex index = indexes.get(ngramSize);
for (Map.Entry<String, Long> entry : ngramToCount.entrySet()) {
Term ngram = new Term("ngram", entry.getKey());
index.reader = DirectoryReader.open(index.indexWriter, true);
index.searcher = new IndexSearcher(index.reader);
// not sure why this doesn't work, should be faster:
/*DirectoryReader newReader = DirectoryReader.openIfChanged(reader);
if (newReader != null) {
reader = newReader;
}*/
TopDocs topDocs = index.searcher.search(new TermQuery(ngram), 2);
//System.out.println(ngram + " ==> " + topDocs.totalHits);
if (topDocs.totalHits == 0) {
Document doc = getDoc(entry.getKey(), entry.getValue());
index.indexWriter.addDocument(doc);
} else if (topDocs.totalHits == 1) {
int docNumber = topDocs.scoreDocs[0].doc;
Document document = index.reader.document(docNumber);
long oldCount = Long.parseLong(document.getField("count").stringValue());
//System.out.println(ngram + " -> " + oldCount + "+" + entry.getValue());
index.indexWriter.deleteDocuments(ngram);
index.indexWriter.addDocument(getDoc(entry.getKey(), oldCount + entry.getValue()));
// would probably be faster, but we currently rely on the count being a common field:
//indexWriter.updateNumericDocValue(ngram, "count", oldCount + entry.getValue());
} else if (topDocs.totalHits > 1) {
throw new RuntimeException("Got more than one hit for: " + ngram);
}
//System.out.println(" " + entry.getKey() + " -> " + entry.getValue());
}
// TODO: add/update 'totalTokenCount'
index.indexWriter.commit();
ngramToCount.clear();
}
@NotNull
private Document getDoc(String ngram, long count) {
Document doc = new Document();
doc.add(new Field("ngram", ngram, StringField.TYPE_STORED)); // TODO: store only for debugging
doc.add(getCountField(count));
return doc;
}
@NotNull
private LongField getCountField(long count) {
FieldType fieldType = new FieldType();
fieldType.setStored(true);
fieldType.setNumericType(FieldType.NumericType.LONG);
fieldType.setDocValuesType(DocValuesType.NUMERIC);
return new LongField("count", count, fieldType);
}
public static void main(String[] args) throws IOException {
if (args.length != 4) {
System.out.println("Usage: " + CommonCrawlToNgram.class + " <langCode> <input.xz> <ngramIndexDir> <simpleEvalFile>");
System.out.println(" <simpleEvalFile> a plain text file with simple error markup");
System.exit(1);
}
Language language = Languages.getLanguageForShortName(args[0]);
File input = new File(args[1]);
File outputDir = new File(args[2]);
File evalFile = new File(args[3]);
try (CommonCrawlToNgram prg = new CommonCrawlToNgram(language, input, outputDir, evalFile)) {
prg.indexInputFile();
}
}
class LuceneLiveIndex {
private final Directory directory;
private final IndexWriter indexWriter;
private DirectoryReader reader;
private IndexSearcher searcher;
LuceneLiveIndex(File dir) throws IOException {
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
directory = FSDirectory.open(dir.toPath());
indexWriter = new IndexWriter(directory, config);
reader = DirectoryReader.open(indexWriter, true); // TODO: see if false is faster
searcher = new IndexSearcher(reader);
}
void close() throws IOException {
indexWriter.close();
directory.close();
}
}
}