-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathmysql_hukugo.pm
281 lines (245 loc) · 5.66 KB
/
mysql_hukugo.pm
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# 複合名詞のリストを作製するためのロジック
package mysql_hukugo;
use strict;
use utf8;
use Benchmark;
use kh_jchar;
use mysql_exec;
use gui_errormsg;
sub search{
my $class = shift;
my %args = @_;
if (length($args{query}) == 0){
my @r = @{&get_majority()};
return \@r;
}
#$args{query} = Jcode->new($args{query},'sjis')->euc;
$args{query} =~ s/ / /g;
my @query = split(/ /, $args{query});
my $sql = '';
$sql .= "SELECT name, num\n";
$sql .= "FROM hukugo\n";
$sql .= "WHERE\n";
my $num = 0;
foreach my $i (@query){
next unless length($i);
if ($num){
$sql .= "\t$args{method} ";
}
if ($args{mode} eq 'p'){
$sql .= "\tname LIKE ".'"%'.$i.'%"';
}
elsif ($args{mode} eq 'c'){
$sql .= "\tname LIKE ".'"'.$i.'"';
}
elsif ($args{mode} eq 'z'){
$sql .= "\tname LIKE ".'"'.$i.'%"';
}
elsif ($args{mode} eq 'k'){
$sql .= "\tname LIKE ".'"%'.$i.'"';
}
else {
die('illegal parameter!');
}
$sql .= "\n";
++$num;
}
$sql .= "ORDER BY num DESC, ".$::project_obj->mysql_sort('name')."\n";
$sql .= "LIMIT 500\n";
#print Jcode->new($sql)->sjis, "\n";
my $h = mysql_exec->select($sql,1)->hundle;
my @r = ();
while (my $i = $h->fetch){
push @r, [$i->[0], $i->[1]];
}
return \@r;
}
# 検索文字列が指定されなかった場合
sub get_majority{
my $h = mysql_exec->select("
SELECT name, num
FROM hukugo
ORDER BY num DESC, ".$::project_obj->mysql_sort('name')."
LIMIT 500
",1)->hundle;
my @r = ();
while (my $i = $h->fetch){
push @r, [$i->[0], $i->[1]];
}
return \@r;
}
sub run_from_morpho{
my $class = shift;
my $target = $::project_obj->file_HukugoList;
my $t0 = new Benchmark;
# 形態素解析
#print "1. morpho\n";
my $source = $::config_obj->os_path( $::project_obj->file_target);
my $dist = $::config_obj->os_path( $::project_obj->file_m_target);
unlink($dist);
my $icode = kh_jchar->check_code2($source);
my $ocode;
if ($::config_obj->os eq 'win32'){
$ocode = 'cp932';
} else {
if (eval 'require Encode::EUCJPMS') {
$ocode = 'eucJP-ms';
} else {
$ocode = 'euc-jp';
}
}
open (MARKED,">:encoding($ocode)", $dist) or
gui_errormsg->open(
type => 'file',
thefile => $dist
);
open (SOURCE,"<:encoding($icode)", $source) or
gui_errormsg->open(
type => 'file',
thefile => $source
);
use Lingua::JA::Regular::Unicode qw(katakana_h2z);
while (<SOURCE>){
chomp;
my $text = katakana_h2z($_);
$text =~ s/ / /go;
$text =~ s/\\/¥/go;
$text =~ s/'/’/go;
$text =~ s/"/”/go;
print MARKED "$text\n";
}
close (SOURCE);
close (MARKED);
#kh_jchar->to_sjis($dist) if $::config_obj->os eq 'win32';
$::config_obj->use_hukugo(1);
$::config_obj->save;
kh_morpho->run;
$::config_obj->use_hukugo(0);
$::config_obj->save;
my $mcode = '';
if ($::config_obj->os eq 'win32') {
$mcode = 'sjis';
} else {
$mcode = 'ujis'
}
# 読み込み
#print "2. read\n";
mysql_exec->drop_table("rowdata_h");
mysql_exec->do("create table rowdata_h
(
hyoso varchar(255) not null,
yomi varchar(255) not null,
genkei varchar(255) not null,
hinshi varchar(255) not null,
katuyogata varchar(255) not null,
katuyo varchar(255) not null,
id int auto_increment primary key not null
)
",1);
my $thefile = "'".$::project_obj->file_MorphoOut."'";
$thefile =~ tr/\\/\//;
mysql_exec->do("LOAD DATA LOCAL INFILE $thefile INTO TABLE rowdata_h CHARACTER SET $mcode",1);
# 中間テーブル作製
mysql_exec->drop_table("rowdata_h2");
mysql_exec->do("
create table rowdata_h2 (
genkei varchar(255) not null
)
",1);
mysql_exec->do("
insert into rowdata_h2
select genkei
from rowdata_h
where
hinshi = \'複合名詞\'
",1);
# 書き出し
#print "4. print out\n";
mysql_exec->drop_table("hukugo");
mysql_exec->do("
CREATE TABLE hukugo (
name varchar(255),
num int
)
",1);
use Excel::Writer::XLSX;
my $workbook = Excel::Writer::XLSX->new($target);
my $worksheet = $workbook->add_worksheet('Sheet1',1);
$worksheet->hide_gridlines(1);
#my $font = '';
#if ($] > 5.008){
# $font = 'MS Pゴシック';
#} else {
# $font = 'MS PGothic';
#}
#$workbook->{_formats}->[15]->set_properties( # cannot do this with Excel::Writer::XLSX
# font => $font,
# size => 11,
# valign => 'vcenter',
# align => 'center',
#);
my $format_n = $workbook->add_format( # 数値
num_format => '0',
size => 11,
#font => $font,
align => 'right',
);
my $format_c = $workbook->add_format( # 文字列
#font => $font,
size => 11,
align => 'left',
num_format => '@'
);
# the first line
$worksheet->write_string(
0,
0,
'複合語',
$format_c
);
$worksheet->write_string(
0,
1,
'出現数',
$format_c
);
# body
my $oh = mysql_exec->select("
SELECT genkei, count(*) as hoge
FROM rowdata_h2
GROUP BY genkei
ORDER BY hoge DESC
",1)->hundle;
use Lingua::JA::Regular::Unicode qw(alnum_z2h);
my $row = 1;
while (my $i = $oh->fetch){
#print ".";
#my $tmp = Jcode->new($i->[0], 'euc')->tr('0-9','0-9');
my $tmp = alnum_z2h($i->[0]);
next if $tmp =~ /^(昭和)*(平成)*(\d+年)*(\d+月)*(\d+日)*(午前)*(午後)*(\d+時)*(\d+分)*(\d+秒)*$/o; # 日付・時刻
next if $tmp =~ /^\d+$/o; # 数値のみ
#print ",";
$worksheet->write_string(
$row,
0,
$i->[0],
$format_c
);
$worksheet->write_number(
$row,
1,
$i->[1],
$format_n
);
mysql_exec->do("
INSERT INTO hukugo (name, num) VALUES (\"$i->[0]\", $i->[1])
",1);
#print "!";
++$row;
}
$worksheet->freeze_panes(1, 0);
$workbook->close;
my $t1 = new Benchmark;
#print timestr(timediff($t1,$t0)),"\n";
}
1;