countfuns.pl 6.94 KB
Newer Older
David BAELDE's avatar
David BAELDE committed
1 2
#!/usr/bin/perl

David BAELDE's avatar
David BAELDE committed
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
# Standard XPATH functions
@stdfuns = (
  "abs",
  "acos",
  "add-dayTimeDurations",
  "add-dayTimeDuration-to-date",
  "add-dayTimeDuration-to-dateTime",
  "add-dayTimeDuration-to-time",
  "add-yearMonthDurations",
  "add-yearMonthDuration-to-date",
  "add-yearMonthDuration-to-dateTime",
  "adjust-dateTime-to-timezone",
  "adjust-date-to-timezone",
  "adjust-time-to-timezone",
  "analyze-string",
  "asin",
  "atan",
  "atan2",
  "available-environment-variables",
  "avg",
  "base64Binary-equal",
  "base-uri",
  "boolean",
  "boolean-equal",
  "boolean-greater-than",
  "boolean-less-than",
  "ceiling",
  "codepoint-equal",
  "codepoints-to-string",
  "collection",
  "compare",
  "concat",
  "concatenate",
  "contains",
  "cos",
  "count",
  "current-date",
  "current-dateTime",
  "current-time",
  "data",
  "date-equal",
  "date-greater-than",
  "date-less-than",
  "dateTime",
  "dateTime-equal",
  "dateTime-greater-than",
  "dateTime-less-than",
  "day-from-date",
  "day-from-dateTime",
  "days-from-duration",
  "dayTimeDuration-greater-than",
  "dayTimeDuration-less-than",
  "deep-equal",
  "default-collation",
  "distinct-values",
  "divide-dayTimeDuration",
  "divide-dayTimeDuration-by-dayTimeDuration",
  "divide-yearMonthDuration",
  "divide-yearMonthDuration-by-yearMonthDuration",
  "doc",
  "doc-available",
  "document-uri",
  "duration-equal",
  "element-with-id",
  "empty",
  "encode-for-uri",
  "ends-with",
  "environment-variable",
  "error",
  "escape-html-uri",
  "exactly-one",
  "except",
  "exists",
  "exp",
  "exp10",
  "false",
  "filter",
  "floor",
  "fold-left",
  "fold-right",
  "for-each",
  "for-each-pair",
  "format-date",
  "format-dateTime",
  "format-integer",
  "format-number",
  "format-time",
  "function-arity",
  "function-lookup",
  "function-name",
  "gDay-equal",
  "generate-id",
  "gMonthDay-equal",
  "gMonth-equal",
  "gYear-equal",
  "gYearMonth-equal",
  "has-children",
  "head",
  "hexBinary-equal",
  "hours-from-dateTime",
  "hours-from-duration",
  "hours-from-time",
  "id",
  "idref",
  "implicit-timezone",
  "index-of",
  "innermost",
  "in-scope-prefixes",
  "insert-before",
  "intersect",
  "iri-to-uri",
  "is-same-node",
  "lang",
  "last",
  "local-name",
  "local-name-from-QName",
  "log",
  "log10",
  "lower-case",
  "matches",
  "max",
  "min",
  "minutes-from-dateTime",
  "minutes-from-duration",
  "minutes-from-time",
  "month-from-date",
  "month-from-dateTime",
  "months-from-duration",
  "multiply-dayTimeDuration",
  "multiply-yearMonthDuration",
  "name",
  "namespace-uri",
  "namespace-uri-for-prefix",
  "namespace-uri-from-QName",
  "nilled",
  "node-after",
  "node-before",
  "node-name",
  "normalize-space",
  "normalize-unicode",
  "not",
  "NOTATION-equal",
  "number",
  "numeric-add",
  "numeric-divide",
  "numeric-equal",
  "numeric-greater-than",
  "numeric-integer-divide",
  "numeric-less-than",
  "numeric-mod",
  "numeric-multiply",
  "numeric-subtract",
  "numeric-unary-minus",
  "numeric-unary-plus",
  "one-or-more",
  "outermost",
  "parse-xml",
  "parse-xml-fragment",
  "path",
  "pi",
  "position",
  "pow",
  "prefix-from-QName",
  "QName",
  "QName-equal",
  "remove",
  "replace",
  "resolve-QName",
  "resolve-uri",
  "reverse",
  "root",
  "round",
  "round-half-to-even",
  "seconds-from-dateTime",
  "seconds-from-duration",
  "seconds-from-time",
  "serialize",
  "sin",
  "sqrt",
  "starts-with",
  "static-base-uri",
  "string",
  "string-join",
  "string-length",
  "string-to-codepoints",
  "subsequence",
  "substring",
  "substring-after",
  "substring-before",
  "subtract-dates",
  "subtract-dateTimes",
  "subtract-dayTimeDuration-from-date",
  "subtract-dayTimeDuration-from-dateTime",
  "subtract-dayTimeDuration-from-time",
  "subtract-dayTimeDurations",
  "subtract-times",
  "subtract-yearMonthDuration-from-date",
  "subtract-yearMonthDuration-from-dateTime",
  "subtract-yearMonthDurations",
  "sum",
  "tail",
  "tan",
  "time-equal",
  "time-greater-than",
  "time-less-than",
  "timezone-from-date",
  "timezone-from-dateTime",
  "timezone-from-time",
  "to",
  "tokenize",
  "trace",
  "translate",
  "true",
  "union",
  "unordered",
  "unparsed-text",
  "unparsed-text-available",
  "unparsed-text-lines",
  "upper-case",
  "uri-collection",
  "year-from-date",
  "year-from-dateTime",
  "yearMonthDuration-greater-than",
  "yearMonthDuration-less-than",
  "years-from-duration",
  "zero-or-one"
);

