1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary.engine;
17 import java.io.BufferedOutputStream;
18 import java.io.DataOutputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.LinkedHashMap;
24 import java.util.List;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.parsers.SAXParser;
32 import org.apache.xerces.jaxp.SAXParserFactoryImpl;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
36 import com.hughes.android.dictionary.parser.wiktionary.WiktionaryLangs;
38 public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler {
40 // The matches the whole line, otherwise regexes don't work well on French:
42 static final Pattern headingStart = Pattern.compile("^(=+)[^=].*$", Pattern.MULTILINE);
44 final Map<String,List<Selector>> pathToSelectors = new LinkedHashMap<String, List<Selector>>();
45 List<Selector> currentSelectors = null;
47 StringBuilder titleBuilder;
48 StringBuilder textBuilder;
49 StringBuilder currentBuilder = null;
51 public static void main(final String[] args) throws Exception {
52 final WiktionarySplitter wiktionarySplitter = new WiktionarySplitter();
53 wiktionarySplitter.go();
56 private WiktionarySplitter() {
57 List<Selector> selectors;
58 for (final String code : WiktionaryLangs.wikiCodeToIsoCodeToWikiName.keySet()) {
59 //if (!code.equals("fr")) {continue;}
60 selectors = new ArrayList<WiktionarySplitter.Selector>();
61 pathToSelectors.put(String.format("data/inputs/%swiktionary-pages-articles.xml", code), selectors);
62 for (final Map.Entry<String, String> entry : WiktionaryLangs.wikiCodeToIsoCodeToWikiName.get(code).entrySet()) {
63 final String dir = String.format("data/inputs/wikiSplit/%s", code);
64 new File(dir).mkdirs();
65 selectors.add(new Selector(String.format("%s/%s.data", dir, entry.getKey()), entry.getValue()));
70 private void go() throws Exception {
71 final SAXParser parser = SAXParserFactoryImpl.newInstance().newSAXParser();
74 for (final Map.Entry<String, List<Selector>> pathToSelectorsEntry : pathToSelectors.entrySet()) {
76 currentSelectors = pathToSelectorsEntry.getValue();
78 for (final Selector selector : currentSelectors) {
79 selector.out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(selector.outFilename)));
84 parser.parse(new File(pathToSelectorsEntry.getKey()), this);
85 } catch (Exception e) {
86 System.err.println("Exception during parse, lastPageTitle=" + lastPageTitle + ", titleBuilder=" + titleBuilder.toString());
91 for (final Selector selector : currentSelectors) {
98 String lastPageTitle = null;
100 private void endPage() {
101 final String title = titleBuilder.toString();
102 lastPageTitle = title;
103 if (++pageCount % 1000 == 0) {
104 System.out.println("endPage: " + title + ", count=" + pageCount);
107 String text = textBuilder.toString();
109 while (text.length() > 0) {
111 final Matcher startMatcher = headingStart.matcher(text);
112 if (!startMatcher.find()) {
115 text = text.substring(startMatcher.end());
117 final String heading = startMatcher.group();
118 for (final Selector selector : currentSelectors) {
119 if (selector.pattern.matcher(heading).find()) {
122 final int depth = startMatcher.group(1).length();
123 final Pattern endPattern = Pattern.compile(String.format("^={1,%d}[^=].*$", depth), Pattern.MULTILINE);
125 final Matcher endMatcher = endPattern.matcher(text);
127 if (endMatcher.find()) {
128 end = endMatcher.start();
133 final String sectionText = text.substring(0, end);
134 final Section section = new Section(title, heading, sectionText);
137 selector.out.writeUTF(section.title);
138 selector.out.writeUTF(section.heading);
139 final byte[] bytes = section.text.getBytes("UTF8");
140 selector.out.writeInt(bytes.length);
141 selector.out.write(bytes);
142 } catch (IOException e) {
143 throw new RuntimeException(e);
146 text = text.substring(end);
153 // -----------------------------------------------------------------------
155 static class Section implements java.io.Serializable {
156 private static final long serialVersionUID = -7676549898325856822L;
159 final String heading;
162 public Section(final String title, final String heading, final String text) {
164 this.heading = heading;
167 //System.out.printf("TITLE:%s\nHEADING:%s\nTEXT:%s\n\n\n\n\n\n", title, heading, text);
171 static class Selector {
172 final String outFilename;
173 final Pattern pattern;
175 DataOutputStream out;
177 public Selector(final String filename, final String pattern) {
178 this.outFilename = filename;
179 this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
183 // -----------------------------------------------------------------------
186 public void startElement(String uri, String localName, String qName,
187 Attributes attributes) {
188 currentBuilder = null;
189 if ("page".equals(qName)) {
190 titleBuilder = new StringBuilder();
192 // Start with "\n" to better match certain strings.
193 textBuilder = new StringBuilder("\n");
194 } else if ("title".equals(qName)) {
195 currentBuilder = titleBuilder;
196 } else if ("text".equals(qName)) {
197 currentBuilder = textBuilder;
202 public void characters(char[] ch, int start, int length) throws SAXException {
203 if (currentBuilder != null) {
204 currentBuilder.append(ch, start, length);
209 public void endElement(String uri, String localName, String qName)
210 throws SAXException {
211 currentBuilder = null;
212 if ("page".equals(qName)) {
218 public void parse(final File file) throws ParserConfigurationException,
219 SAXException, IOException {
220 final SAXParser parser = SAXParserFactoryImpl.newInstance().newSAXParser();
221 parser.parse(file, this);