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.Arrays;
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;
31 import javax.xml.parsers.SAXParserFactory;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
36 public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler {
38 private static final String FILE_TO_SPLIT = "data/inputs/enwiktionary-20111224-pages-articles.xml";
40 static class Section implements java.io.Serializable {
41 private static final long serialVersionUID = -7676549898325856822L;
47 public Section(final String title, final String heading, final String text) {
49 this.heading = heading;
52 //System.out.printf("TITLE:%s\nHEADING:%s\nTEXT:%s\n\n\n\n\n\n", title, heading, text);
56 static class Selector {
60 public Selector(final String filename, final String pattern) throws IOException {
61 this.out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
62 this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
66 final List<Selector> selectors = new ArrayList<Selector>();
67 StringBuilder titleBuilder;
68 StringBuilder textBuilder;
69 StringBuilder currentBuilder = null;
71 public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
72 final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
73 final WiktionarySplitter wiktionarySplitter = new WiktionarySplitter();
77 final List<Selector> selectors = wiktionarySplitter.selectors;
78 for (int i = 1; i < args.length; i += 2) {
79 final Selector selector = new Selector(args[i], args[i+1]);
80 selectors.add(selector);
83 if (selectors.isEmpty()) {
84 for (final Map.Entry<String, String> entry : Language.isoCodeToWikiName.entrySet()) {
85 selectors.add(new Selector(String.format("data/inputs/enWikiSplit/%s.data", entry.getKey()), entry.getValue()));
90 parser.parse(new File(FILE_TO_SPLIT), wiktionarySplitter);
93 for (final Selector selector : selectors) {
98 static final Pattern headingStart = Pattern.compile("^(=+)[^=]+=+", Pattern.MULTILINE);
101 private void endPage() {
102 final String title = titleBuilder.toString();
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 : selectors) {
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 // -----------------------------------------------------------------------
156 public void startElement(String uri, String localName, String qName,
157 Attributes attributes) {
158 currentBuilder = null;
159 if ("page".equals(qName)) {
160 titleBuilder = new StringBuilder();
162 // Start with "\n" to better match certain strings.
163 textBuilder = new StringBuilder("\n");
164 } else if ("title".equals(qName)) {
165 currentBuilder = titleBuilder;
166 } else if ("text".equals(qName)) {
167 currentBuilder = textBuilder;
172 public void characters(char[] ch, int start, int length) throws SAXException {
173 if (currentBuilder != null) {
174 currentBuilder.append(ch, start, length);
179 public void endElement(String uri, String localName, String qName)
180 throws SAXException {
181 currentBuilder = null;
182 if ("page".equals(qName)) {
188 public void parse(final File file) throws ParserConfigurationException,
189 SAXException, IOException {
190 final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
191 parser.parse(file, this);