231 232 233
unless (@ARGV) {
  print <<HELP;
Usage: $0 <XML files>
David BAELDE's avatar
David BAELDE committed
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248
Produces gnuplot data files countfuns_<XPPSUF>{,_std,_nonstd}.dat,
where XPPSUF is set as an env variable.

Issues a detailed output with function names if env variable VERBOSE is set.
HELP
  exit 1;
}

$suffix = $ENV{'XPPSUF'};
$verbose = $ENV{'VERBOSE'};

%table=(); # maps function names to occurrence count
$total=0;  # total nb of occurrences of functions
$nbfuns=0; # nb of functions i.e. size of %table
David BAELDE's avatar
David BAELDE committed
249 250 251 252
for (@ARGV) {
  open(STARLET,"xmlstarlet sel -t -n -v \"//*[local-name()='functionName']\" $_ |");
  while(<STARLET>) {
    chomp;
253 254
    next if $_ eq "not";
    $total++;
David BAELDE's avatar
David BAELDE committed
255 256 257
    if (defined $table{$_}) {
      $table{$_}++;
    } else {
258
      $nbfuns++;
259
      $table{$_}=1;
David BAELDE's avatar
David BAELDE committed
260 261 262 263
    }
  }
}

264 265 266 267 268
if ($verbose ne "") {
  print "Results in ascending number of occurrences:\n";
  for (sort { $table{$a} <=> $table{$b} } keys %table) {
	print "$_: $table{$_}\n";
  }
David BAELDE's avatar
David BAELDE committed
269
}
270

271 272 273 274 275 276 277
print "Generating countfuns_${suffix}{,_std,_nonstd}.dat...\n";
open DAT,">","countfuns_${suffix}.dat"
  or die "Cannot open countfuns_${suffix}.dat!\n";
open STD,">","countfuns_${suffix}_std.dat"
  or die "Cannot open countfuns_${suffix}_std.dat!\n";
open NONSTD,">","countfuns_${suffix}_nonstd.dat"
  or die "Cannot open countfuns_${suffix}_nonstd.dat!\n";
Sylvain Schmitz's avatar
Sylvain Schmitz committed
278 279
open TOTAL,">","countfuns_${suffix}_total.tex"
  or die "Cannot open countfuns_${suffix}_total.tex!\n";
Sylvain Schmitz's avatar
Sylvain Schmitz committed
280 281
open COV,">","countfuns_${suffix}_std_cov.tex"
  or die "Cannot open countfuns_${suffix}_std_cov.tex!\n";
David BAELDE's avatar
David BAELDE committed
282 283 284 285 286 287 288
$n=0;             # rank of current function (decr. order)
$sofar=0;         # total nb of occ. so far
$sofar_std=0;     # total nb of occ. of std funs so far
$target=0.7;      # display nb fun. needed to reach this % of total
$threshold=100;   # display stats about fun. with >$threshold occ.
for $fname (sort { $table{$b} <=> $table{$a} } keys %table) {
  if ($table{$fname}<$threshold) {
David BAELDE's avatar
David BAELDE committed
289 290 291 292 293
    print "* There are $n functions with >=$threshold occurrences,\n";
    printf("  together they account for %.2f%% of occurrences.\n",
      100*($sofar/$total));
    $threshold=0;
  }
294
  $n++;
David BAELDE's avatar
David BAELDE committed
295 296 297
  $sofar+=$table{$fname};
  if ($sofar >= $target*$total) {
    $target=2;
David BAELDE's avatar
David BAELDE committed
298 299
    print
      "* $n functions (out of $nbfuns) needed to cover 70% of occurrences.\n";
300
  }
David BAELDE's avatar
David BAELDE committed
301 302 303 304 305 306 307 308 309
  $percent = 100*$sofar/$total;
  print DAT "$n $table{$fname} $percent\n";
  if (grep { $fname eq $_ } @stdfuns) {
    $sofar_std+=$table{$fname};
    $percent = 100*$sofar_std/$total;
    print STD "$n $table{$fname} $percent\n";
  } else {
    print NONSTD "$n $table{$fname}\n";
  }
310
}
Sylvain Schmitz's avatar
Sylvain Schmitz committed
311 312
$locale_nbfuns = reverse join ',', unpack '(A3)*', reverse $nbfuns;
print TOTAL ("$locale_nbfuns");