Add converter
@@ -1,3 +1,21 @@ | ||
1 | +/* | |
2 | + * Copyright 2009 Junpei Kawamoto | |
3 | + * | |
4 | + * This file is part of Japanese WordNet wrapper for Java (Wnj2). | |
5 | + * Wnj2 is free software: you can redistribute it and/or modify | |
6 | + * it under the terms of the GNU Lesser General Public License as published by | |
7 | + * the Free Software Foundation, either version 3 of the License, or | |
8 | + * (at your option) any later version. | |
9 | + * | |
10 | + * Wnj2 is distributed in the hope that it will be useful, | |
11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | + * GNU Lesser General Public License for more details. | |
14 | + * | |
15 | + * You should have received a copy of the GNU Lesser General Public License | |
16 | + * along with Wnj2. If not, see <http://www.gnu.org/licenses/>. | |
17 | + */ | |
18 | + | |
1 | 19 | package org.wnj2.sample; |
2 | 20 | |
3 | 21 | import java.io.File; |
@@ -0,0 +1,351 @@ | ||
1 | +/* | |
2 | + * Copyright 2009 Junpei Kawamoto | |
3 | + * | |
4 | + * This file is part of Japanese WordNet wrapper for Java (Wnj2). | |
5 | + * Wnj2 is free software: you can redistribute it and/or modify | |
6 | + * it under the terms of the GNU Lesser General Public License as published by | |
7 | + * the Free Software Foundation, either version 3 of the License, or | |
8 | + * (at your option) any later version. | |
9 | + * | |
10 | + * Wnj2 is distributed in the hope that it will be useful, | |
11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | + * GNU Lesser General Public License for more details. | |
14 | + * | |
15 | + * You should have received a copy of the GNU Lesser General Public License | |
16 | + * along with Wnj2. If not, see <http://www.gnu.org/licenses/>. | |
17 | + */ | |
18 | + | |
19 | +package org.wnj2.converter; | |
20 | + | |
21 | +import java.sql.Connection; | |
22 | +import java.sql.DriverManager; | |
23 | +import java.sql.ResultSet; | |
24 | +import java.sql.SQLException; | |
25 | +import java.sql.Statement; | |
26 | + | |
27 | +public class Converter { | |
28 | + | |
29 | + /** | |
30 | + * @param args | |
31 | + */ | |
32 | + public static void main(String[] args) { | |
33 | + | |
34 | + String srcUri = "jdbc:sqlite:./data/wnjpn-0.9.db"; | |
35 | + String destUri = ""; | |
36 | + String destUser = ""; | |
37 | + String destPass = ""; | |
38 | + | |
39 | + try { | |
40 | + | |
41 | + Class.forName("org.sqlite.JDBC"); | |
42 | + Class.forName("org.gjt.mm.mysql.Driver"); | |
43 | + | |
44 | + } catch (ClassNotFoundException e) { | |
45 | + | |
46 | + e.printStackTrace(); | |
47 | + System.exit(1); | |
48 | + | |
49 | + } | |
50 | + | |
51 | + try { | |
52 | + | |
53 | + final Connection srcCon = DriverManager.getConnection(srcUri); | |
54 | + final Statement srcStat = srcCon.createStatement(); | |
55 | + | |
56 | + final Connection destCon = DriverManager.getConnection(destUri, destUser, destPass); | |
57 | + final Statement destStat = destCon.createStatement(); | |
58 | + | |
59 | + | |
60 | + System.out.println("Copying LINK_DEF table"); | |
61 | + try{ | |
62 | + | |
63 | + // 既にテーブルがある場合は削除 | |
64 | + destStat.executeUpdate("drop table if exists LINK_DEF;"); | |
65 | + | |
66 | + // 新たにテーブルを作成 | |
67 | + destStat.executeUpdate("create table LINK_DEF(link varchar(5) primary key, lang char(3) not null, def text not null);"); | |
68 | + | |
69 | + // 元のデータベースからコピー | |
70 | + final ResultSet rs = srcStat.executeQuery("select * from LINK_DEF"); | |
71 | + try{ | |
72 | + | |
73 | + while(rs.next()){ | |
74 | + | |
75 | + // read the result set | |
76 | + final String link = rs.getString("link"); | |
77 | + final String lang = rs.getString("lang"); | |
78 | + final String def = rs.getString("def"); | |
79 | + | |
80 | + if(link != null && lang != null && def != null){ | |
81 | + | |
82 | + destStat.executeUpdate(String.format("insert into LINK_DEF values('%s', '%s', '%s');", link, lang, def)); | |
83 | + | |
84 | + } | |
85 | + | |
86 | + } | |
87 | + | |
88 | + }finally{ | |
89 | + | |
90 | + rs.close(); | |
91 | + | |
92 | + } | |
93 | + | |
94 | + }catch(final SQLException e){ | |
95 | + | |
96 | + e.printStackTrace(); | |
97 | + System.exit(1); | |
98 | + | |
99 | + } | |
100 | + | |
101 | + | |
102 | + System.out.println("Copying POS_DEF table"); | |
103 | + try{ | |
104 | + | |
105 | + // 既にテーブルがある場合は削除 | |
106 | + destStat.executeUpdate("drop table if exists POS_DEF;"); | |
107 | + | |
108 | + // 新たにテーブルを作成 | |
109 | + destStat.executeUpdate("create table POS_DEF(pos char(1), lang char(3), def text not null, primary key(pos, lang));"); | |
110 | + | |
111 | + // 元のデータベースからコピー | |
112 | + final ResultSet rs = srcStat.executeQuery("select * from POS_DEF"); | |
113 | + try{ | |
114 | + | |
115 | + while(rs.next()){ | |
116 | + | |
117 | + // read the result set | |
118 | + final String pos = rs.getString("pos"); | |
119 | + final String lang = rs.getString("lang"); | |
120 | + final String def = rs.getString("def"); | |
121 | + | |
122 | + if(pos != null && lang != null && def != null){ | |
123 | + | |
124 | + destStat.executeUpdate(String.format("insert into POS_DEF values('%s', '%s', '%s');", pos, lang, def)); | |
125 | + | |
126 | + } | |
127 | + | |
128 | + } | |
129 | + | |
130 | + }finally{ | |
131 | + | |
132 | + rs.close(); | |
133 | + | |
134 | + } | |
135 | + | |
136 | + }catch(final SQLException e){ | |
137 | + | |
138 | + e.printStackTrace(); | |
139 | + System.exit(1); | |
140 | + | |
141 | + } | |
142 | + | |
143 | + System.out.println("Copying SENSE table"); | |
144 | + try{ | |
145 | + | |
146 | + // 既にテーブルがある場合は削除 | |
147 | + destStat.executeUpdate(String.format("drop table if exists SENSE;")); | |
148 | + | |
149 | + // 新たにテーブルを作成 | |
150 | + destStat.executeUpdate("create table SENSE(synset varchar(12), wordid integer unsigned, lang char(3), rank integer unsigned, lexid integer unsigned, freq integer unsigned, src varchar(10), primary key(synset, wordid));"); | |
151 | + | |
152 | + // 元のデータベースからコピー | |
153 | + final ResultSet rs = srcStat.executeQuery("select * from SENSE"); | |
154 | + try{ | |
155 | + | |
156 | + while(rs.next()){ | |
157 | + | |
158 | + // read the result set | |
159 | + final String synset = rs.getString("synset"); | |
160 | + final int wordid = rs.getInt("wordid"); | |
161 | + final String lang = rs.getString("lang"); | |
162 | + final int rank = rs.getInt("rank"); | |
163 | + final int lexid = rs.getInt("lexid"); | |
164 | + final int freq = rs.getInt("freq"); | |
165 | + final String src = rs.getString("src"); | |
166 | + | |
167 | + destStat.executeUpdate(String.format("insert into sense values('%s', %d, '%s', %d, %d, %d, '%s');", synset, wordid, lang, rank, lexid, freq, src)); | |
168 | + | |
169 | + } | |
170 | + | |
171 | + }finally{ | |
172 | + | |
173 | + rs.close(); | |
174 | + | |
175 | + } | |
176 | + | |
177 | + }catch(final SQLException e){ | |
178 | + | |
179 | + e.printStackTrace(); | |
180 | + System.exit(1); | |
181 | + | |
182 | + } | |
183 | + | |
184 | + System.out.println("Copying SYNLINK table"); | |
185 | + try{ | |
186 | + | |
187 | + // 既にテーブルがある場合は削除 | |
188 | + destStat.executeUpdate(String.format("drop table if exists SYNLINK;")); | |
189 | + | |
190 | + // 新たにテーブルを作成 | |
191 | + destStat.executeUpdate("create table SYNLINK(synset1 varchar(12), synset2 varchar(12), link varchar(5), src varchar(10), primary key(synset1, synset2, link));"); | |
192 | + | |
193 | + // 元のデータベースからコピー | |
194 | + final ResultSet rs = srcStat.executeQuery("select * from SYNLINK"); | |
195 | + try{ | |
196 | + while(rs.next()){ | |
197 | + | |
198 | + // read the result set | |
199 | + final String synset1 = rs.getString("synset1"); | |
200 | + final String synset2 = rs.getString("synset2"); | |
201 | + final String link = rs.getString("link"); | |
202 | + final String src = rs.getString("src"); | |
203 | + | |
204 | + destStat.executeUpdate(String.format("INSERT INTO SYNLINK VALUES('%s', '%s', '%s', '%s');", synset1, synset2, link, src)); | |
205 | + | |
206 | + } | |
207 | + | |
208 | + }finally{ | |
209 | + | |
210 | + rs.close(); | |
211 | + | |
212 | + } | |
213 | + | |
214 | + }catch(final SQLException e){ | |
215 | + | |
216 | + e.printStackTrace(); | |
217 | + System.exit(1); | |
218 | + | |
219 | + } | |
220 | + | |
221 | + System.out.println("Copying SYNSET table"); | |
222 | + try{ | |
223 | + | |
224 | + // 既にテーブルがある場合は削除 | |
225 | + destStat.executeUpdate(String.format("drop table if exists SYNSET;")); | |
226 | + | |
227 | + // 新たにテーブルを作成 | |
228 | + destStat.executeUpdate("create table SYNSET(synset varchar(12) primary key, pos char(1), name text, src varchar(10));"); | |
229 | + | |
230 | + // 元のデータベースからコピー | |
231 | + final ResultSet rs = srcStat.executeQuery("select * from SYNSET"); | |
232 | + try{ | |
233 | + | |
234 | + while(rs.next()){ | |
235 | + | |
236 | + // read the result set | |
237 | + final String synset = rs.getString("synset"); | |
238 | + final String pos = rs.getString("pos"); | |
239 | + final String name = rs.getString("name"); | |
240 | + final String src = rs.getString("src"); | |
241 | + | |
242 | + destStat.executeUpdate(String.format("INSERT INTO SYNSET VALUES('%s', '%s', \"%s\", '%s');", synset, pos, name, src)); | |
243 | + | |
244 | + } | |
245 | + | |
246 | + }finally{ | |
247 | + | |
248 | + rs.close(); | |
249 | + | |
250 | + } | |
251 | + | |
252 | + }catch(final SQLException e){ | |
253 | + | |
254 | + e.printStackTrace(); | |
255 | + System.exit(1); | |
256 | + | |
257 | + } | |
258 | + | |
259 | + System.out.println("Copying SYNSET_DEF table"); | |
260 | + try{ | |
261 | + | |
262 | + // 既にテーブルがある場合は削除 | |
263 | + destStat.executeUpdate(String.format("drop table if exists SYNSET_DEF;")); | |
264 | + | |
265 | + // 新たにテーブルを作成 | |
266 | + destStat.executeUpdate("create table SYNSET_DEF(synset varchar(12) primary key, lang char(3), def text, sid integer unsigned);"); | |
267 | + | |
268 | + // 元のデータベースからコピー | |
269 | + final ResultSet rs = srcStat.executeQuery("select * from SYNSET_DEF"); | |
270 | + try{ | |
271 | + | |
272 | + while(rs.next()){ | |
273 | + | |
274 | + // read the result set | |
275 | + final String synset = rs.getString("synset"); | |
276 | + final String lang = rs.getString("lang"); | |
277 | + final String def = rs.getString("def"); | |
278 | + final String sid = rs.getString("sid"); | |
279 | + | |
280 | + final String sql = String.format("INSERT INTO SYNSET_DEF VALUES('%s', '%s', \"%s\", '%s');", synset, lang, def.replace("\"", "\\\""), sid); | |
281 | + destStat.executeUpdate(sql); | |
282 | + | |
283 | + } | |
284 | + | |
285 | + }finally{ | |
286 | + | |
287 | + rs.close(); | |
288 | + | |
289 | + } | |
290 | + | |
291 | + }catch(final SQLException e){ | |
292 | + | |
293 | + e.printStackTrace(); | |
294 | + System.exit(1); | |
295 | + | |
296 | + } | |
297 | + | |
298 | + System.out.println("Copying WORD table"); | |
299 | + try{ | |
300 | + | |
301 | + // 既にテーブルがある場合は削除 | |
302 | + destStat.executeUpdate(String.format("drop table if exists WORD;")); | |
303 | + | |
304 | + // 新たにテーブルを作成 | |
305 | + destStat.executeUpdate("create table WORD(wordid integer primary key, lang char(3), lemma text, pron text, pos char(1));"); | |
306 | + | |
307 | + // 元のデータベースからコピー | |
308 | + final ResultSet rs = srcStat.executeQuery("select * from WORD"); | |
309 | + try{ | |
310 | + | |
311 | + while(rs.next()){ | |
312 | + | |
313 | + // read the result set | |
314 | + final int wordid = rs.getInt("wordid"); | |
315 | + final String lang = rs.getString("lang"); | |
316 | + final String lemma = rs.getString("lemma"); | |
317 | + final String pron = rs.getString("pron"); | |
318 | + final String pos = rs.getString("pos"); | |
319 | + | |
320 | + destStat.executeUpdate(String.format("INSERT INTO WORD VALUES(%d, '%s', \"%s\", \"%s\", '%s');", wordid, lang, lemma.replace("\"", "\\\""), pron, pos)); | |
321 | + | |
322 | + } | |
323 | + | |
324 | + }finally{ | |
325 | + | |
326 | + rs.close(); | |
327 | + | |
328 | + } | |
329 | + | |
330 | + }catch(final SQLException e){ | |
331 | + | |
332 | + e.printStackTrace(); | |
333 | + System.exit(1); | |
334 | + | |
335 | + } | |
336 | + | |
337 | + destStat.close(); | |
338 | + destCon.close(); | |
339 | + srcStat.close(); | |
340 | + srcCon.close(); | |
341 | + | |
342 | + } catch (SQLException e) { | |
343 | + | |
344 | + e.printStackTrace(); | |
345 | + | |
346 | + } | |
347 | + | |
348 | + } | |
349 | + | |
350 | +} | |
351 | + |