File size: 6,313 Bytes
4a08ba7 |
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 196 197 198 199 200 201 202 203 |
#!/usr/bin/env python3
import re
import argparse
import sys
import csv
import traceback
from collections import deque, defaultdict
from math import inf
import translate04 as translate
from contextRep import contextRep
def oneNyield(item, iterable):
yield item
for item in iterable:
yield item
def getRep(fobj, a2ipa, minfreq=1):
ret = contextRep()
stats = {"nlines": 0,
"skipped": 0,
"missing": 0,
"@words": dict()}
ret.wordlist = deque()
ret.stats = stats
finalnr = re.compile("[\r\n]*$")
##
## Try to understand file type
##
sniffLine = fobj.readline()
sniffLine = re.sub(finalnr, "", sniffLine)
if sniffLine.find("\t") >= 0 and len(sniffLine.split("\t")) == 2:
sep = "\t"
elif sniffLine.find(",") >= 0 and len(sniffLine.split(",")) == 2:
sep = ","
elif sniffLine.find(" ") >= 0 and len(sniffLine.split(" ")) == 2:
sep = " "
else:
print("Could not understand frequencies file, not proceeding")
return ret
##
## Try to figure whether there are headers
##
try:
int(sniffLine.split(sep)[-1])
lines = oneNyield(sniffLine, fobj)
except ValueError:
lines = fobj
for line in lines:
stats["nlines"] += 1
line = re.sub(finalnr, "", line)
try:
##
## parse line
##
(word, freq) = line.split(sep)
freq = int(freq)
##
## ignore low frequencies
##
if freq < minfreq:
stats["skipped"] += 1
continue
##
## Translate
##
translation = a2ipa.translate(word)
if "@" in translation:
stats["missing"] += 1
stats["@words"][word] = {"freq":freq, "translation":translation}
##
## Add context to representation
##
else:
ret.wordlist.append({"word:": word, "translation": translation})
ret.add(translation, freq)
except Exception as err:
print("Error in word frequency parsing. Offending line is {}, the message is: {}".format(repr(line), err), file=sys.stderr)
exit(1)
##ret.precalc()
##for (wx, wordprops) in enumerate(ret.wordlist):
## ret.wordlist[wx]["probs"] = ret.probs(wordprops["translation"])
return ret
def main(argv):
parser = argparse.ArgumentParser("Provide summary statistics for language and frequency files")
##
## Specifies the rules used for trsnslation
##
parser.add_argument("-l", "--langrules", dest="langrules",
type=argparse.FileType('r', encoding="utf8"),
required=True,
help="language code rules file")
##
## Specifies a verifcation file, in any csv format. Headers are
## not expected. The first columns is supposed to be a word, and
## the second is its ideal translation
##
parser.add_argument("-c", "--check", dest="check",
default=None, type=argparse.FileType('r', encoding="utf8"),
help="file to use for verification")
##
## Allows to read date from some file (which should not be compressed)
##
parser.add_argument("-r", "--read", dest="read",
default=sys.stdin,
type=argparse.FileType('r', encoding="utf8"),
help="file used for translation (word and frequency)")
##
## Add min frequency
##
parser.add_argument("-m", "--min", dest="min",
default=1,
type=int,
help="minimal frequency to consider")
##
## Print summary?
##
parser.add_argument("-N", "--no-summary", dest="nosummary",
default=False, action="store_true",
help="suppress summary information")
##
## Print all probs?
##
parser.add_argument("-A", "--all-words", dest="allwords",
default=False, action="store_false",
help="Enumerate all words and probabilities")
##
## How many @ words?
##
parser.add_argument("-@", "--max@", dest="max@",
default=10,
type=int,
help="number of @ words to include in summary")
options = vars(parser.parse_args(argv))
a2ipa = translate.alphabet2ipa(options["langrules"])
if "check" in options and not options["check"] is None:
allGood = a2ipa.check(options["check"])
if not allGood:
print("Verification failed, not processing additional data", file=sys.stderr)
exit(1)
##ret = ((word, " ".join(a2ipa.translate(word)))
## for word in words)
rep = getRep(options["read"], a2ipa, minfreq=options["min"])
rep.precalc()
(info, counts) = rep.informativity_counts()
print("seg\tinformativity\t,count")
for (count, seg) in sorted((-counts[seg], seg) for seg in counts):
print("{seg}\t{info}\t{count}".format(seg=seg,
info=info[seg],
count=-count))
##
## Print summary information, if not suppressed
#3
if not options["nosummary"]:
print("## Summary statistics:")
print("## processed (inc. skipped):", rep.stats["nlines"])
print("## skipped:", rep.stats["skipped"])
print("## %@ words:", round(rep.stats["missing"] /
(rep.stats["nlines"] - rep.stats["skipped"])*100, 1))
atwords = rep.stats["@words"]
print("## Top missing:")
for (nfreq, word, translation) in sorted((-atwords[word]["freq"], word, atwords[word]["translation"]) for word in atwords)[:options["max@"]]:
print("## {word} → '{translation}' ({freq})".format(word=word, translation=" ".join(translation), freq=-nfreq))
if __name__ == "__main__":
main(sys.argv[1:])
|