/usr/bin/tegaki-stats is in python-tegakitools 0.3.1-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2009 The Tegaki project contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Contributors to this file:
# - Mathieu Blondel
import sys
import os
from optparse import OptionParser
from tegaki.charcol import CharacterCollection
from tegakitools.charcol import *
VERSION = '0.3.1'
class TegakiStatsError(Exception):
pass
class TegakiStats(object):
"""
Show various statistics about a character collection.
"""
def __init__(self, options, args):
self._directories = options.directories
self._charcols = options.charcols
self._databases = options.databases
self._tomoe = options.tomoe
self._kuchibue = options.kuchibue
self._include = options.include
self._exclude = options.exclude
self._max_samples = options.max_samples
self._verbosity_level = options.verbosity_level
def run(self):
charcol = get_aggregated_charcol(
((TYPE_CHARCOL, self._charcols),
(TYPE_CHARCOL_DB, self._databases),
(TYPE_DIRECTORY, self._directories),
(TYPE_TOMOE, self._tomoe),
(TYPE_KUCHIBUE, self._kuchibue)))
charcol.include_characters_from_files(self._include)
charcol.exclude_characters_from_files(self._exclude)
# max samples
if self._max_samples:
charcol.remove_samples(keep_at_most=self._max_samples)
if charcol.get_total_n_characters() == 0:
raise TegakiStatsError, "No character collection provided " + \
"or character collections are empty!"
print "Number of sets: %d" % charcol.get_n_sets()
samp_by_class = self._get_samples_by_class(charcol)
# the number of sets and the number of different characters (classes)
# are the same if sets are used to group characters by class
print "Number of different characters: ", len(samp_by_class)
print "Total number of samples: ", charcol.get_total_n_characters()
print "Total number of strokes: ", charcol.get_total_n_strokes()
n_samples = samp_by_class.values()
avg = float(sum(n_samples)) / len(n_samples)
print "Average number of samples per character/class: %0.2f" % avg
if self._verbosity_level >= 2:
print "\nNumber of samples for each character:"
for utf8, n_chars in samp_by_class.items():
print "%s: %d" % (utf8, n_chars)
print "\n"
classes_by_sc = self._get_classes_by_stroke_count(charcol)
n_classes = [len(classes_by_sc[k]) for k in classes_by_sc.keys()]
avg = float(sum(n_classes)) / len(n_classes)
print "Average number of chars/classes per stroke count: %0.2f" % avg
if self._verbosity_level >= 1:
print "\nCharacters found for each stroke count:"
for sc in sorted(classes_by_sc.keys()):
print "%d: %s" % (sc, ", ".join(classes_by_sc[sc]))
print "\n"
sc_by_class = self._get_stroke_counts_by_class(charcol)
n_sc = [len(sc_by_class[k]) for k in sc_by_class.keys()]
avg = float(sum(n_sc)) / len(n_sc)
print "Average number of stroke counts per char/class: %0.2f" % avg
if self._verbosity_level >= 2:
print "\nDifferent stroke counts for each character:"
for utf8, stroke_counts in sc_by_class.items():
stroke_counts = [str(i) for i in sorted(stroke_counts)]
print "%s: %s" % (utf8, ", ".join(stroke_counts))
print "\n"
def _get_samples_by_class(self, charcol):
d = {}
for set_name in charcol.get_set_list():
for row in charcol.get_character_rows(set_name):
utf8 = row['utf8'].encode("utf8")
d[utf8] = d.get(utf8, 0) + 1
return d
def _get_classes_by_stroke_count(self, charcol):
d = {}
for set_name in charcol.get_set_list():
for row in charcol.get_character_rows(set_name):
n_strokes = row['n_strokes']
utf8 = row['utf8'].encode("utf8")
d[n_strokes] = d.get(n_strokes, [])
if not utf8 in d[n_strokes]:
d[n_strokes].append(utf8)
return d
def _get_stroke_counts_by_class(self, charcol):
d = {}
for set_name in charcol.get_set_list():
for row in charcol.get_character_rows(set_name):
n_strokes = row['n_strokes']
utf8 = row['utf8'].encode("utf8")
d[utf8] = d.get(utf8, [])
if not n_strokes in d[utf8]:
d[utf8].append(n_strokes)
return d
parser = OptionParser(usage="usage: %prog [options]",
version="%prog " + VERSION)
parser.add_option("-d", "--directory",
action="append", type="string", dest="directories",
default=[],
help="Directory containing individual XML character files")
parser.add_option("-c", "--charcol",
action="append", type="string", dest="charcols",
default=[],
help="character collection XML files")
parser.add_option("-b", "--db",
action="append", type="string", dest="databases",
default=[],
help="character collection XML files")
parser.add_option("-t", "--tomoe-dict",
action="append", type="string", dest="tomoe",
default=[],
help="Tomoe XML dictionary files")
parser.add_option("-k", "--kuchibue",
action="append", type="string", dest="kuchibue",
default=[],
help="Kuchibue unipen database")
parser.add_option("-i", "--include",
action="append", type="string", dest="include",
default=[],
help="File containing characters to include")
parser.add_option("-e", "--exclude",
action="append", type="string", dest="exclude",
default=[],
help="File containing characters to exclude")
parser.add_option("-m", "--max-samples",
type="int", dest="max_samples",
help="Maximum number of samples per character")
parser.add_option("-v", "--verbosity-level",
type="int", dest="verbosity_level", default=0,
help="verbosity level between 0 and 2")
(options, args) = parser.parse_args()
try:
TegakiStats(options, args).run()
except TegakiStatsError, e:
sys.stderr.write(str(e) + "\n\n")
parser.print_help()
sys.exit(1)
|