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.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.parsers.SAXParser;
30 import javax.xml.parsers.SAXParserFactory;
32 import org.xml.sax.Attributes;
33 import org.xml.sax.SAXException;
35 import com.hughes.android.dictionary.parser.enwiktionary.EnWiktionaryLangs;
37 public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler {
39 private static final String FILE_TO_SPLIT = "data/inputs/enwiktionary-pages-articles.xml";
41 static class Section implements java.io.Serializable {
42 private static final long serialVersionUID = -7676549898325856822L;
48 public Section(final String title, final String heading, final String text) {
50 this.heading = heading;
53 //System.out.printf("TITLE:%s\nHEADING:%s\nTEXT:%s\n\n\n\n\n\n", title, heading, text);
57 static class Selector {
61 public Selector(final String filename, final String pattern) throws IOException {
62 this.out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
63 this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
67 final List<Selector> selectors = new ArrayList<Selector>();
68 StringBuilder titleBuilder;
69 StringBuilder textBuilder;
70 StringBuilder currentBuilder = null;
72 public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
73 final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
74 final WiktionarySplitter wiktionarySplitter = new WiktionarySplitter();
78 final List<Selector> selectors = wiktionarySplitter.selectors;
79 for (int i = 1; i < args.length; i += 2) {
80 final Selector selector = new Selector(args[i], args[i+1]);
81 selectors.add(selector);
84 if (selectors.isEmpty()) {
85 for (final Map.Entry<String, String> entry : EnWiktionaryLangs.isoCodeToWikiName.entrySet()) {
86 selectors.add(new Selector(String.format("data/inputs/enWikiSplit/%s.data", entry.getKey()), entry.getValue()));
91 parser.parse(new File(FILE_TO_SPLIT), wiktionarySplitter);
94 for (final Selector selector : selectors) {
99 static final Pattern headingStart = Pattern.compile("^(=+)[^=]+=+", Pattern.MULTILINE);
102 private void endPage() {
103 final String title = titleBuilder.toString();
104 if (++pageCount % 1000 == 0) {
105 System.out.println("endPage: " + title + ", count=" + pageCount);
108 String text = textBuilder.toString();
110 while (text.length() > 0) {
112 final Matcher startMatcher = headingStart.matcher(text);
113 if (!startMatcher.find()) {
116 text = text.substring(startMatcher.end());
118 final String heading = startMatcher.group();
119 for (final Selector selector : selectors) {
120 if (selector.pattern.matcher(heading).find()) {
123 final int depth = startMatcher.group(1).length();
124 final Pattern endPattern = Pattern.compile(String.format("^={1,%d}[^=]+=+", depth), Pattern.MULTILINE);
126 final Matcher endMatcher = endPattern.matcher(text);
128 if (endMatcher.find()) {
129 end = endMatcher.start();
134 final String sectionText = text.substring(0, end);
135 final Section section = new Section(title, heading, sectionText);
138 selector.out.writeUTF(section.title);
139 selector.out.writeUTF(section.heading);
140 final byte[] bytes = section.text.getBytes("UTF8");
141 selector.out.writeInt(bytes.length);
142 selector.out.write(bytes);
143 } catch (IOException e) {
144 throw new RuntimeException(e);
147 text = text.substring(end);
154 // -----------------------------------------------------------------------
157 public void startElement(String uri, String localName, String qName,
158 Attributes attributes) {
159 currentBuilder = null;
160 if ("page".equals(qName)) {
161 titleBuilder = new StringBuilder();
163 // Start with "\n" to better match certain strings.
164 textBuilder = new StringBuilder("\n");
165 } else if ("title".equals(qName)) {
166 currentBuilder = titleBuilder;
167 } else if ("text".equals(qName)) {
168 currentBuilder = textBuilder;
173 public void characters(char[] ch, int start, int length) throws SAXException {
174 if (currentBuilder != null) {
175 currentBuilder.append(ch, start, length);
180 public void endElement(String uri, String localName, String qName)
181 throws SAXException {
182 currentBuilder = null;
183 if ("page".equals(qName)) {
189 public void parse(final File file) throws ParserConfigurationException,
190 SAXException, IOException {
191 final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
192 parser.parse(file, this);