| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | frequency.pl - Compute the distribution of senses in a Senseval-2 data file | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | frequency.pl [OPTIONS] SOURCE | ||
| 10 | |||
| 11 | You can find begin.v-test.xml in samples/Data | ||
| 12 | |||
| 13 | frequency.pl begin.v-test.xml | ||
| 14 | |||
| 15 | Output => | ||
| 16 | |||
| 17 | <sense id="begin%2:30:00::" percent="64.31"/> | ||
| 18 | <sense id="begin%2:30:01::" percent="14.51"/> | ||
| 19 | <sense id="begin%2:42:04::" percent="21.18"/> | ||
| 20 | Total Instances = 255 | ||
| 21 | Total Distinct Senses=3 | ||
| 22 | Distribution={64.31,21.18,14.51} | ||
| 23 | % of Majority Sense = 64.31 | ||
| 24 | |||
| 25 | Type C<frequency.pl --help> for a quick summary of options | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | Displays distribution of senses in a given Senseval-2 file to STDOUT. | ||
| 30 | This information can be used to better understand the data, and also to | ||
| 31 | decide to filter low frequency senses (using L<filter.pl>) or balance | ||
| 32 | the distribution of senses (using L<balance.pl>). | ||
| 33 | |||
| 34 | =head1 INPUT | ||
| 35 | |||
| 36 | =head2 Required Arguments: | ||
| 37 | |||
| 38 | =head4 SOURCE | ||
| 39 | |||
| 40 | SOURCE should be a Senseval-2 formatted file. The sense ids are searched by | ||
| 41 | matching a regex /sense\s*id="S"/. | ||
| 42 | |||
| 43 | An instance having multiple sense ids should appear only once with multiple | ||
| 44 | <answer> tags. e.g. If an instance IID has 2 sense ids SID1 and SID2, then | ||
| 45 | in the SOURCE file, instance IID should be formatted as - | ||
| 46 | |||
| 47 | <instance id="IID"> | ||
| 48 | <answer instance="IID" senseid="SID1"/> | ||
| 49 | <answer instance="IID" senseid="SID2"/> | ||
| 50 | <context> | ||
| 51 | Context Data comes here .... | ||
| 52 | </context> | ||
| 53 | </instance> | ||
| 54 | |||
| 55 | =head2 Optional Arguments: | ||
| 56 | |||
| 57 | =head4 --help | ||
| 58 | |||
| 59 | Displays this message. | ||
| 60 | |||
| 61 | =head4 --version | ||
| 62 | |||
| 63 | Displays the version information. | ||
| 64 | |||
| 65 | =head1 OUTPUT | ||
| 66 | |||
| 67 | Output displays | ||
| 68 | |||
| 69 | 1. Total number of instances in SOURCE | ||
| 70 | |||
| 71 | These are counted by matching regex /instance id=\"ID\"/ for unique | ||
| 72 | instance ids. | ||
| 73 | |||
| 74 | 2. Total number of distinct sense tags found in SOURCE | ||
| 75 | |||
| 76 | These are searched by matching a regex /sense\s*id="S"/. | ||
| 77 | |||
| 78 | 3. Sense Distribution | ||
| 79 | |||
| 80 | Output shows | ||
| 81 | |||
| 82 | <sense id="S" percent="P"/> | ||
| 83 | |||
| 84 | for each sense id found in SOURCE. P is the percentage frequency of the | ||
| 85 | sense S. | ||
| 86 | |||
| 87 | 4. % of Majority sense | ||
| 88 | |||
| 89 | This will be the highest sense percentage found in SOURCE. | ||
| 90 | |||
| 91 | =head2 Sample Output | ||
| 92 | |||
| 93 | <sense id="begin%2:30:00::" percent="59.49"/> | ||
| 94 | <sense id="begin%2:30:01::" percent="13.38"/> | ||
| 95 | <sense id="begin%2:42:00::" percent="4.70"/> | ||
| 96 | <sense id="begin%2:42:03::" percent="3.44"/> | ||
| 97 | <sense id="begin%2:42:04::" percent="18.99"/> | ||
| 98 | Total Instances = 548 | ||
| 99 | Total Distinct Senses=5 | ||
| 100 | Distribution={59.49,18.99,13.38,4.70,3.44} | ||
| 101 | % of Majority Sense = 59.49 | ||
| 102 | |||
| 103 | Shows that there are total 548 instances and 5 senses. | ||
| 104 | |||
| 105 | The senses are distributed with frequencies | ||
| 106 | |||
| 107 | {59.49,18.99,13.38,4.70,3.44} | ||
| 108 | |||
| 109 | where majority sense has frequency = 59.49 | ||
| 110 | |||
| 111 | The <sense> tags show the frequency of each individual tag. | ||
| 112 | |||
| 113 | =head1 AUTHORS | ||
| 114 | |||
| 115 | Ted Pedersen, University of Minnesota, Duluth | ||
| 116 | tpederse at d.umn.edu | ||
| 117 | |||
| 118 | Amruta Purandare, University of Pittsburgh | ||
| 119 | |||
| 120 | |||
| 121 | =head1 COPYRIGHT | ||
| 122 | |||
| 123 | Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen | ||
| 124 | |||
| 125 | This program is free software; you can redistribute it and/or modify it | ||
| 126 | under the terms of the GNU General Public License as published by the | ||
| 127 | Free Software Foundation; either version 2 of the License, or (at your | ||
| 128 | option) any later version. | ||
| 129 | |||
| 130 | This program is distributed in the hope that it will be useful, but | ||
| 131 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 132 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
| 133 | Public License for more details. | ||
| 134 | |||
| 135 | You should have received a copy of the GNU General Public License along | ||
| 136 | with this program; if not, write to : | ||
| 137 | |||
| 138 | The Free Software Foundation, Inc., | ||
| 139 | 59 Temple Place - Suite 330, | ||
| 140 | Boston, MA 02111-1307, USA. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | ############################################################################### | ||
| 145 | |||
| 146 | # THE CODE STARTS HERE | ||
| 147 | |||
| 148 | ############################################################################### | ||
| 149 | |||
| 150 | # ================================ | ||
| 151 | # COMMAND LINE OPTIONS AND USAGE | ||
| 152 | # ================================ | ||
| 153 | |||
| 154 | # command line options | ||
| 155 | use Getopt::Long; | ||
| 156 | GetOptions ("help","version"); | ||
| 157 | # show help option | ||
| 158 | if(defined $opt_help) | ||
| 159 | { | ||
| 160 | $opt_help=1; | ||
| 161 | &showhelp(); | ||
| 162 | exit; | ||
| 163 | } | ||
| 164 | |||
| 165 | # show version information | ||
| 166 | if(defined $opt_version) | ||
| 167 | { | ||
| 168 | $opt_version=1; | ||
| 169 | &showversion(); | ||
| 170 | exit; | ||
| 171 | } | ||
| 172 | |||
| 173 | # show minimal usage message if no arguments | ||
| 174 | if($#ARGV<0) | ||
| 175 | { | ||
| 176 | &showminimal(); | ||
| 177 | exit; | ||
| 178 | } | ||
| 179 | |||
| 180 | ############################################################################# | ||
| 181 | |||
| 182 | # ================================ | ||
| 183 | # INITIALIZATION AND INPUT | ||
| 184 | # ================================ | ||
| 185 | |||
| 186 | #$0 contains the program name along with | ||
| 187 | #the complete path. Extract just the program | ||
| 188 | #name and use in error messages | ||
| 189 | $0=~s/.*\/(.+)/$1/; | ||
| 190 | |||
| 191 | if(!defined $ARGV[0]) | ||
| 192 | { | ||
| 193 | print STDERR "ERROR($0): | ||
| 194 | Please specify a Senseval-2 formatted Source file...\n"; | ||
| 195 | exit; | ||
| 196 | } | ||
| 197 | #accept input | ||
| 198 | $infile=$ARGV[0]; | ||
| 199 | |||
| 200 | #check if exists | ||
| 201 | if(!-e $infile) | ||
| 202 | { | ||
| 203 | print STDERR "ERROR($0): | ||
| 204 | Source file <$infile> doesn't exist ... \n"; | ||
| 205 | exit; | ||
| 206 | } | ||
| 207 | open(IN,$infile) || die "ERROR($0): | ||
| 208 | Error(code=$!) in opening Source file <$infile>.\n"; | ||
| 209 | |||
| 210 | ############################################################################## | ||
| 211 | |||
| 212 | # ============================== | ||
| 213 | # Get Tag Frequency | ||
| 214 | # ============================== | ||
| 215 | |||
| 216 | $total=0; | ||
| 217 | $instances=0; | ||
| 218 | $histo={}; | ||
| 219 | |||
| 220 | while(<IN>) | ||
| 221 | { | ||
| 222 | if(/instance id=\"([^\"]+)\"/) | ||
| 223 | { | ||
| 224 | if(defined $instance_hash{$1}) | ||
| 225 | { | ||
| 226 | print STDERR "ERROR($0): | ||
| 227 | Instance Id <$1> is repeated in file <$infile>.\n"; | ||
| 228 | } | ||
| 229 | $instances++; | ||
| 230 | $instance_hash{$1}=1; | ||
| 231 | } | ||
| 232 | # get the sense tag | ||
| 233 | if(/sense\s*id=\"([^\"]+)\"/) | ||
| 234 | { | ||
| 235 | $histo{$1}++; | ||
| 236 | $total++; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | # now find percentages | ||
| 241 | foreach (sort keys %histo) | ||
| 242 | { | ||
| 243 | $histo{$_}=sprintf("%2.2f",$histo{$_}/$total*100); | ||
| 244 | print "<sense id=\"$_\" percent=\"$histo{$_}\"\/>\n"; | ||
| 245 | push @sense_distri,$histo{$_}; | ||
| 246 | delete $histo{$_}; | ||
| 247 | } | ||
| 248 | # sort frequency array in descending order | ||
| 249 | @sorted=sort {$b <=> $a} @sense_distri; | ||
| 250 | $distinct=$#sense_distri+1; | ||
| 251 | # display | ||
| 252 | print "Total Instances = $instances\n"; | ||
| 253 | print "Total Distinct Senses=$distinct\nDistribution={".join(",",@sorted)."}\n"; | ||
| 254 | if(defined $sorted[0]) | ||
| 255 | { | ||
| 256 | print "% of Majority Sense = $sorted[0]\n"; | ||
| 257 | } | ||
| 258 | close IN; | ||
| 259 | |||
| 260 | ############################################################################## | ||
| 261 | |||
| 262 | # ========================== | ||
| 263 | # SUBROUTINE SECTION | ||
| 264 | # ========================== | ||
| 265 | |||
| 266 | #----------------------------------------------------------------------------- | ||
| 267 | #show minimal usage message | ||
| 268 | sub showminimal() | ||
| 269 | { | ||
| 270 | print "Usage: frequency.pl [OPTIONS] SOURCE"; | ||
| 271 | print "\nTYPE frequency.pl --help for help\n"; | ||
| 272 | } | ||
| 273 | |||
| 274 | #----------------------------------------------------------------------------- | ||
| 275 | #show help | ||
| 276 | sub showhelp() | ||
| 277 | { | ||
| 278 | print "Usage: frequency.pl [OPTIONS] SOURCE | ||
| 279 | |||
| 280 | Displays total number of instances, total distinct senses, sense distribution | ||
| 281 | and % frequency of majority sense in a given Senseval-2 formatted SOURCE file. | ||
| 282 | |||
| 283 | SOURCE | ||
| 284 | Specify a Senseval-2 formatted file for which sense distribution is | ||
| 285 | to be shown. Sense ids for instances in SOURCE are searched by | ||
| 286 | matching regex /sense\\s*id=\"S\"/. | ||
| 287 | |||
| 288 | OPTIONS: | ||
| 289 | --help | ||
| 290 | Displays this message. | ||
| 291 | --version | ||
| 292 | Displays the version information.\n"; | ||
| 293 | } | ||
| 294 | |||
| 295 | #------------------------------------------------------------------------------ | ||
| 296 | #version information | ||
| 297 | sub showversion() | ||
| 298 | { | ||
| 299 | # print "frequency.pl - Version 0.11"; | ||
| 300 | print '$Id: frequency.pl,v 1.11 2008/03/29 20:52:27 tpederse Exp $'; | ||
| 301 | print "\nDisplay sense distribution of a given Senseval-2 file\n"; | ||
| 302 | # print "\nCopyright (c) 2002-2005, Amruta Purandare, Ted Pedersen.\n"; | ||
| 303 | # print "Date of Last Update: 05/07/2003\n"; | ||
| 304 | } | ||
| 305 | |||
| 306 | ############################################################################# | ||
| 307 |