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