X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2Fengine%2FWiktionarySplitter.java;h=ed17fb5bf55c4f72f3bcc4dfe5d30f9a37cb98b4;hb=2b63f73985dbbaf4069086ec6d9abcff6f488bb0;hp=097643f1af7e3762cb1d76b16e0a44c4e0f3e240;hpb=863fc804a6496c920a2b2045913c45f938bb646c;p=DictionaryPC.git diff --git a/src/com/hughes/android/dictionary/engine/WiktionarySplitter.java b/src/com/hughes/android/dictionary/engine/WiktionarySplitter.java index 097643f..ed17fb5 100644 --- a/src/com/hughes/android/dictionary/engine/WiktionarySplitter.java +++ b/src/com/hughes/android/dictionary/engine/WiktionarySplitter.java @@ -20,8 +20,8 @@ import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -32,8 +32,8 @@ import java.util.regex.Pattern; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; -import org.apache.xerces.jaxp.SAXParserFactoryImpl; import org.apache.commons.compress.compressors.CompressorStreamFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -46,7 +46,8 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { // {{=uk=}} // Spanish has no initial headings, tried to also detect {{ES as such // with "^(\\{\\{ES|(=+)[^=]).*$" but that broke English. - static final Pattern headingStart = Pattern.compile("^(=+)[^=].*$", Pattern.MULTILINE); + static final Matcher headingStart = Pattern.compile("^(=+)[^=].*$", Pattern.MULTILINE).matcher(""); + static final Matcher startSpanish = Pattern.compile("\\{\\{ES(\\|[^{}=]*)?}}").matcher(""); final Map> pathToSelectors = new LinkedHashMap>(); List currentSelectors = null; @@ -75,7 +76,7 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { } private void go() throws Exception { - final SAXParser parser = SAXParserFactoryImpl.newInstance().newSAXParser(); + final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); // Configure things. for (final Map.Entry> pathToSelectorsEntry : pathToSelectors.entrySet()) { @@ -83,7 +84,9 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { currentSelectors = pathToSelectorsEntry.getValue(); for (final Selector selector : currentSelectors) { - OutputStream tmp = new FileOutputStream(selector.outFilename); + OutputStream tmp = new FileOutputStream(selector.outFilename + ".gz"); + tmp = new BufferedOutputStream(tmp); + tmp = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, tmp); tmp = new WriteBuffer(tmp, 20 * 1024 * 1024); selector.out = new DataOutputStream(tmp); } @@ -103,7 +106,7 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { parser.parse(new BufferedInputStream(in), this); } } catch (Exception e) { - System.err.println("Exception during parse, lastPageTitle=" + lastPageTitle + ", titleBuilder=" + titleBuilder.toString()); + System.err.println("Exception during parse, lastPageTitle=" + lastPageTitle + ", titleBuilder=" + titleBuilder.toString() + " of file " + pathToSelectorsEntry.getKey()); throw e; } @@ -117,21 +120,23 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { String lastPageTitle = null; int pageCount = 0; - Pattern endPatterns[] = new Pattern[100]; + Matcher endPatterns[] = new Matcher[100]; - private Pattern getEndPattern(int depth) { + private Matcher getEndPattern(int depth) { if (endPatterns[depth] == null) - endPatterns[depth] = Pattern.compile(String.format("^={1,%d}[^=].*$", depth), Pattern.MULTILINE); + endPatterns[depth] = Pattern.compile(String.format("^={1,%d}[^=].*$", depth), Pattern.MULTILINE).matcher(""); return endPatterns[depth]; } private void endPage() { final String title = titleBuilder.toString(); lastPageTitle = title; - if (++pageCount % 1000 == 0) { + if (++pageCount % 100000 == 0) { System.out.println("endPage: " + title + ", count=" + pageCount); } - if (title.startsWith("Wiktionary:") || + if (title.startsWith("Unsupported titles/")) return; + if (title.contains(":")) { + if (title.startsWith("Wiktionary:") || title.startsWith("Appendix:") || title.startsWith("Help:") || title.startsWith("Index:") || @@ -142,13 +147,13 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { title.startsWith("Rhymes:") || title.startsWith("Category:") || title.startsWith("Wikisaurus:") || - title.startsWith("Unsupported titles/") || title.startsWith("Transwiki:") || title.startsWith("File:") || title.startsWith("Thread:") || title.startsWith("Template:") || title.startsWith("Summary:") || title.startsWith("Module:") || + title.startsWith("Reconstruction:") || // DE title.startsWith("Datei:") || title.startsWith("Verzeichnis:") || @@ -157,6 +162,7 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { title.startsWith("Kategorie:") || title.startsWith("Hilfe:") || title.startsWith("Reim:") || + title.startsWith("Modul:") || // FR: title.startsWith("Annexe:") || title.startsWith("Catégori:") || @@ -166,16 +172,20 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { title.startsWith("Aide:") || title.startsWith("Fichier:") || title.startsWith("Wiktionnaire:") || + title.startsWith("Translations:Wiktionnaire:") || + title.startsWith("Translations:Projet:") || title.startsWith("Catégorie:") || title.startsWith("Portail:") || title.startsWith("utiliusateur:") || title.startsWith("Kategorio:") || + title.startsWith("Tutoriel:") || // IT title.startsWith("Wikizionario:") || title.startsWith("Appendice:") || title.startsWith("Categoria:") || title.startsWith("Aiuto:") || title.startsWith("Portail:") || + title.startsWith("Modulo:") || // ES title.startsWith("Apéndice:") || title.startsWith("Archivo:") || @@ -184,58 +194,70 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { title.startsWith("Plantilla:") || title.startsWith("Wikcionario:") || + // PT + title.startsWith("Ajuda:") || + title.startsWith("Apêndice:") || + title.startsWith("Citações:") || + title.startsWith("Portal:") || + title.startsWith("Predefinição:") || + title.startsWith("Vocabulário:") || + title.startsWith("Wikcionário:") || + title.startsWith("Módulo:") || + // sentinel false - ) { - return; - } - if (title.contains(":")) { - if (!title.startsWith("Sign gloss:")) { + ) return; + // leave the Flexion: pages in for now and do not warn about them + if (!title.startsWith("Sign gloss:") && !title.startsWith("Flexion:")) { System.err.println("title with colon: " + title); } } String text = textBuilder.toString(); + // Workaround for Spanish wiktionary {{ES}} and {{ES|word}} patterns + text = startSpanish.reset(text).replaceAll("== {{lengua|es}} =="); String translingual = ""; + int start = 0; + headingStart.reset(text); - while (text.length() > 0) { + while (start < text.length()) { // Find start. - final Matcher startMatcher = headingStart.matcher(text); - if (!startMatcher.find()) { + if (!headingStart.find(start)) { return; } - text = text.substring(startMatcher.end()); - - final String heading = startMatcher.group(); - for (final Selector selector : currentSelectors) { - if (heading.indexOf("Translingual") != -1) { - // Find end. - final int depth = startMatcher.group(1).length(); - final Pattern endPattern = getEndPattern(depth); - - final Matcher endMatcher = endPattern.matcher(text); - if (endMatcher.find()) { - int end = endMatcher.start(); - translingual = text.substring(0, endMatcher.start()); - text = text.substring(end); - break; - } + start = headingStart.end(); + + final String heading = headingStart.group(); + + // For Translingual entries just store the text for later + // use in the per-language sections + if (heading.indexOf("Translingual") != -1) { + // Find end. + final int depth = headingStart.group(1).length(); + final Matcher endMatcher = getEndPattern(depth).reset(text); + + if (endMatcher.find(start)) { + int end = endMatcher.start(); + translingual = text.substring(start, end); + start = end; + continue; } - if (selector.pattern.matcher(heading).find()) { + } + for (final Selector selector : currentSelectors) { + if (selector.pattern.reset(heading).find()) { // Find end. - final int depth = startMatcher.group(1).length(); - final Pattern endPattern = getEndPattern(depth); + final int depth = headingStart.group(1).length(); + final Matcher endMatcher = getEndPattern(depth).reset(text); - final Matcher endMatcher = endPattern.matcher(text); final int end; - if (endMatcher.find()) { + if (endMatcher.find(start)) { end = endMatcher.start(); } else { end = text.length(); } - String sectionText = text.substring(0, end); + String sectionText = text.substring(start, end); // Hack to remove empty dummy section from French if (sectionText.startsWith("\n=== {{S|étymologie}} ===\n: {{ébauche-étym")) { int dummy_end = sectionText.indexOf("}}", 41) + 2; @@ -257,7 +279,7 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { throw new RuntimeException(e); } - text = text.substring(end); + start = end; break; } } @@ -285,13 +307,13 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { static class Selector { final String outFilename; - final Pattern pattern; + final Matcher pattern; DataOutputStream out; public Selector(final String filename, final String pattern) { this.outFilename = filename; - this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); + this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(""); } } @@ -331,7 +353,7 @@ public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler { public void parse(final File file) throws ParserConfigurationException, SAXException, IOException { - final SAXParser parser = SAXParserFactoryImpl.newInstance().newSAXParser(); + final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse(file, this); }