]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/WiktionarySplitter.java
More languages, simpler splitter.
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / WiktionarySplitter.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary.engine;
16
17 import java.io.BufferedOutputStream;
18 import java.io.DataOutputStream;
19 import java.io.File;
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;
25 import java.util.Map;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.parsers.SAXParser;
31 import javax.xml.parsers.SAXParserFactory;
32
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
35
36 public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler {
37   
38   private static final String FILE_TO_SPLIT = "data/inputs/enwiktionary-20111224-pages-articles.xml";
39   
40   static class Section implements java.io.Serializable {
41     private static final long serialVersionUID = -7676549898325856822L;
42
43     final String title;
44     final String heading;
45     final String text;
46     
47     public Section(final String title, final String heading, final String text) {
48       this.title = title;
49       this.heading = heading;
50       this.text = text;
51       
52       //System.out.printf("TITLE:%s\nHEADING:%s\nTEXT:%s\n\n\n\n\n\n", title, heading, text);
53     }
54   }
55   
56   static class Selector {
57     DataOutputStream out;
58     Pattern pattern;
59     
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);
63     }
64   }
65
66   final List<Selector> selectors = new ArrayList<Selector>();
67   StringBuilder titleBuilder;
68   StringBuilder textBuilder;
69   StringBuilder currentBuilder = null;
70
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();
74     
75     // Configure things.
76     
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);
81     }
82
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()));
86       }
87     }
88     
89     // Do it.
90     parser.parse(new File(FILE_TO_SPLIT), wiktionarySplitter);
91     
92     // Shutdown.
93     for (final Selector selector : selectors) {
94       selector.out.close();
95     }
96   }
97
98   static final Pattern headingStart = Pattern.compile("^(=+)[^=]+=+", Pattern.MULTILINE);
99   
100   int pageCount = 0;
101   private void endPage() {
102     final String title = titleBuilder.toString();
103     if (++pageCount % 1000 == 0) {
104       System.out.println("endPage: " + title + ", count=" + pageCount);
105     }
106     
107     String text = textBuilder.toString();
108     
109     while (text.length() > 0) {
110       // Find start.
111       final Matcher startMatcher = headingStart.matcher(text);
112       if (!startMatcher.find()) {
113         return;
114       }
115       text = text.substring(startMatcher.end());
116       
117       final String heading = startMatcher.group();
118       for (final Selector selector : selectors) {
119         if (selector.pattern.matcher(heading).find()) {
120           
121           // Find end.
122           final int depth = startMatcher.group(1).length();
123           final Pattern endPattern = Pattern.compile(String.format("^={1,%d}[^=]+=+", depth), Pattern.MULTILINE);
124           
125           final Matcher endMatcher = endPattern.matcher(text);
126           final int end;
127           if (endMatcher.find()) {
128             end = endMatcher.start();
129           } else {
130             end = text.length();
131           }
132           
133           final String sectionText = text.substring(0, end);
134           final Section section = new Section(title, heading, sectionText);
135           
136           try {
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);
144           }
145           
146           text = text.substring(end);
147         }
148       }
149     }
150     
151   }
152
153   // -----------------------------------------------------------------------
154   
155     @Override
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();
161         
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;
168       }
169     }
170
171     @Override
172     public void characters(char[] ch, int start, int length) throws SAXException {
173       if (currentBuilder != null) {
174         currentBuilder.append(ch, start, length);
175       }
176     }
177
178     @Override
179     public void endElement(String uri, String localName, String qName)
180         throws SAXException {
181       currentBuilder = null;
182       if ("page".equals(qName)) {
183         endPage();
184       }
185     }
186     
187
188     public void parse(final File file) throws ParserConfigurationException,
189         SAXException, IOException {
190       final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
191       parser.parse(file, this);
192     }
193     
194 }