]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/WiktionarySplitter.java
Apache license.
[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.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 public class WiktionarySplitter extends org.xml.sax.helpers.DefaultHandler {
36   
37   static class Section implements java.io.Serializable {
38     private static final long serialVersionUID = -7676549898325856822L;
39
40     final String title;
41     final String heading;
42     final String text;
43     
44     public Section(final String title, final String heading, final String text) {
45       this.title = title;
46       this.heading = heading;
47       this.text = text;
48       
49       //System.out.printf("TITLE:%s\nHEADING:%s\nTEXT:%s\n\n\n\n\n\n", title, heading, text);
50     }
51   }
52   
53   static class Selector {
54     DataOutputStream out;
55     Pattern pattern;
56     
57     public Selector(final String filename, final String pattern) throws IOException {
58       this.out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
59       this.pattern = Pattern.compile(pattern);
60     }
61   }
62
63   final List<Selector> selectors = new ArrayList<Selector>();
64   StringBuilder titleBuilder;
65   StringBuilder textBuilder;
66   StringBuilder currentBuilder = null;
67
68   public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
69     final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
70     final WiktionarySplitter wiktionarySplitter = new WiktionarySplitter();
71     
72     // Configure things.
73     final File file = new File(args[0]);
74     final List<Selector> selectors = wiktionarySplitter.selectors;
75     for (int i = 1; i < args.length; i += 2) {
76       final Selector selector = new Selector(args[i], args[i+1]);
77       selectors.add(selector);
78     }
79
80     if (selectors.isEmpty()) {
81       selectors.addAll(Arrays.asList(
82           new Selector("wikiSplit/arabic.data", ".*[Ar]rabic.*"),
83           new Selector("wikiSplit/croation.data", ".*[Cc]roation.*"),
84           new Selector("wikiSplit/czech.data", ".*[Cc]zech.*"),
85           new Selector("wikiSplit/mandarin.data", ".*[Mm]andarin|[Cc]hinese.*"),
86           new Selector("wikiSplit/dutch.data", ".*[Du]utch.*"),
87           new Selector("wikiSplit/english.data", ".*[Ee]nglish.*"),
88           new Selector("wikiSplit/french.data", ".*[Ff]rench.*"),
89           new Selector("wikiSplit/german.data", ".*[Gg]erman.*"),
90           new Selector("wikiSplit/greek.data", ".*[Gg]reek.*"),
91           new Selector("wikiSplit/hindi.data", ".*[Hh]indi.*"),
92           new Selector("wikiSplit/italian.data", ".*[Ii]talian.*"),
93           new Selector("wikiSplit/japanese.data", ".*[Jj]apanese.*"),
94           new Selector("wikiSplit/korean.data", ".*[Kk]orean.*"),
95           new Selector("wikiSplit/persian.data", ".*[Pp]ersian.*"),
96           new Selector("wikiSplit/portuguese.data", ".*[Pp]ortuguese.*"),
97           new Selector("wikiSplit/romanian.data", ".*[Rr]omanian.*"),
98           new Selector("wikiSplit/russian.data", ".*[Rr]ussian.*"),
99           new Selector("wikiSplit/spanish.data", ".*[Ss]panish.*"),
100           new Selector("wikiSplit/swedish.data", ".*[Ss]wedish.*"),
101           new Selector("wikiSplit/thai.data", ".*[Tt]hai.*"),
102           new Selector("wikiSplit/vietnamese.data", ".*[Vv]ietnamese.*")
103           ));
104     }
105     
106     // Do it.
107     parser.parse(file, wiktionarySplitter);
108     
109     // Shutdown.
110     for (final Selector selector : selectors) {
111       selector.out.close();
112     }
113   }
114
115   static final Pattern headingStart = Pattern.compile("^(=+)[^=]+=+", Pattern.MULTILINE);
116   
117   private void endPage() {
118     final String title = titleBuilder.toString();
119     System.out.println("endPage: " + title);
120     
121     String text = textBuilder.toString();
122     
123     while (text.length() > 0) {
124       // Find start.
125       final Matcher startMatcher = headingStart.matcher(text);
126       if (!startMatcher.find()) {
127         return;
128       }
129       text = text.substring(startMatcher.end());
130       
131       final String heading = startMatcher.group();
132       for (final Selector selector : selectors) {
133         if (selector.pattern.matcher(heading).find()) {
134           
135           // Find end.
136           final int depth = startMatcher.group(1).length();
137           final Pattern endPattern = Pattern.compile(String.format("^={1,%d}[^=]+=+", depth), Pattern.MULTILINE);
138           
139           final Matcher endMatcher = endPattern.matcher(text);
140           final int end;
141           if (endMatcher.find()) {
142             end = endMatcher.start();
143           } else {
144             end = text.length();
145           }
146           
147           final String sectionText = text.substring(0, end);
148           final Section section = new Section(title, heading, sectionText);
149           
150           try {
151             selector.out.writeUTF(section.title);
152             selector.out.writeUTF(section.heading);
153             final byte[] bytes = section.text.getBytes("UTF8");
154             selector.out.writeInt(bytes.length);
155             selector.out.write(bytes);
156           } catch (IOException e) {
157             throw new RuntimeException(e);
158           }
159           
160           text = text.substring(end);
161         }
162       }
163     }
164     
165   }
166
167   // -----------------------------------------------------------------------
168   
169     @Override
170     public void startElement(String uri, String localName, String qName,
171         Attributes attributes) {
172       currentBuilder = null;
173       if ("page".equals(qName)) {
174         titleBuilder = new StringBuilder();
175         
176         // Start with "\n" to better match certain strings.
177         textBuilder = new StringBuilder("\n");
178       } else if ("title".equals(qName)) {
179         currentBuilder = titleBuilder;
180       } else if ("text".equals(qName)) {
181         currentBuilder = textBuilder;
182       }
183     }
184
185     @Override
186     public void characters(char[] ch, int start, int length) throws SAXException {
187       if (currentBuilder != null) {
188         currentBuilder.append(ch, start, length);
189       }
190     }
191
192     @Override
193     public void endElement(String uri, String localName, String qName)
194         throws SAXException {
195       currentBuilder = null;
196       if ("page".equals(qName)) {
197         endPage();
198       }
199     }
200     
201
202     public void parse(final File file) throws ParserConfigurationException,
203         SAXException, IOException {
204       final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
205       parser.parse(file, this);
206     }
207     
208 }