-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalltokens.pl
170 lines (159 loc) · 5.19 KB
/
alltokens.pl
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
#!/usr/bin/perl
# Usage: cat file.txt | alltokens.pl "'-" "A-Za-z"
# first argument is a string of 'interior-only' characters
# second argument is a string of characters that we grep for
use strict;
use warnings;
use Encode qw(decode);
use Unicode::Normalize;
use utf8;
if ($#ARGV != 1) {
die "Usage: $0 interiorcharstring bdcharstring\n";
}
@ARGV = map { NFC(decode('utf-8', $_)) } @ARGV;
my $ints = $ARGV[0];
my $bdstr = $ARGV[1];
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";
my $emoji = '\x{0023}\x{002A}-\x{0039}\x{00A9}\x{00AE}\x{203C}\x{2049}\x{2122}-\x{2B55}\x{1F004}-\x{1FAF6}';
my $skintone = '\x{1F3FB}-\x{1F3FF}';
# probably want to sync these with filt.pl
# (good way of cleaning corpus is to kill sentences with these!)
my @fixed = (
qr/<[\/]?[A-Za-z]([^>]+)?>/, # markup
qr/(?:https?|ftp):\/\/[A-ZÁÉÍÓÚa-záéíóú0-9\/.:=_%?&~+;\$@\#()-]+[A-ZÁÉÍÓÚa-záéíóú0-9\/=]/, # URLs
qr/[A-Za-z0-9][A-Za-z0-9._]*@[A-Za-z0-9.]+[A-Za-z0-9]/, # emails
qr/(?:www\.)?([A-ZÁÉÍÓÚa-záéíóú0-9-]+\.){1,3}(?:blog|ca|com|ed?u|i[em]|info|net|org|scot|(?:org|gov|co|ac)\.uk)/, # URLs w/o protocol
qr/&([A-Za-z.]+|#[0-9]+);/, # SGML entities & " ߤ etc.
qr/%([0-9]\$)?[A-Za-z]+/, # l10n vars, %1$S, %S, %d, %lu, etc.
qr/[:;=]['’0o-]?[()\]\\{}|dpDP][)]*/, # emoticons
qr/[$emoji][$skintone]?(\x{200D}[$emoji][$skintone]?)+[\x{FE0E}\x{FE0F}]?/, # non-trivial ZWJ sequences
qr/[$emoji][\x{FE0E}\x{FE0F}]/, # emoji with variation selector
qr/[\x{1F1E6}-\x{1F1FF}][\x{1F1E6}-\x{1F1FF}]/, # flags
qr/[\x{261D}\x{26F9}-\x{270D}\x{1F385}-\x{1F9DD}][$skintone]/, # skin-tone modified emoji but no ZWJ sequence
qr/[1-9][0-9]{0,2}(?:,[0-9]{3})+(?:\.[0-9]+)?/, # numbers with commas in them
qr/(?<![,0-9])[0-9]+(?:[:.][0-9]+)+/, # times: 6:45 11.30, IP addresses, etc.; negative lookbehind prevents this from breaking up token "5,000.00" found by previous regex
);
my $curr='';
my $toktype=0; # 0=word, 1=number, 2=other (punc, etc.)
sub flushcurr {
if ($curr ne '') {
if ($toktype==0) {
# first block is pair of balanced parens, but not initial/final
# e.g. analytic(al)
if ($curr =~ m/^..*\(.+\)/ or $curr =~ m/\(.+\).*.$/) {
print "$curr\n";
$curr = '';
return;
}
if ($curr =~ m/^([(]+)([^()]*[)]?)$/) { # "(Don't" or "(good)"
print "$1\n";
$curr = $2;
}
if ($ints eq '') {
print "$curr\n" unless ($curr eq '');
}
else {
(my $post) = $curr =~ m/([$ints]*)$/;
$curr =~ s/([$ints]*)$//;
print "$curr\n" unless ($curr eq '');
while ($post =~ /(.)/g) {
print "$1\n";
}
}
}
else {
print "$curr\n";
}
$curr = '';
}
}
# usually a line, or chunk between URLs, email addresses, etc.
sub process_chunk {
(my $chunk) = @_;
while ($chunk =~ /(.)/gs) {
my $c=$1;
if ($c =~ /^([\x{200C}\x{200D}\x{055A}\x{055B}\x{055C}\x{055E}]|\p{L}|\p{M}|[$bdstr])$/) {
flushcurr() unless ($toktype==0);
$toktype=0;
}
elsif ($ints ne '' and $c =~ /^[$ints]$/) {
unless ($toktype==0 and $curr ne '' and $curr !~ m/[$ints]$/) {
flushcurr();
$toktype=2;
}
# if toktype==0 and curr is non-empty, then do nothing, toktype
# remains 0 and append the intchar to curr
}
elsif ($c =~ /^[0-9]$/) { # won't get here if 0-9 are bdchars for lang
flushcurr() unless ($toktype==1);
$toktype=1;
}
else {
flushcurr();
$toktype=2;
}
$curr .= $c unless ($c =~ /^\s$/);
}
flushcurr();
$toktype = 0;
}
while (<STDIN>) {
my $newline_p = /\n$/;
# first part of each iteration converts the current line into
# a linked list of nodes, each representing a "chunk" of the line
# where a chunk consists of either a token matching one of the
# regexen in @fixed, or some substring in between those fixed tokens
# The point is that we want to apply these in order, and once we
# find a fixed token (an HTML tag, say), we don't want to look for
# the later patterns within it (URLs, say)
my %head = (
'string' => $_,
'fixed_p' => 0,
'next' => 0,
);
my $currnode;
for my $patt (@fixed) {
$currnode = \%head;
while ($currnode != 0) {
my $currstr = $currnode->{'string'};
if ($currnode->{'fixed_p'}==0 and $currstr =~ /$patt/) {
my $nextnode = $currnode->{'next'};
$currstr =~ s/($patt)/\n$1\n/sg;
my @chunks = split(/\n/,$currstr);
my $first = shift @chunks;
$currnode->{'string'} = $first;
$currnode->{'fixed_p'} = 1 if ($first =~ /$patt/);
for my $chunk (@chunks) {
unless ($chunk eq '') {
my %node = (
'string' => $chunk,
'fixed_p' => 0,
'next' => $nextnode,
);
$node{'fixed_p'} = 1 if ($chunk =~ /$patt/);
$currnode->{'next'} = \%node;
$currnode = $currnode->{'next'};
}
}
}
$currnode = $currnode->{'next'};
}
}
# the remainder iterates over the linked list, outputting the fixed
# tokens as they are, and passing the others to process_chunk for
# further decompsition into words, etc.
$currnode = \%head;
while ($currnode != 0) {
if ($currnode->{'fixed_p'}==1) {
print $currnode->{'string'}."\n";
}
else {
process_chunk($currnode->{'string'});
}
$currnode = $currnode->{'next'};
}
print '\n'."\n" if $newline_p;
}
exit 0;