-
Notifications
You must be signed in to change notification settings - Fork 52
/
countstopwords.pl
executable file
·67 lines (48 loc) · 1.09 KB
/
countstopwords.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
#!/usr/bin/perl
# Count frequency of one, two and three word phrases in XML files
# Use something like this:
# $ cat ~/pwdata/scrapedxml/debates/2004-05*.xml | ./countphrase.pl >out
use Data::Dumper;
$c = 0;
$counts = {};
while (<>)
{
chomp;
# Strip XML tags
s/\<.+?\>/ /g;
# Strip whitespace from ends, squash in middle
s/^\s+//;
s/\s+$//;
s/\s+/ /;
# Convert bad quotes of X
s/\bX([A-Z])/ $1/;
# Strip any other characters
s/[.,-:;?&]/ /g;
s/[^a-zA-Z ]//g;
# Split into words
split /\s/;
$lastword = "";
$lastlastword = "";
# Loop through words
foreach (@_)
{
# Strip whitespace from ends
s/^\s+//;
s/\s+$//;
next if ($_ eq "");
$_ = lc $_;
$counts->{$_}++;
if ($lastword ne "") {
$counts->{"$lastword $_"}++;
if ($lastlastword ne "") {
$counts->{"$lastlastword $lastword $_"}++;
}
}
$lastlastword = $lastword;
$lastword = $_;
}
}
@sorted = sort { $counts->{$a} <=> $counts->{$b} } keys %$counts;
foreach (@sorted) {
print $_ . " " . $counts->{$_} . "\n";
}