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 public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler {
37 private static final String FILE_TO_SPLIT = "data/inputs/enwiktionary-20111224-pages-articles.xml";
39 static class Section implements java.io.Serializable {
40 private static final long serialVersionUID = -7676549898325856822L;
46 public Section(final String title, final String heading, final String text) {
48 this.heading = heading;
51 //System.out.printf("TITLE:%s\nHEADING:%s\nTEXT:%s\n\n\n\n\n\n", title, heading, text);
55 static class Selector {
59 public Selector(final String filename, final String pattern) throws IOException {
60 this.out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
61 this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
65 final List<Selector> selectors = new ArrayList<Selector>();
66 StringBuilder titleBuilder;
67 StringBuilder textBuilder;
68 StringBuilder currentBuilder = null;
70 public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
71 final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
72 final WiktionarySplitter wiktionarySplitter = new WiktionarySplitter();
76 final List<Selector> selectors = wiktionarySplitter.selectors;
77 for (int i = 1; i < args.length; i += 2) {
78 final Selector selector = new Selector(args[i], args[i+1]);
79 selectors.add(selector);
82 if (selectors.isEmpty()) {
83 for (final Map.Entry<String, String> entry : Language.isoCodeToWikiName.entrySet()) {
84 selectors.add(new Selector(String.format("data/inputs/enWikiSplit/%s.data", entry.getKey()), entry.getValue()));
89 parser.parse(new File(FILE_TO_SPLIT), wiktionarySplitter);
92 for (final Selector selector : selectors) {
97 static final Pattern headingStart = Pattern.compile("^(=+)[^=]+=+", Pattern.MULTILINE);
100 private void endPage() {
101 final String title = titleBuilder.toString();
102 if (++pageCount % 1000 == 0) {
103 System.out.println("endPage: " + title + ", count=" + pageCount);
106 String text = textBuilder.toString();
108 while (text.length() > 0) {
110 final Matcher startMatcher = headingStart.matcher(text);
111 if (!startMatcher.find()) {
114 text = text.substring(startMatcher.end());
116 final String heading = startMatcher.group();
117 for (final Selector selector : selectors) {
118 if (selector.pattern.matcher(heading).find()) {
121 final int depth = startMatcher.group(1).length();
122 final Pattern endPattern = Pattern.compile(String.format("^={1,%d}[^=]+=+", depth), Pattern.MULTILINE);
124 final Matcher endMatcher = endPattern.matcher(text);
126 if (endMatcher.find()) {
127 end = endMatcher.start();
132 final String sectionText = text.substring(0, end);
133 final Section section = new Section(title, heading, sectionText);
136 selector.out.writeUTF(section.title);
137 selector.out.writeUTF(section.heading);
138 final byte[] bytes = section.text.getBytes("UTF8");
139 selector.out.writeInt(bytes.length);
140 selector.out.write(bytes);
141 } catch (IOException e) {
142 throw new RuntimeException(e);
145 text = text.substring(end);
152 // -----------------------------------------------------------------------
155 public void startElement(String uri, String localName, String qName,
156 Attributes attributes) {
157 currentBuilder = null;
158 if ("page".equals(qName)) {
159 titleBuilder = new StringBuilder();
161 // Start with "\n" to better match certain strings.
162 textBuilder = new StringBuilder("\n");
163 } else if ("title".equals(qName)) {
164 currentBuilder = titleBuilder;
165 } else if ("text".equals(qName)) {
166 currentBuilder = textBuilder;
171 public void characters(char[] ch, int start, int length) throws SAXException {
172 if (currentBuilder != null) {
173 currentBuilder.append(ch, start, length);
178 public void endElement(String uri, String localName, String qName)
179 throws SAXException {
180 currentBuilder = null;
181 if ("page".equals(qName)) {
187 public void parse(final File file) throws ParserConfigurationException,
188 SAXException, IOException {
189 final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
190 parser.parse(file, this);