-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathmysql_ready.pm
2204 lines (1988 loc) · 53 KB
/
mysql_ready.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mysql_ready;
use strict;
use utf8;
use Jcode;
use Benchmark;
use kh_project;
use kh_jchar;
use kh_dictio;
use kh_mailif;
use mysql_exec;
use mysql_ready::check;
use mysql_ready::doclength;
use mysql_ready::heap;
use mysql_ready::df;
use mysql_ready::dump;
use mysql_ready::fc;
my $rows_per_once = 30000; # MySQLからPerlに一度に読み込む行数
my $data_per_1ins = 200; # 一度にINSERTする値の数
sub first{
my $class = shift;
my $reload = shift;
my $self;
$self->{temp} = 'temp';
bless $self, $class;
$::config_obj->in_preprocessing(1);
# Reload Excel/CSV
if ($reload) {
&reload_source;
my $source = $::project_obj->status_source_file;
if ( $::project_obj->status_from_table ){
&reload_from_spreadsheet;
} elsif (
$source =~ /(.+)\.docx$/i
|| $source =~ /(.+)\.doc$/i
|| $source =~ /(.+)\.rtf$/i
|| $source =~ /(.+)\.odt$/i
) {
&reload_from_docx;
}
}
if ($::config_obj->use_heap) {
# $self->{type_heap} = ' TYPE=HEAP ';
$self->{type_heap} = ''; # 安全第一 / heapではなくmyisamで
} else {
$self->{type_heap} = '';
}
my $ta0 = new Benchmark;
$::project_obj->read_hinshi_setting;
kh_dictio->readin->mark;
kh_morpho->run or return 0;
my $ta1 = new Benchmark;
print "Morpho1\t",timestr(timediff($ta1,$ta0)),"\n";
my $t0 = new Benchmark;
$self->readin; # rowdata
my $t1 = new Benchmark;
print "Read\t",timestr(timediff($t1,$t0)),"\n";
$self->reform; # hinshi, genkei, katuyo, hyoso, khhinshi
my $t15 = new Benchmark;
print "Format\t",timestr(timediff($t15,$t1)),"\n";
kh_dictio->readin->save;
$self->hyosobun;
$self->tag_fix; # hyoso, genkei
kh_dictio->readin->save;
my $t2 = new Benchmark;
print "Strat1\t",timestr(timediff($t2,$t15)),"\n";
$self->tanis;
my $t4 = new Benchmark;
print "Strat2\t",timestr(timediff($t4,$t2)),"\n";
$self->rowtxt;
my $t3 = new Benchmark;
print "RawTXT\t",timestr(timediff($t3,$t4)),"\n";
mysql_ready::df->calc($self);
my $t5 = new Benchmark;
print "df\t",timestr(timediff($t5,$t3)),"\n";
mysql_ready::fc->calc_by_db;
my $t6 = new Benchmark;
print "fc\t",timestr(timediff($t6,$t5)),"\n";
mysql_ready::check->do;
my $t7 = new Benchmark;
print "Check\t",timestr(timediff($t7,$t6)),"\n";
$self->fix_katuyo;
my $t8 = new Benchmark;
print "fix_morpho\t",timestr(timediff($t8,$t7)),"\n";
$self->fix_michigo;
my $t9 = new Benchmark;
print "fix_unknown\t",timestr(timediff($t9,$t8)),"\n";
&zero_length_headings;
my $t10 = new Benchmark;
print "fix_headings\t",timestr(timediff($t10,$t9)),"\n";
&make_cache;
my $t11 = new Benchmark;
print "make_cache\t",timestr(timediff($t11,$t10)),"\n";
my $t12;
if ( $::config_obj->unify_words_with_same_lemma ){
&unify_words;
$t12 = new Benchmark;
print "unify_words\t",timestr(timediff($t12,$t11)),"\n";
} else {
$t12 = new Benchmark;
}
# Korean Jamo fix
my $t13;
if ($::project_obj->morpho_analyzer_lang eq 'kr' ) {
&fix_hangul;
$t13 = new Benchmark;
print "fix_hangul\t",timestr(timediff($t13,$t12)),"\n";
} else {
$t13 = new Benchmark;
}
# データベース内の一時テーブルをクリア
mysql_exec->clear_tmp_tables;
mysql_ready::heap->clear_heap;
mysql_exec->drop_table("hyosobun_t");
#mysql_exec->drop_table("hghi");
my $t14 = new Benchmark;
print "clear_tmp\t",timestr(timediff($t14,$t13)),"\n";
mysql_exec->flush;
print "Morpho File: ".$::config_obj->uni_path( $::project_obj->file_MorphoOut )."\n";
kh_mailif->success;
$::config_obj->in_preprocessing(0);
return 1;
}
sub unify_words{
# Words with the same basic form are regarded as identical even if POS is different
# list up existing units
my @avail = ();
foreach my $tani ('bun','dan','h1','h2','h3','h4','h5'){
if ( mysql_exec->table_exists($tani) ){
push @avail, $tani;
}
}
my $h = mysql_exec->select("
select genkei.name, sum(num)
from genkei, hselection
where
genkei.khhinshi_id = hselection.khhinshi_id
AND genkei.nouse = 0
AND hselection.ifuse = 1
group by genkei.name having count(genkei.name) > 1
order by sum(num) DESC
",1)->hundle;
my $config;
while (my $i = $h->fetch) {
$config->{$i->[0]} = [$i->[0]];
}
# get required info
my ($hyoso, $genkei);
foreach my $i (keys %{$config}){
# parent
my $quoted = mysql_exec->quote($i);
my $hdl7 = mysql_exec->select("
SELECT genkei.id, genkei.num
FROM genkei
WHERE genkei.name = $quoted
ORDER BY num DESC
LIMIT 1
",1)->hundle->fetch;
next unless $hdl7;
$genkei->{$i}{id} = $hdl7->[0];
$genkei->{$i}{num} = $hdl7->[1];
# hyoso of children
my $sql = "
SELECT hyoso.id
FROM hyoso, genkei, hselection
WHERE
hyoso.genkei_id = genkei.id
AND genkei.khhinshi_id = hselection.khhinshi_id
AND genkei.nouse = 0
AND hselection.ifuse = 1
AND
";
my $sql_w;
my $n = 0;
foreach my $h (@{$config->{$i}}){
$sql_w .= " OR " if $n;
my $quoted1 = mysql_exec->quote($h);
$sql_w .= "genkei.name = $quoted1";
++$n;
}
$sql = $sql .= "( $sql_w )";
$sql .= " AND NOT genkei_id = $genkei->{$i}{id}";
my $hdl8 = mysql_exec->select($sql,1)->hundle;
while (my $h = $hdl8->fetch){
push @{$hyoso->{$i}}, $h->[0];
}
# freq of children
my $hdl9 = mysql_exec->select("
SELECT sum(genkei.num)
FROM genkei, hselection
WHERE
genkei.khhinshi_id = hselection.khhinshi_id
AND hselection.ifuse = 1
AND ( $sql_w )
AND NOT genkei.id = $genkei->{$i}{id}
",1)->hundle->fetch;
if ($hdl9){
$genkei->{$i}{add} = $hdl9->[0];
} else {
$genkei->{$i}{add} = 0;
}
}
# exec
foreach my $i (keys %{$config}){
# hyoso
my $sql = '';
$sql .= "
UPDATE hyoso
SET genkei_id = $genkei->{$i}{id}
WHERE
";
my $n = 0;
foreach my $h (@{$hyoso->{$i}}){
$sql .= " OR " if $n;
$sql .= "id = $h";
++$n;
}
next unless $n;
mysql_exec->do($sql,1);
# genkei 1
$sql = '';
$sql .= "DELETE FROM genkei\nWHERE ";
my $nn = 0;
foreach my $h (@{$config->{$i}}){
my $quoted2 = mysql_exec->quote($h);
$sql .= " OR " if $nn;
$sql .= "( name = $quoted2 AND NOT id = $genkei->{$i}{id} )";
++$nn;
}
mysql_exec->do($sql,1);
# genkei 2
my $new_num = $genkei->{$i}{num} + $genkei->{$i}{add};
$sql = '';
$sql .= "
UPDATE genkei
SET num = $new_num
WHERE id = $genkei->{$i}{id}
";
mysql_exec->do($sql,1);
# df
foreach my $u (@avail){
my $df = 0;
if ($u eq 'bun'){
$df = mysql_exec->select("
SELECT COUNT(DISTINCT bun_idt)
FROM hyosobun, hyoso
WHERE
hyosobun.bun_idt
AND hyosobun.hyoso_id = hyoso.id
AND hyoso.genkei_id = $genkei->{$i}{id}
",1)->hundle->fetch;
$df = $df->[0] if $df;
} else {
$df = mysql_exec->select("
SELECT COUNT(DISTINCT tid)
FROM hyosobun, $u"."_hb, hyoso
WHERE
hyosobun.id = $u"."_hb.hyosobun_id
AND hyosobun.hyoso_id = hyoso.id
AND hyoso.genkei_id = $genkei->{$i}{id}
",1)->hundle->fetch;
$df = $df->[0] if $df;
}
mysql_exec->do("
UPDATE df_$u
SET f = $df
WHERE genkei_id = $genkei->{$i}{id}
",1);
}
}
return 1;
}
sub reload_source{
my $copied = $::config_obj->os_path( $::project_obj->status_copied_file );
my $source = $::config_obj->os_path( $::project_obj->status_source_file );
if (-e $copied) {
unlink($copied)
or gui_errormsg->open(
type => 'file',
thefile =>$copied,
)
;
}
unless ( length($copied) ){
my $suf;
if ($source =~ /(\.[a-zA-Z]+?)$/) {
$suf = $1;
}
$copied = $::project_obj->file_datadir.'_tgt'.$suf;
$::project_obj->status_copied_file( $::config_obj->uni_path($copied) );
}
use File::Copy;
copy($source, $copied)
or gui_errormsg->open(
type => 'file',
thefile =>$copied,
)
;
print "Specified target file is copied to: $copied\n";
return 1;
}
sub reload_from_docx{
my $copied = $::config_obj->os_path( $::project_obj->status_copied_file );
my $target = $::config_obj->os_path( $::project_obj->file_target );
my $temp = $::project_obj->file_TempTXT;
use kh_docx;
my $c = kh_docx->new( $copied );
$c->{converted} = $temp;
$c->conv;
unless (-e $temp){
warn("Failed to reload data from ".$::project_obj->status_copied_file."\n");
return 0;
}
unlink $target;
rename($temp, $target) or die;
print "Converted to: $target\n";
unless ( $::project_obj->status_converted_file ){
$::project_obj->status_converted_file($target);
}
return 1;
}
sub reload_from_spreadsheet{
# Identify the target column
my $col_name = $::project_obj->status_selected_coln;
my $copied = $::config_obj->os_path( $::project_obj->status_copied_file );
my $columns = kh_spreadsheet->new($copied)->columns();
my $col = 0;
my $hit = 0;
foreach my $i (@{$columns}){
if ($i eq $col_name){
$hit = 1;
last;
}
++$col;
}
unless ($hit){
die(
"Cannot reload data from the Excel/CSV file: cannot identify the target column.\n"
."Please create a new project with the Excel/CSV file"
);
}
# Create text files from Excel/CSV
my $target = $::config_obj->os_path( $::project_obj->file_target );
my $var = $::config_obj->os_path( $::project_obj->status_var_file );
my $temp_t = $::project_obj->file_TempTXT;
my $temp_v = $::project_obj->file_TempTXT;
unlink($temp_t);
unlink($temp_v);
my $sheet_obj = kh_spreadsheet->new($copied);
$sheet_obj->save_files(
filet => $temp_t,
filev => $temp_v,
selected => $col,
lang => $::project_obj->morpho_analyzer_lang,
);
unless ( -e $temp_t ) {
return 0;
}
unlink($target);
unlink($var);
rename($temp_t, $target) or die;
rename($temp_v, $var) if -e $temp_v;
print "Converted to: $target\n";
unless ( $::project_obj->status_converted_file ){
$::project_obj->status_converted_file($target);
}
# Drop duplicated variable
use File::BOM;
File::BOM::open_bom (my $fh, $var, ":encoding(utf-8)" );
use Text::CSV_XS;
my $tsv = Text::CSV_XS->new({
binary => 1,
auto_diag => 2,
sep_char => "\t",
allow_loose_quotes => 1
});
my $names = $tsv->getline($fh);
close $fh;
foreach my $i (@{$names}){
mysql_outvar->delete(
name => $i,
);
}
# Load variables
mysql_outvar::read::tab->new(
file => $var,
tani => 'h5',
skip_checks => 1,
)->read if -e $var;
return 1;
}
sub make_cache{
# Light weight version of "hyosobun" for mysql_crossout.pm
mysql_exec->do("
DROP TABLE IF EXISTS hyosobun2
",1);
mysql_exec->do("
CREATE TABLE hyosobun2(
id INT primary key,
hyoso_id INT
)
",1);
mysql_exec->do("
INSERT INTO hyosobun2 (id, hyoso_id)
SELECT id, hyoso_id
FROM hyosobun
",1);
mysql_exec->do("
ALTER TABLE hyosobun2 ADD INDEX index1 (id, hyoso_id);
",1);
# Light weight version of "bun" for mysql_crossout.pm
mysql_exec->do("
DROP TABLE IF EXISTS bun_hb
",1);
mysql_exec->do("
CREATE TABLE bun_hb(
hyosobun_id INT primary key,
tid INT
)
",1);
mysql_exec->do("
INSERT INTO bun_hb (hyosobun_id, tid)
SELECT hyosobun.id, bun.id
FROM hyosobun, bun
WHERE
hyosobun.bun_id = bun.bun_id
AND hyosobun.dan_id = bun.dan_id
AND hyosobun.h5_id = bun.h5_id
AND hyosobun.h4_id = bun.h4_id
AND hyosobun.h3_id = bun.h3_id
AND hyosobun.h2_id = bun.h2_id
AND hyosobun.h1_id = bun.h1_id
ORDER BY hyosobun.id
",1);
}
# We will NOT count zero length headings as sentences
sub zero_length_headings{
# make a backup
mysql_exec->do("drop table if exists bun_bak",1);
mysql_exec->do("CREATE TABLE bun_bak LIKE bun",1);
mysql_exec->do("INSERT INTO bun_bak SELECT * FROM bun",1);
# check length (is it necessary?)
mysql_exec->do("drop table if exists bun_length_nouse;",1);
mysql_exec->do("
CREATE TABLE bun_length_nouse(
id int primary key not null,
len int
)
",1);
mysql_exec->do("
INSERT INTO bun_length_nouse (id, len)
SELECT
hyosobun.bun_idt,
SUM( IF(nouse=0 and not khhinshi_id=99999, 1, 0) ) as len
FROM hyosobun, hyoso, genkei
WHERE hyosobun.hyoso_id = hyoso.id
AND hyoso.genkei_id = genkei.id
GROUP BY hyosobun.bun_idt
ORDER BY hyosobun.bun_idt
",1);
# delete from "bun" table
mysql_exec->do("
DELETE
FROM bun
USING bun, bun_length_nouse
WHERE bun.id = bun_length_nouse.id
AND bun_length_nouse.len = 0;
",1);
# Counts of document length will be broken here and there,
# so I disabled this feature (count all heading sentences as 0)
#if ($::project_obj->status_hb) {
# mysql_exec->do("
# DELETE
# FROM bun
# WHERE bun_id = 0 AND dan_id = 0
# ",1);
#}
# add sequential number
mysql_exec->do("drop table if exists bun_tmp;",1);
mysql_exec->do("
CREATE TABLE bun_tmp(
id int primary key not null,
bun_id int not null,
dan_id int not null,
h5_id int not null,
h4_id int not null,
h3_id int not null,
h2_id int not null,
h1_id int not null,
seq int auto_increment not null UNIQUE
)
",1);
mysql_exec->do("
INSERT INTO bun_tmp (id, bun_id, dan_id, h5_id, h4_id, h3_id, h2_id, h1_id)
SELECT id, bun_id, dan_id, h5_id, h4_id, h3_id, h2_id, h1_id
FROM bun
ORDER BY id
",1);
mysql_exec->do("alter table bun_tmp CHANGE COLUMN seq seq int not null",1);
mysql_exec->do("drop table if exists bun",1);
mysql_exec->do("RENAME TABLE bun_tmp TO bun",1);
mysql_exec->do("ALTER TABLE bun ADD INDEX index1 (bun_id)",1);
mysql_exec->do("ALTER TABLE bun ADD INDEX index2 (dan_id)",1);
mysql_exec->do("ALTER TABLE bun ADD INDEX index3 (h5_id)",1);
mysql_exec->do("ALTER TABLE bun ADD INDEX index4 (h4_id)",1);
mysql_exec->do("ALTER TABLE bun ADD INDEX index5 (h3_id)",1);
mysql_exec->do("ALTER TABLE bun ADD INDEX index6 (h2_id)",1);
mysql_exec->do("ALTER TABLE bun ADD INDEX index7 (h1_id)",1);
mysql_exec->do("
ALTER TABLE bun ADD INDEX index8 (
bun_id,dan_id,h5_id,h4_id,h3_id,h2_id,h1_id
)
",1);
mysql_exec->do("ALTER TABLE bun ADD INDEX index9 (id,seq)",1);
}
sub fix_michigo{
my $self = shift;
# Japanese
if ( $::project_obj->morpho_analyzer_lang eq 'jp' ){
my $h = mysql_exec->select("
SELECT genkei.id, genkei.name
FROM genkei, khhinshi
WHERE
genkei.khhinshi_id = khhinshi.id
AND khhinshi.name = \"未知語\"
",1)->hundle;
my @gomi = ();
while (my $i = $h->fetch){
#print Jcode->new("$i->[1]: ")->sjis;
if ( $i->[1] =~ /[a-z]/io ){
#print "alpha\n";
next;
}
if ( $i->[1] =~ /\p{Han}|\p{Hiragana}|\p{Katakana}/o){
#print "zenkaku\n";
next;
}
if ( $i->[1] =~ /[a-z|A-Z]/o){
#print "zenkaku-alpha\n";
next;
}
#print "gomi\n";
push @gomi, $i->[0];
}
my $sql = '';
$sql .= "UPDATE genkei SET khhinshi_id = 9999 WHERE\n";
my $n = 0;
foreach my $i (@gomi){
$sql .= "OR " if $n;
$sql .= "( id = $i )\n";
++$n;
}
#print "$sql\n\n";
mysql_exec->do($sql, 1) if $n;
}
# Korean
if ( $::project_obj->morpho_analyzer_lang eq 'kr' ){
my $h = mysql_exec->select("
SELECT genkei.id, genkei.name
FROM genkei, khhinshi
WHERE
genkei.khhinshi_id = khhinshi.id
AND khhinshi.name = \"Unknown\"
",1)->hundle;
my @gomi = ();
while (my $i = $h->fetch){
# numbers
if ( $i->[1] =~ /^\p{PosixDigit}+$/o ){
push @gomi, $i->[0];
}
# symbols
if ( $i->[1] =~ /^\p{PosixPunct}+$/o ){
push @gomi, $i->[0];
}
}
my $sql = '';
$sql .= "UPDATE genkei SET khhinshi_id = 99999 WHERE\n";
my $n = 0;
foreach my $i (@gomi){
$sql .= "OR " if $n;
$sql .= "( id = $i )\n";
++$n;
}
#print "$sql\n\n";
mysql_exec->do($sql, 1) if $n;
}
}
# Korean Jamo fix
sub fix_hangul{
my $h = mysql_exec->select("
SELECT genkei.id, genkei.name, hyoso.id, hyoso.name
FROM hyoso, genkei, khhinshi
WHERE
hyoso.genkei_id = genkei.id
AND genkei.khhinshi_id = khhinshi.id
AND khhinshi.name = \"Unknown\"
")->hundle;
use Unicode::Normalize;
while (my $i = $h->fetch) {
my $t1 = NFC($i->[1]);
my $t2 = NFC($i->[3]);
unless ($t1 eq $i->[1]){
$t1 = mysql_exec->quote($t1);
mysql_exec->do("UPDATE genkei SET name = $t1 WHERE id = $i->[0]");
}
unless ($t2 eq $i->[2]){
$t2 = mysql_exec->quote($t2);
mysql_exec->do("UPDATE hyoso SET name = $t2 WHERE id = $i->[2]");
}
}
}
sub fix_katuyo{
my $self = shift;
mysql_exec->drop_table("katuyo_old");
mysql_exec->do("ALTER TABLE katuyo RENAME katuyo_old",1);
mysql_exec->do ("
create table katuyo (
id int auto_increment primary key not null,
name varchar(".$self->length('katuyo').")
)
",1);
my $sql = 'INSERT INTO katuyo (name) VALUES ';
my $h = mysql_exec->select("select name from katuyo_old",1)->hundle;
while (my $i = $h->fetch){
chomp $i->[0];
if ($i->[0] =~ /^(.*)\r$/){
$i->[0] = $1;
}
my $t = mysql_exec->quote($i->[0]);
$sql .= "($t),";
}
chop $sql;
mysql_exec->do("$sql",1);
mysql_exec->do("alter table katuyo add index index1 (id, name)",1);
}
#----------------------#
# データの読み込み #
sub readin{
my $self = shift;
my $file = $::config_obj->os_path( $::project_obj->file_MorphoOut );
my $icode = 'utf8mb4';
# 入力データの文字コード
if (
( $::config_obj->c_or_j eq 'chasen' )
#|| ( $::config_obj->c_or_j eq 'mecab' )
){
if ($::config_obj->os eq 'win32') {
$icode = 'cp932';
} else {
$icode = 'eucjpms'
}
}
#SCREEN Plugin
use screen_code::negationchecker;
screen_code::negationchecker::check_use_plug();
#SCREEN Plugin
# データファイルのサイズを確認
open (RCNT,$file) or
gui_errormsg->open(
type => 'file',
file => $file
);
my $max_rows = 0;
$max_rows += tr/\n/\n/ while read(RCNT, $_, 2 ** 16); # 行数カウント
close (RCNT);
$max_rows += 100;
$self->{max_rows} = "MAX_ROWS = $max_rows";
# ローデータの読み込み
mysql_exec->drop_table("rowdata");
mysql_exec->do("create table rowdata
(
hyoso varchar(128) binary not null,
yomi varchar(128) not null,
genkei varchar(128) not null,
hinshi varchar(128) not null,
katuyogata varchar(128) not null,
katuyo varchar(128) not null,
id int auto_increment primary key not null
) $self->{max_rows}
",1);
#my $thefile = $::config_obj->uni_path($file);
#my $icode = Jcode->new($thefile)->icode;
#$thefile = Jcode->new($thefile,$icode)->euc;
#$thefile =~ tr/\\/\//;
#$thefile = Jcode->new($thefile,'euc')->$icode;
#print "$thefile\n";
my $thefile = "'$file'";
mysql_exec->do("LOAD DATA LOCAL INFILE $thefile INTO TABLE rowdata CHARACTER SET $icode",1);
# 新しいバージョンの茶筌に対応するためのFix
mysql_exec->drop_table("rowdata_org");
mysql_exec->do("ALTER TABLE rowdata RENAME rowdata_org",1);
mysql_exec->do("create table rowdata
(
hyoso varchar(128) binary not null,
yomi varchar(128) not null,
genkei varchar(128) not null,
hinshi varchar(128) not null,
katuyogata varchar(128) not null,
katuyo varchar(128) not null,
id int primary key not null
) $self->{max_rows}
",1);
mysql_exec->do("
INSERT INTO rowdata (hyoso, yomi, genkei, hinshi, katuyogata, katuyo, id)
SELECT hyoso, yomi, if( ((length(genkei) = 0) and not (hyoso = 'EOS') and not (hyoso = 'EOS\r')), hyoso, genkei), hinshi, katuyogata, katuyo, id
FROM rowdata_org
ORDER BY id
",1);
mysql_exec->drop_table("rowdata_org");
# フィールド長の取得
my $morpho_length_error_flag = 0;
my $t = mysql_exec->select("
SELECT
MAX( CHAR_LENGTH(hyoso) ) AS hyoso,
MAX( CHAR_LENGTH(genkei) ) AS genkei,
MAX( CHAR_LENGTH(hinshi) ) AS hinshi,
MAX( CHAR_LENGTH(katuyo) ) AS katuyo
FROM rowdata
",1)->hundle;
my $r = $t->fetchrow_hashref;
$t->finish;
foreach my $key (keys %{$r}){
$self->length($key,$r->{$key});
$morpho_length_error_flag = 1 if $r->{$key} >= 128;
}
mysql_ready::dump->word_length if $morpho_length_error_flag;
mysql_ready::heap->rowdata($self);
# 強調文字列保存用のテーブルを準備
unless ( mysql_exec->table_exists('d_force') ){
mysql_exec->do("create table d_force
(
id int auto_increment primary key not null,
name varchar(128) not null,
type int not null
)
",1);
}
}
#----------------#
# データ整形 #
sub reform{
my $self = shift;
my $report_time = 0;
my $pt1 = new Benchmark;
# キャッシュ・テーブル作成
mysql_exec->drop_table("hgh");
my @len = (
$self->length('hyoso'),
$self->length('genkei'),
$self->length('hinshi'),
$self->length('katuyo')
);
mysql_exec->do("
create table hgh (
id int auto_increment primary key not null,
num int not null,
hyoso varchar($len[0]) binary not null,
genkei varchar($len[1]) not null,
hinshi varchar($len[2]) not null,
katuyo varchar($len[3]) not null
)
", 1);
mysql_exec->do("
INSERT
INTO hgh ( num, hyoso, genkei, hinshi, katuyo)
SELECT COUNT(*), hyoso, genkei, hinshi, katuyo
FROM rowdata
# WHERE LENGTH(rowdata.genkei) > 1
GROUP BY rowdata.katuyo, rowdata.hyoso, rowdata.genkei, rowdata.hinshi
", 1);
my $pt2 = new Benchmark;
print "\tcache(hgh)\t",timestr(timediff($pt2,$pt1)),"\n" if $report_time;
# 品詞テーブル作成
my $len = $self->length('hinshi');
mysql_exec->drop_table("hinshi");
mysql_exec->do("
create table hinshi (
id int auto_increment primary key not null,
name varchar($len) not null
)
",1);
mysql_exec->do('
INSERT
INTO hinshi (name)
SELECT (hinshi)
FROM hgh
WHERE (length(genkei) > 0) and (length(hinshi) > 0)
GROUP BY hinshi
',1);
mysql_exec->do("alter table hinshi add index index1 (name)",1);
my $pt3 = new Benchmark;
print "\thinshi\t\t",timestr(timediff($pt3,$pt2)),"\n" if $report_time;
# 原形テーブル作成
$len = $self->length('genkei'); # テーブル準備
mysql_exec->drop_table("genkei");
mysql_exec->do("
create table genkei (
id int auto_increment primary key not null,
name varchar($len) not null,
num int not null,
hinshi_id int not null,
khhinshi_id int not null,
nouse int not null default 0
)
",1);
my $rule; # 分類規則準備
if (mysql_exec->table_exists('hinshi_setting')){
$rule = mysql_exec->select(
"SELECT khhinshi,condition1,condition2,khhinshi_id "
."FROM hinshi_setting",
1
)->hundle->fetchall_arrayref;
} else {
my $dbhh = DBI->connect("dbi:CSV:", undef, undef, {
f_dir => "./config",
f_encoding => "UTF8",
csv_eol => "\n",
}) or die;
my $th = $dbhh->prepare("
SELECT kh_hinshi,condition1,condition2,hinshi_id
FROM hinshi_chasen
") or die;
$th->execute or die;
$rule = $th->fetchall_arrayref;
}
my %stopwords = ();
foreach my $i (@{$::config_obj->stopwords_current}){
$i =~ tr/A-Z/a-z/;
$stopwords{$i} = 1;
#print "$i, ";
}
#print "\n";
# データ準備
mysql_exec->drop_table("hgh2");
mysql_exec->do("
create table hgh2 (
id INT auto_increment primary key not null,
genkei varchar($len) not null,
sum INT,
h_id INT,
h_name varchar(".$self->length('hinshi').")
)
",1);
mysql_exec->do("
INSERT INTO hgh2 (genkei, sum, h_id, h_name)
SELECT hgh.genkei, SUM(num), hinshi.id, hinshi.name
FROM hgh, hinshi
WHERE hgh.hinshi=hinshi.name
GROUP BY hgh.genkei, hgh.hinshi
",1);
my ($num, $con) = (0,'');
my $id = 1;
while (1){
my $td = mysql_exec->select(
mysql_ready->genkei_sql($id, $id + $rows_per_once),
1
)->hundle;
unless ($td->rows > 0){
last;
}
$id += $rows_per_once;
while (my $d = $td->fetch){ # 振り分け
my $kh_hinshi = '9999';
my $chk = $d->[0];
#SCREEN Plugin
$chk = &screen_code::negationchecker::mod_for_detecting_pos($chk);
#SCREEN Plugin
foreach my $i (@{$rule}){
$i->[2] = '' unless defined($i->[2]);
if ( index("$d->[3]","$i->[1]") == 0 ){ # 条件1
if ($i->[2] eq 'ひらがな'){ # 条件2:ひらがな
if ($chk =~ /^\p{Hiragana}+$/o){
$kh_hinshi = $i->[3];
last;
}
}