-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTALT.plx
executable file
·220 lines (185 loc) · 4.72 KB
/
TALT.plx
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
package TALT;
use feature ":5.10";
use warnings;
use strict;
use utf8;
use Carp;
use Getopt::Easy;
use AProfile;
use Core::Tokens;
use Import::Wordlist;
use Benchmark qw(:all);
use YAML::Tiny qw(DumpFile);
use Encode qw(decode);
use Encode::Detect::Detector qw(detect);
use Export::Tables;
use File::Spec;
use Extensions::Collocations;
use Extensions;
our $VERSION='';
=pod
=head1 DESCRIPTION
Read TALT files from sourcedir and make wordilst, collocation etc.
=cut
get_options "s-sourcedir= w-wordlist=",
"usage: perl TALT.plx -w wordlist",
"H";
binmode(STDOUT,':utf8');
# initialize Core
my $Prof=AProfile->new();
my $Conf=$Prof->active_config;
my $toks=Core::Tokens->new();
my $tmo=$toks->get_textobject;
say $tmo;
my (@puff,$tok,$item,$param,$sigil,$value);
my $wordlist;
if ( $O{wordlist} and -f $O{wordlist} ) {
$wordlist=Import::Wordlist->new($O{wordlist});
$wordlist=$wordlist->get_wordlist;
#DumpFile( 'RELOADED.XLS.yml',$wordlist);
#exit;
} else {
warn "No wordlist of wordlist did not pass -f test" ;
}
# Load source & Tokenize
use Import::Source;
my $source=Import::Source->new();
my $tokenizer=Extensions->new( {type=>'tokenizer',module_name=>'default'} );
my $rex= qr/.*?\[(.*?)\]/;
$tokenizer->rex($rex);
my @tokens;
while ( my ($file,$text)=$source->get_next ) {
#($file)=($file=~/^(.*?)\s/);
@tokens=@{ $tokenizer->grep($text) };
@tokens= map {
my @a=split(',');
map{
s/^\s//;
s/\s+/./g;
s/\.{2,}/./g;
tr/I/1/ } @a;
@a } @tokens; # this is for ATU
# say $file;
# say join(' ',@tokens);
# <>;
$tmo->start_unit('text',$file);
process_tokens(\@tokens);
$tmo->close_unit('text');
}
$tmo->count_occurrences(['text']);
my $tlist=$toks->get_frequencies;
# TABLES CSV
=pod
my $tables=Export::Tables->new( { file=>'Table.csv',sheet=>'one', from=>'frequencies', unit=>'text', } );
$tables->header( $tmo->get_names('text'));
say "Number of tokens is ".$#$tlist;
$tables->generate_table( $tlist );
$tables->close;
=cut
# TABLES XLS
=pod
my $tables=Export::Tables->new( { file=>'Table_dupl.xlsx',sheet=>'one', from=>'frequencies', unit=>'text', } );
$tables->header( $tmo->get_names('text'));
say "Number of tokens is ".$#$tlist;
$tables->generate_table( $tlist );
$tables->close;
#$tables->close;
=cut
#COLLOCATION
=pod
for my $no ( 4 ) {
my $tables=Export::Tables->new( { file=>"Table_${no}let.xlsx",sheet=>"Colloc ${no}", from=>'frequencies', unit=>'text', } );
my $collobj=Extensions::Collocations->new( $tlist );
carp "Collocating: $no";
my $colled=$collobj->collocate($no,'text',10);
last unless $colled;
# $tables->add_sheet("Colloc $no");
$tlist=$colled->get_frequencies;
$tables->header( $tmo->get_names('text'));
$tables->generate_table( $tlist );
$tables->close;
}
=cut
# DECORATOR
=pod
use Export::Decorator;
my $decor=Export::Decorator->new('html');
my $text=$tmo->generate_text(undef,undef,$decor);
=cut
# STEMMER
=pod
my $stem;
my %stemmed;
use Extensions::Stemmer::Hunspell;
my $st=Extensions::Stemmer::Hunspell->new( );
my $path = "/home/salmonix/HUNSPELL/hu_HU-1.6.1/";
$st->set_module({ lang=>'hun',files=>[ $path."hu_HU.aff",$path."hu_HU.dic" ] });
my ($stem,$name);
foreach ( @$tlist ) {
$name=$_->{name};
$stem=$st->stem($name)||$name;
utf8::decode($stem);
push @{$stemmed{ $stem }},$_->{name};
}
DumpFile('STEMMED.yml',\%stemmed);
%stemmed=();
foreach ( @$tlist ) {
$name=$_->{name};
$stem=$st->base($name)||$name;
utf8::decode($stem);
push @{$stemmed{ $stem }},$_->{name};
}
DumpFile('BASED.yml',\%stemmed);
=cut
sub process_tokens {
foreach my $item ( @{$_[0]} ) {
$item=~s/\s+/ /; # normal multiple white spaces.
next unless $item;
chomp $item;
if ( ord($item) < 65 or ord($item) == 176 ) { # punctuation
$param->{hide}='#PUNCTUATION';
make_token($item,$param);
next;
}
if ( ! $wordlist ) { # no wordlist at all
make_token($item);
next;
}
# we have a wordlist
if ( not exists $wordlist->{$item} ) { # not in list
say "\t$item ---> is not in list";
make_token($item);
next;
}
no warnings;
($sigil,$value)=( $wordlist->{$item}[0]=~/^([#|]*)(.*)/ ); # process from list
given ( $sigil ) {
when ( '#' ) {
$param->{hide}='#STOPWORD';
}
when ( '|' ) {
1;
}
default {
$value=lc $value || lc $item;
$value=~s/\s+/ /;
}
}
$item=$value;
make_token($_,$param);
}
}
sub make_token {
map {
$tok=$toks->aToken($_,$_[1]);
$tmo->add_to_textmatrix($tok);
} split (' ',$_[0]);
}
1;
__END__
=pod
=head1 BUGS, WARNINGS and TODO
It is only a procedural skeleton. It shows:
- a processed text is a closed thing in terms of positions. Inserting a new
token is expensive and not implemented. Therefore any normalization must take
place with processing the text defining a pre-processing phase.