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