-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathimport_dict.php
More file actions
100 lines (92 loc) · 2.82 KB
/
import_dict.php
File metadata and controls
100 lines (92 loc) · 2.82 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/php
<?php
$dbname = 'vdict';
$dbuser = 'vdict';
$dbpass = '';
$dbhost = 'localhost';
$path = 'data/EV';
$db = mysql_connect($dbhost,$dbuser,$dbpass) or
die('Could not connect: ' . mysql_error());
mysql_select_db($dbname) or die('Could not select database');
mysql_query("SET NAMES 'UTF8'",$db);
$data = array (
'en_vi' => $path.'/anhviet109K.dict'
'vi_en' => $path.'/vietanh.dict'
);
foreach ($data as $table => $file){
Prepare_Table($table);
$h = fopen($file,'r');
$is_word = 0;
$word = ''; $phonetic = ''; $meanings = '';
while(!feof($h)){
$buf = fgets($h, 4096);
if(preg_match("/^@([^\/]+)\s+\/([^\/]+)\//",$buf,$matches)){
$word = $matches[1];
$phonetic = $matches[2];
$is_word = 1;
$meanings = '';
}elseif(preg_match("/^@(.+)\s*$/",$buf,$matches)){
$word = $matches[1]."---------------------------------";
$phonetic = '';
$is_word = 1;
$meanings = '';
}
if($is_word && preg_match("/^[\*\-=!]/",$buf)){
$meanings .= $buf;
}
if($is_word && preg_match("/^\s*$/",$buf)){
$is_word = 0;
if($word){
Import_Word($table,$word,$phonetic,$meanings);
//echo "$word\n";
}
$word = ''; $phonetic = '';
}elseif($is_word && preg_match("/^@(.+)\s*$/",$buf,$matches)){
$_word = $matches[1];
if($word && $meanings){
//echo "$word\n";
Import_Word($table,$word,$phonetic,$meanings);
}
$word = $_word; $phonetic = '';$meanings = '';
$is_word = 1;
}
}
fclose($h);
}
exit;
//---------------------------------------------------
function Import_Word($table,$word,$phonetic,$meanings){
global $db;
/*
echo "Word: $word\n";
echo "Phonetic: /$phonetic/\n";
echo "Meanings:\n$meanings\n";
echo "--------------------------\n";
*/
$query = 'INSERT INTO '.$table.' (d_word,d_phonetic,d_meanings) VALUES (\''.
sqlAddslashes($word).'\', \''.sqlAddslashes($phonetic).'\',\''.
sqlAddslashes($meanings).'\')';
if(mysql_query($query,$db)){
echo "$word\n";
}
}
function sqlAddslashes($str){
$str = str_replace('\\', '\\\\', $str);
$str = str_replace('\'', '\'\'', $str);
return $str;
}
function Prepare_Table($table){
global $db;
$query="CREATE TABLE IF NOT EXISTS $table (
d_id int(6) unsigned NOT NULL auto_increment,
d_word varchar(64) NOT NULL,
d_phonetic varchar(64) NOT NULL,
d_index int(6) NOT NULL,
d_meanings text NOT NULL,
PRIMARY KEY (d_id),
KEY windex (d_index)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
mysql_query($query,$db);
mysql_query("TRUNCATE TABLE $table",$db);
}
?>