forked from deton/AndroidTUTCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DicMaker.java
88 lines (69 loc) · 2.65 KB
/
DicMaker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.Properties;
import jdbm.RecordManager;
import jdbm.RecordManagerFactory;
import jdbm.btree.BTree;
import jdbm.helper.StringComparator;
import jdbm.helper.Tuple;
import jdbm.helper.TupleBrowser;
public class DicMaker {
static String BTREE_NAME = "skk_dict";
static public void main(String argv[]) {
RecordManager recman;
long recid;
Tuple tuple = new Tuple();
TupleBrowser browser;
BTree tree;
Properties props;
props = new Properties();
try {
// open database and setup an object cache
recman = RecordManagerFactory.createRecordManager("skk_dict_btree", props );
// try to reload an existing B+Tree
recid = recman.getNamedObject( BTREE_NAME );
if ( recid != 0 ) {
tree = BTree.load( recman, recid );
System.out.println( "Reloaded existing BTree with " + tree.size()
+ " famous people." );
} else {
// create a new B+Tree data structure and use a StringComparator
// to order the records based on people's name.
tree = BTree.createInstance( recman, new StringComparator() );
recman.setNamedObject( BTREE_NAME, tree.getRecid() );
System.out.println( "Created a new empty BTree" );
}
// insert people with their respective occupation
InputStreamReader fr = null;
BufferedReader br = null;
FileInputStream fis = new FileInputStream("mazedict-utf8-sorted.txt");
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
fr = new InputStreamReader(fis, decoder);
br = new BufferedReader(fr);
int c = 0;
for (String line = br.readLine(); line != null; line = br.readLine()) {
if (line.startsWith(";;")) continue;
int idx = line.indexOf(' ');
if (idx == -1) continue;
String key = line.substring(0, idx);
String value = line.substring(idx + 1, line.length());
tree.insert( key, value, true );
if (++c % 1000 == 0) {
recman.commit();
}
}
// make the data persistent in the database
recman.commit();
recman.close();
} catch ( Exception e ) {
throw new RuntimeException(e);
}
}
}