-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull_script.script
More file actions
3024 lines (2792 loc) · 85.1 KB
/
full_script.script
File metadata and controls
3024 lines (2792 loc) · 85.1 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
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
#!/Users/cat/perl
use strict;
use warnings;
package FreqDist;
use Carp;
# use lib '/Users/cat/perl5/lib/perl5';
sub new {
my $class = shift;
my %hash;
my %keyword_dict;
my $self = {
_types => 0,
_tokens => 0,
_hash => \%hash,
_keyword_dict => \%keyword_dict,
};
bless $self, $class;
return $self;
}
sub get_types {
my ($self) = @_;
return $self->{_types};
}
sub get_tokens {
my ($self) = @_;
return $self->{_tokens};
}
sub get_keys {
my ($self) = @_;
my @keys = ();
foreach my $key (sort { $self->{_hash}{$b} <=> $self->{_hash}{$a} } keys $self->{_hash}) {
push(@keys, $key);
}
return @keys;
}
sub get_hash {
my ($self) = @_;
return $self->{_hash};
}
sub get_count {
my ($self, $token) = @_;
my $count = $self->{_hash}{$token};
if ($count) {
return $count;
}
return 0;
}
sub get_prob_token {
my ($self, $token) = @_;
if ($self->get_tokens() == 0) { return 0; }
return $self->get_count($token) / $self->get_tokens();
}
sub get_prob_type {
my ($self, $type) = @_;
if ($self->get_types() == 0) { return 0; }
return 1 / $self->get_types();
}
sub get_normalized_freq {
my ($self, $token) = @_;
if ($token eq "" or $self->get_tokens() == 0) { return 0; }
return $self->get_count($token) / $self->get_tokens() * 1000000;
}
sub add_token {
my ($self, $token) = @_;
$self->{_hash}{$token} += 1;
$self->{_tokens} += 1;
$self->{_types} = scalar keys %{$self->{_hash}};
}
sub remove_type {
my ($self, $type) = @_;
my $tokens = $self->get_count($type);
delete $self->{_hash}{$type};
$self->{_tokens} -= $tokens;
$self->{_types} -= 1;
}
sub add_token_freq {
my ($self, $token, $freq) = @_;
if ($freq > 0) {
$self->{_hash}{$token} += $freq;
$self->{_tokens} += $freq;
$self->{_types} = keys %{$self->{_hash}};
}
}
sub get_max {
my ($self) = @_;
my $max_token;
my $max_freq = 0;
scalar keys $self->{_hash}; # reset the internal iterator so a prior each() doesn't affect the loop
while(my($token, $freq) = each $self->{_hash}) {
if ($freq > $max_freq) {
$max_token = $token;
$max_freq = $freq;
}
}
return ($max_token, $max_freq)
}
sub clear_hash { # TODO: fix this
my ($self) = @_;
$self->{_hash} = {};
$self->{_types} = 0;
$self->{_tokens} = 0;
$self->{_keyword_dict} = {};
}
sub out_to_txt {
my ($self, $filename) = @_;
my $out;
my $success;
if ($filename eq "STDOUT") {
$success = 0; # did not open a file
$out = *STDOUT;
}
else {
$success = open($out, ">", $filename);
if (! $success) {
carp "Couldn't open $filename, $!\n";
return -1;
}
}
printf $out "#Word types: %d\n", $self->{_types};
printf $out "#Word tokens: %d\n", $self->{_tokens};
printf $out "#Search results: 0\n";
my $rank = 1;
foreach my $key (sort { $self->{_hash}{$b} <=> $self->{_hash}{$a}
or $a cmp $b } keys $self->{_hash}) {
printf $out "%d\t%d\t%s\n", $rank, $self->{_hash}{$key}, $key;
$rank += 1;
}
close($out) if $success; # don't close STDOUT
}
sub open_from_txt {
my ($self, $filename) = @_;
# clear out the old values
$self->clear_hash();
# read in the new values
open(my $in, "<", $filename) || carp "Couldn't open $filename, $!";
while(my $line = <$in>) {
next if $. < 2; # skip first 2 lines which have types and tokens (https://stackoverflow.com/questions/14393295/best-way-to-skip-a-header-when-reading-in-from-a-text-file-in-perl)
chomp($line); # get rid of newline at end
my @word_and_freq = split(/\t/, $line);
my $len = @word_and_freq;
next if $len < 3;
my $freq = $word_and_freq[1];
$freq = int($freq);
$self->add_token_freq($word_and_freq[2], $freq);
}
close $in;
}
sub update { # TODO: check declaration
my ($self, $other) = @_;
my $other_hash = $other->get_hash(); # reset the internal iterator so a prior each() doesn't affect the loop
while(my($token, $freq) = each %{$other_hash}) {
$self->add_token_freq($token, $freq);
}
}
1;
#!/Users/cat/perl
use strict;
use warnings;
package Keyword;
use FreqDist; # this uses Carp
use Carp;
use Data::Dumper;
sub new {
my $class = shift;
my (%args) = @_;
my %keyword_dict;
my $freqdist1 = $args{freqdist1} // FreqDist->new(); # // is defined-or operator introduced in Perl 5.10
my $freqdist2 = $args{freqdist2} // FreqDist->new();
my $name1 = $args{name1} // "Corpus 1";
my $name2 = $args{name2} // "Corpus 2";
my $self = {
_freqdist1 => $freqdist1, # make empty FreqDist because this will be easier to deal with later
_freqdist2 => $freqdist2,
_name1 => $name1,
_name2 => $name2,
_keyword_dict => \%keyword_dict,
};
bless $self, $class;
return $self;
}
sub set_name1 {
my ($self, $name1) = @_; # TODO: type checking in case user gives non-string?
$self->{_name1} = $name1;
}
sub get_name1 {
my ($self) = @_;
return $self->{_name1};
}
sub set_name2 {
my ($self, $name2) = @_; # TODO: type checking in case user gives non-string?
$self->{_name2} = $name2;
}
sub set_freqdist1 {
my ($self, $freqdist1) = @_;
$self->{_freqdist1} = $freqdist1;
}
sub get_freqdist1 {
my ($self) = @_;
return $self->{_freqdist1};
}
sub set_freqdist2 {
my ($self, $freqdist2) = @_;
$self->{_freqdist2} = $freqdist2;
}
sub get_freqdist2 {
my ($self) = @_;
return $self->{_freqdist2};
}
sub swap_freqdists {
my ($self) = @_;
my $new_1 = $self->{_freqdist2};
my $new_2 = $self->{_freqdist1};
$self->set_freqdist1($new_1);
$self->set_freqdist2($new_2);
}
sub get_tokens {
my ($self) = @_;
return keys $self->{_keyword_dict};
}
sub get_token_stats {
my ($self, $token) = @_;
if ($token eq "") {
return {};
}
return $self->{_keyword_dict}{$token};
}
sub get_token_keyness {
my ($self, $token) = @_;
return $self->{_keyword_dict}{$token}{'keyness'};
}
sub keyword_analysis {
my ($self, $p) = @_;
my $crit;
if ($p == 0.05) { $crit = 3.84; }
elsif ($p == 0.01) { $crit = 6.63; }
elsif ($p == 0.001) { $crit = 10.83; }
elsif ($p == 0.0001) { $crit = 15.13; }
elsif ($p == 0){ $crit = 0; }
else {
warn("Invalid p value. Setting p value to .01\n");
$crit = 6.63;
}
my %keyword_hash;
my $types1 = $self->{_freqdist1}->get_types();
my $types2 = $self->{_freqdist2}->get_types();
# printf("# Corpus 1:\t%d\t%d\n", $self->{_freqdist1}->get_types(), $self->{_freqdist1}->get_tokens());
# printf("# Corpus 2:\t%d\t%d\n", $self->{_freqdist2}->get_types(), $self->{_freqdist2}->get_tokens());
scalar keys $self->{_keyword_dict}; # reset the internal iterator so a prior each() doesn't affect the loop
while(my($token, $freq1) = each $self->{_freqdist1}->get_hash()) {
my $freq2 = $self->{_freqdist2}->get_count($token);
my $norm1 = $self->{_freqdist1}->get_normalized_freq($token);
my $norm2 = $freq2 == 0? 0 : $self->{_freqdist2}->get_normalized_freq($token);
next if ($norm2 > $norm1); # avoid double counting when going both ways
# Note: double counting will still occur when $norm2 == $norm1, but in that case the log likelihood is 0 so we don't care
my $tokens1 = $self->{_freqdist1}->get_tokens();
my $tokens2 = $self->{_freqdist2}->get_tokens();
my $num = ($freq1 + $freq2) / ($tokens1 + $tokens2);
my $E1 = $tokens1 * $num;
my $E2 = $tokens2 * $num;
my $keyness;
if ($freq2 == 0 or $E2 == 0) {
$keyness = 2 * ($freq1 * log($freq1/$E1));
}
else {
$keyness = 2 * ($freq1 * log($freq1/$E1) + ($freq2 * log($freq2/$E2)));
}
next if ($keyness < $crit);
$keyword_hash{$token} = {'keyness'=> $keyness, 'freq1'=>$freq1, 'norm1'=>$norm1, 'freq2'=>$freq2, 'norm2'=>$norm2};
}
$self->{_keyword_dict} = \%keyword_hash; # TODO: deal with types and tokens
# print(Dumper($self->{_keyword_dict}));
return \%keyword_hash; # return hash in case we want to use it later
}
sub print_keywords {
my ($self, $filename) = @_;
my $out;
my $success;
if ($filename eq "STDOUT") {
$success = 0; # did not open a file
$out = *STDOUT;
}
else {
$success = open($out, ">", $filename);
if (! $success) {
carp "Couldn't open $filename, $!\n";
return -1;
}
}
printf($out "# Corpus 1:\t%d\t%d\n", $self->{_freqdist1}->get_types(), $self->{_freqdist1}->get_tokens());
printf($out "# Corpus 2:\t%d\t%d\n", $self->{_freqdist2}->get_types(), $self->{_freqdist2}->get_tokens());
printf($out "# %s\t%s\t%s\t%s\t%s\t%s\n", "word", "keyness", "freq1",
"norm1", "freq2", "norm2");
foreach my $key (sort { $self->{_keyword_dict}{$a}{'keyness'} <=> $self->{_keyword_dict}{$b}{'keyness'}
or $a cmp $b } keys $self->{_keyword_dict}) {
printf($out "%s\t%f\t%d\t%f\t%d\t%f\n", $key, $self->{_keyword_dict}{$key}{'keyness'}, $self->{_keyword_dict}{$key}{'freq1'},
$self->{_keyword_dict}{$key}{'norm1'}, $self->{_keyword_dict}{$key}{'freq2'}, $self->{_keyword_dict}{$key}{'norm2'});
}
close($out) if $success; # don't close STDOUT
}
1;
#!/Users/cat/perl
package TagParser;
use base qw(HTML::Parser);
#!/Users/cat/perl
use strict;
use warnings;
use lib '/Users/cat/perl5/lib/perl5';
use Lingua::EN::Segmenter::TextTiling qw(segments); # Note: Splitter.pm has been modified to not change case
use FreqDist;
use Keyword;
use Data::Dumper;
sub tokenize_dir {
my ($dirname, $splitter) = @_;
my $freq_dist_obj = FreqDist->new(); # TODO update definition
opendir(DIR, $dirname) || warn "Could not open $dirname, $!\n";
while (my $filename = readdir(DIR)) {
if (-f $filename) { # is this enough to avoid .DS_Store ?
# print "processing {$filename}";
my $current_freqdist = tokenize_file("$dirname/$filename", $splitter);
$freq_dist_obj->update($current_freqdist);
}
}
closedir(DIR);
return $freq_dist_obj;
}
sub tokenize_file {
my ($filename, $splitter, $case_sensitive) = @_;
# if (! $case_sensitive) {
# print("lc\n");
# }
# else {
# print("original\n");
# }
my $current_freqdist = FreqDist->new();
open(my $in, "<", $filename) || warn "Could not open tokenize $filename, $!\n";
while(my $line = <$in>) {
my $tokens = $splitter->words($line);
foreach my $token (@$tokens) {
if ($token eq "") {
next;
}
if (! $case_sensitive) {
$token = lc $token;
}
$current_freqdist->add_token($token);
# condition not needed; changed token def if $token =~ /^[a-zA-Z']+$/; # add the token unless it contains a non-alpha character
}
}
close $in;
return $current_freqdist;
}
sub tokenize_from_idlist {
my ($list_filename, $path, $splitter, $case_sensitive) = @_;
open(my $in, "<$list_filename") || warn "Could not open list_filename, $!\n";
my $freq_dist_obj = FreqDist->new(); # TODO update definition
while(my $line = <$in>) {
chomp($line);
my $filename = "$path/$line.txt";
# print($filename, "\n");
my $current_freqdist = tokenize_file($filename, $splitter, $case_sensitive);
$freq_dist_obj->update($current_freqdist);
}
close $in;
return $freq_dist_obj;
}
# TODO: modify to work with list of filenames
sub find_prototypical {
my (%args) = @_;
my $comp_freqdist = $args{comp_freqdist} // die "No comparison freqdist provided";
my $source_dir = $args{source_dir} // ".";
my $idlist = $args{idlist} // die "No idlist provided";
my $outfilename = $args{outfilename} // "$source_dir/prototypical.txt";
my $n = $args{n} // 0; # 0 means get everything
my $extension = $args{extension} // "";
my $case_sensitive = $args{case_sensitive} // 0;
my $splitter = $args{splitter} // die "No splitter provided!";
my $use_wordlist = $args{use_wordlist} // 0;
my $verbose = $args{verbose} // 0;
my %file_data = {};
my $keyword_obj = Keyword->new(freqdist1 => $comp_freqdist);
open(my $in, "<$idlist") || warn "Could not open $idlist, $!\n";
while(my $line = <$in>) {
chomp($line);
if ($verbose) {
print("$line wordlist ");
}
my $filename = "$source_dir/$line$extension";
my $current_freqdist = FreqDist->new();
if ($use_wordlist) {
$current_freqdist->open_from_txt($filename);
}
else {
$current_freqdist = tokenize_file($filename, $splitter, $case_sensitive);
}
my $sum = 0;
if ($verbose) {
print("keywords\n");
}
$keyword_obj->set_freqdist2($current_freqdist);
$keyword_obj->keyword_analysis(0); # p value of 0
foreach my $token ($keyword_obj->get_tokens()) {
$sum += $keyword_obj->get_token_keyness($token);
}
$keyword_obj->swap_freqdists();
$keyword_obj->keyword_analysis(0); # p value of 0
foreach my $token ($keyword_obj->get_tokens()) {
$sum += $keyword_obj->get_token_keyness($token);
}
$file_data{$filename} = $sum;
}
close $in;
my $out;
my $success = open($out, ">", $outfilename);
if (! $success) {
warn "Couldn't open $outfilename, $!\n";
return -1;
}
my $i = 0;
if ($n == 0 or $n >= keys %file_data) {
$n = keys %file_data; # TODO: see if this works
}
if ($verbose) {
print("sort and print results\n");
# print(Dumper(\%file_data));
}
foreach my $key (keys %file_data) {
if (! defined $file_data{$key}) {
print("$key\n");
delete $file_data{$key};
}
# print "$key\n";
# printf "%s\t%f\n", $key, $file_data{$key};
}
foreach my $key (sort { $file_data{$a} <=> $file_data{$b} } keys %file_data) {
last if ($i == $n);
printf $out "%s\t%f\n", $key, $file_data{$key};
$i += 1;
}
close $out;
}
return 1;
use strict;
use warnings;
use Lingua::EN::Segmenter::TextTiling qw(segments);
use FreqDist;
use Keyword;
use Time::HiRes qw(time);
use Path::Tiny qw(path);
use File::Basename;
use File::Copy;
require "./Tokenize.PL";
my $global_default_extension = ".txt";
my $global_save_intermediate = "0";
my $global_save_intermediate_dir = "";
my $global_regex = "[^a-zA-Z]+";
my $global_case_sensitive = "0"; # case insensitive
my $global_stop_words = ();
my $tool_wordlist_regex_checkval = "0"; # match whatever global is
my $tool_wordlist_regex = "";
my $tool_wordlist_corpus_checkval = "0"; # raw files (1 means wordlists)
my $tool_wordlist_wordlists = ();
my $tool_concordance_case = "0"; # same as global settings
my $tool_concordance_win_length = "7";
my $tool_ngram_case = "0";
my $tool_ngram_regex_filename = "valid_tokens.txt";
my $tool_ngram_nontoken_checkval = "0";
my $tool_ngram_nontoken_filename = "";
my $tool_ngram_stop_checkval = "0";
my $tool_ngram_stop_filename = "";
my $tool_ngram_freq_checkval = "0";
my $tool_ngram_freq = "1";
my $tool_ngram_ufreq_checkval = "0";
my $tool_ngram_ufreq = "1";
my $tool_ngram_newline_checkval = "1";
my $tool_ngram_precision = "6";
my $tool_ngram_measure_2 = "ll.pm";
my $tool_ngram_measure_3 = "ll.pm";
my $tool_ngram_measure_4 = "ll.pm";
my $tool_ngram_script_dir = "/Users/cat/perl5/bin";
my $tool_keyword_p = 0.01;
my $tool_keyword_reference_checkval = "0"; # text files
my @tool_keyword_files = ();
my $tool_keyword_save_freqdist = "0";
my $keyword_outfilename = "";
my $wordlist_types = "0";
my $wordlist_tokens = "0";
my $wordlist_search_num = "0";
my $wordlist_freqdist = FreqDist->new();
my $keyword_freqdist = FreqDist->new();
my $keyword_obj = Keyword->new();
my @wordlist_files = ();
my $splitter = Lingua::EN::Splitter->new();
$splitter->set_non_word_regexp("$global_regex");
sub set_global_settings {
print("Default extension to use with openDir. Currently $global_default_extension: ");
my $input = <STDIN>;
chomp($input);
if ($input ne "") {$global_default_extension = $input};
print("Global regex to define nontoken (simplest way is [^nontoken]+). Currently $global_regex: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$global_regex = $input};
print("Global case sensitive (1/0). Currently $global_case_sensitive: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$global_case_sensitive = int($input)};
# print("Global stop words (currently $global_stop_words)");
# # TODO: implement add stop words
print("\n");
}
sub view_global_settings {
print("Default extension to use with openDir: $global_default_extension\n");
print("Global regex to define token: $global_regex\n");
print("Global case sensitive: $global_case_sensitive\n");
}
sub set_tool_settings_wordlist {
print("Use global regex for nontoken definition (currently $global_regex) (0) or write your own (1).
Currently $tool_wordlist_regex_checkval: ");
my $input = <STDIN>;
chomp($input);
# TODO: why isn't this working?
if ($input ne "") {$tool_wordlist_regex_checkval = $input};
if ($tool_wordlist_regex_checkval eq "1") {
print("Set wordlist regex for nontoken (simplest way is [^token]+ or !token). Currently $tool_wordlist_regex: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_wordlist_regex = $input};
}
print("Load wordlist from text files (0) or wordlist files (1). Currently $tool_wordlist_corpus_checkval: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_wordlist_corpus_checkval = $input};
print("\n");
}
sub view_tool_settings_wordlist {
print("Use global regex: $tool_wordlist_regex_checkval\n");
if (! $tool_wordlist_regex_checkval) {
print("Wordlist regex: $tool_wordlist_regex\n");
}
print("Wordlist from text files (0) or wordlist files (1): $tool_wordlist_corpus_checkval\n");
}
sub set_tool_settings_concordance {
print("Case sensitive: same as global (currently $global_case_sensitive) (0), sensitive (1), or insensitive (2). Currently $tool_concordance_case: ");
my $input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_concordance_case = $input};
print("Window (how many tokens to display before and after query). Currently $tool_concordance_win_length: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_concordance_win_length = $input};
print("\n");
}
sub view_tool_settings_concordance {
print("Case sensitive: $tool_concordance_case\n");
print("Window (how many tokens to display before and after query): $tool_concordance_win_length\n");
}
sub set_tool_settings_ngram {
print("Case sensitive: same as global (currently $global_case_sensitive) (0), sensitive (1), or insensitive (2). Currently $tool_ngram_case: ");
my $input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_case = $input};
print("Filename containing token regexes for ngrams. Currently $tool_ngram_regex_filename: ");
$input = <STDIN>;
chomp($input);
if ($input ne "" and -f $input) {
$tool_ngram_regex_filename = $input;
}
else {
warn("$input could not be found.");
}
print("Use file for nontokens (1/0). Currently $tool_ngram_nontoken_checkval: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_nontoken_checkval = $input};
if ($tool_ngram_nontoken_checkval eq "1") {
print("Filename containing nontoken regexes for ngrams (currently $tool_ngram_nontoken_filename): ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_nontoken_filename = $input};
}
print("Use file for stopwords (1/0). Currently $tool_ngram_stop_checkval: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_stop_checkval = $input};
if ($tool_ngram_stop_checkval eq "1") {
print("Filename containing stopword regexes for ngrams. Currently $tool_ngram_stop_filename: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_stop_filename = $input};
}
print("Set minimum frequency (1/0). Currently $tool_ngram_freq_checkval: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_freq_checkval = $input};
if ($tool_ngram_freq_checkval eq "1") {
print("Minimum frequency. Currently $tool_ngram_freq: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_freq = $input};
}
print("Set maximum frequency (1/0). Currently $tool_ngram_ufreq_checkval: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_ufreq_checkval = $input};
if ($tool_ngram_ufreq_checkval eq "1") {
print("Maximum frequency. Currently $tool_ngram_ufreq: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_ufreq = $input};
}
print("Count ngrams across newline tokens (1/0). Currently $tool_ngram_newline_checkval: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_newline_checkval = $input};
print("Number of digits to round results to. Currently $tool_ngram_precision: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_newline_checkval = $input};
print("Measure to use for bigrams.
Chi phi (phi.pm), tscore.pm, Chi squared (x2.pm),
dice.pm, jaccard.pm,
fisherLeft.pm, fisherRight.pm, fisherTwotailed.pm,
Log likelihood (ll.pm), Pointwise Mutual Information (pmi.pm),
Poisson Stirling (ps.pm), True Mutual Information (tmi.pm),
odds.pm
Currently $tool_ngram_measure_2: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_measure_2 = $input};
print("Measure to use for trigrams.
Log likelihood (ll.pm), Pointwise Mutual Information (pmi.pm),
Poisson Stirling (ps.pm), True Mutual Information (tmi.pm),
Currently $tool_ngram_measure_3: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_ngram_measure_3 = $input};
print("Path to count.pl and statistic.pl. Currently $tool_ngram_script_dir: ");
$input = <STDIN>;
chomp($input);
if (-f "$input/count.pl" and -f "$input/statistic.pl") {
$tool_ngram_script_dir = $input;
}
else {
warn("count.pl and/or statistic.pl couldn't be found in $tool_ngram_script_dir");
}
print("\n");
}
sub view_tool_settings_ngram {
print("Case sensitive: $tool_ngram_case\n");
print("Use file for nontokens (1/0): $tool_ngram_nontoken_checkval\n");
if (! $tool_ngram_nontoken_checkval) {
print("Filename containing nontoken regexes for ngrams: $tool_ngram_nontoken_filename\n");
}
print("Use file for stopwords (1/0): $tool_ngram_stop_checkval\n");
if (! $tool_ngram_stop_checkval) {
print("Filename containing stopword regexes for ngrams: $tool_ngram_stop_filename\n");
}
print("Set minimum frequency (1/0): $tool_ngram_freq_checkval\n");
if (! $tool_ngram_freq_checkval) {
print("Minimum frequency: $tool_ngram_freq\n");
}
print("Set maximum frequency (1/0): $tool_ngram_ufreq_checkval\n");
if (! $tool_ngram_ufreq_checkval) {
print("Maximum frequency: $tool_ngram_ufreq\n");
}
print("Count ngrams across newline tokens (1/0): $tool_ngram_newline_checkval\n");
print("Number of digits to round results to: $tool_ngram_precision\n");
print("Measure to use for bigrams: $tool_ngram_measure_2\n");
print("Measure to use for trigrams: $tool_ngram_measure_3\n");
print("Path to count.pl and statistic.pl: $tool_ngram_script_dir\n");
}
sub set_tool_settings_keyword {
print("P value for keyword analysis
p = 0.05 (exclude keywords with log likelihood < 3.84),
p = 0.01 (exclude keywords with log likelihood < 6.63),
p = 0.001 (exclude keywords with log likelihood < 10.83,
p = 0.0001 (exclude keywords with log likelihood < 15.13,
p = 0 (include all keywords)
Currently $tool_keyword_p: ");
my $input = <STDIN>;
chomp($input);
# convert input to float. This is a bit messy. TODO: refactor
if ($input eq "0.05" or $input eq ".05") {
$tool_keyword_p = 0.05;
}
elsif ($input eq "0.01" or $input eq ".01") {
$tool_keyword_p = 0.01;
}
elsif ($input eq "0.001" or $input eq ".001") {
$tool_keyword_p = 0.001;
}
elsif ($input eq "0.0001" or $input eq ".0001") {
$tool_keyword_p = 0.0001;
}
elsif ($input eq "0") {
$tool_keyword_p = 0;
}
print("Use text files (0) or wordlists (1). Currently $tool_keyword_reference_checkval: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$tool_keyword_reference_checkval = $input};
print("Keyword files are @tool_keyword_files. 1 to add files, 2 to add directory, 3 to remove files, 4 to swap wordlist and keyword files\n");
$input = <STDIN>;
chomp($input);
if ($input eq "1") {
print("Filenames separated by spaces: ");
my $names = <STDIN>;
chomp($names);
foreach my $filename (split(" ", $names)) {
if (-f $filename) {
push(@tool_keyword_files, $filename);
}
else {
warn("$filename could not be found.");
}
}
}
elsif ($input eq "2") {
print("Directory: ");
my $dirname = <STDIN>;
chomp($dirname);
if (-d $dirname) {
opendir(DIR, $dirname) || warn "Could not open $dirname, $!\n";
while (my $filename = readdir(DIR)) {
$filename = "$dirname/$filename";
if (-f $filename) {
if ($filename =~ /$global_default_extension$/) {
push(@tool_keyword_files, $filename);
}
else {
warn("Skipping $filename because it doesn't match the extension");
}
}
else {
warn("$filename could not be found.");
}
}
}
else {
warn("$dirname could not be found.");
}
}
elsif ($input eq "3") {
for my $i (0..$#tool_keyword_files) {
my $filename = $tool_keyword_files[$i];
print("$i\t$filename\n");
}
print("Indexes of files to remove separated by spaces or 'all' to remove all: ");
my $input = <STDIN>;
chomp($input);
if ($input eq "all") {
@tool_keyword_files = ();
}
else {
my @indexes = split(" ", $input);
foreach my $del_idx (@indexes) {
$del_idx = int($del_idx);
splice(@tool_keyword_files, $del_idx, 1);
}
}
}
elsif ($input eq "4") {
my @temp = @tool_keyword_files;
my $temp_check = $tool_keyword_reference_checkval;
@tool_keyword_files = @wordlist_files;
$tool_keyword_reference_checkval = $tool_wordlist_corpus_checkval;
@wordlist_files = @temp;
$tool_wordlist_corpus_checkval = $temp_check;
$keyword_obj->swap_freqdists();
$wordlist_freqdist = $keyword_obj->get_freqdist1();
$keyword_freqdist = $keyword_obj->get_freqdist2();
}
print("Save keyword FreqDist to a file after creating it (1/0). Currently $tool_keyword_save_freqdist: ");
$input = <STDIN>;
chomp($input);
if ($input eq "1") {
$tool_keyword_save_freqdist = "1";
print("Filename to store keyword FreqDist in. Currently $keyword_outfilename: ");
$input = <STDIN>;
chomp($input);
if ($input ne "") {$keyword_outfilename = $input;}
}
}
sub view_tool_settings_keyword {
print("P value for keyword analysis: $tool_keyword_p\n");
print("Use text files (0) or wordlists (1): $tool_keyword_reference_checkval\n");
print("Keyword files: @tool_keyword_files\n");
print("Save keyword FreqDist to a file after creating it (1/0): $tool_keyword_save_freqdist\n");
print("Filename to store keyword FreqDist in: $keyword_outfilename\n");
}
sub run_wordlist {
print("
1: Get wordlist
2: Search wordlist
0: Exit
");
my $selection = <STDIN>;
chomp($selection);
if ($selection eq "1") {
$wordlist_freqdist->clear_hash(); # clear out the FreqDist
if ($tool_wordlist_corpus_checkval eq "0") { # use regular files
if ($tool_wordlist_regex_checkval eq "1") {
print("using local regex $tool_wordlist_regex\n");
$splitter->set_non_word_regexp("$tool_wordlist_regex");
}
elsif ($tool_wordlist_regex_checkval eq "0") {
print("using global regex $global_regex\n");
$splitter->set_non_word_regexp("$global_regex");
}
foreach my $filename (@wordlist_files) {
my $case = $global_case_sensitive eq "1"? 1 : 0; # TODO: more elegeant solution
$wordlist_freqdist->update(tokenize_file($filename, $splitter, $case));
}
}
else {
foreach my $filename (@wordlist_files) {
my $current_freq_dist = FreqDist->new();
$current_freq_dist->open_from_txt($filename);
$wordlist_freqdist->update($current_freq_dist);
}
}
print("Name of file to save in (or STDOUT to output to screen): ");
my $save_name = <STDIN>;
chomp($save_name);
$wordlist_freqdist->out_to_txt($save_name);
unless ($save_name eq "STDOUT") {
print("Display results (1/0): ");
my $answer = <STDIN>;
chomp($answer);
if ($answer eq "1") {
open(my $in, "<", $save_name) || warn "Could not open $save_name, $!\n";
while(my $line = <$in>) {
print($line);
}
close $in;
}
}
}
elsif ($selection eq "2") { # TODO: get search to work
print("Regex (0) or text (1): ");
my $reg_or_txt = <STDIN>;
chomp($reg_or_txt);
print("Query: ");
my $query = <STDIN>;
chomp($query);
if ($reg_or_txt eq "1") {
$query = quotemeta($query); # escape special characters
}
print("Exact match (0) or partial match (1): ");
my $exact_or_partial = <STDIN>;
chomp($exact_or_partial);
foreach my $token ($wordlist_freqdist->get_keys()) {
if (search_token($token, $query, $exact_or_partial)) {
printf("%d\t%s\n", $wordlist_freqdist->get_count($token), $token);
}
}
}
}
sub search_token {
my ($token, $query, $exact_or_partial) = @_;
if ($exact_or_partial eq "1") { # not exact
if ($token =~ /$query/) {
return 1;
}
}