1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """reads a set of .po or .pot files to produce a pootle-terminology.pot
21
22 See: http://translate.sourceforge.net/wiki/toolkit/poterminology for examples and
23 usage instructions
24 """
25 import os
26 import re
27 import sys
28 import logging
29
30 from translate.lang import factory as lang_factory
31 from translate.misc import optrecurse
32 from translate.storage import po
33 from translate.storage import factory
34 from translate.misc import file_discovery
35
36 -def create_termunit(term, unit, targets, locations, sourcenotes, transnotes, filecounts):
37 termunit = po.pounit(term)
38 if unit is not None:
39 termunit.merge(unit, overwrite=False, comments=False)
40 if len(targets.keys()) > 1:
41 txt = '; '.join(["%s {%s}" % (target, ', '.join(files))
42 for target, files in targets.iteritems()])
43 if termunit.target.find('};') < 0:
44 termunit.target = txt
45 termunit.markfuzzy()
46 else:
47
48 termunit.addnote(txt, "translator")
49 for location in locations:
50 termunit.addlocation(location)
51 for sourcenote in sourcenotes:
52 termunit.addnote(sourcenote, "developer")
53 for transnote in transnotes:
54 termunit.addnote(transnote, "translator")
55 for filename, count in filecounts.iteritems():
56 termunit.addnote("(poterminology) %s (%d)\n" % (filename, count), 'translator')
57 return termunit
58
62 self.foldtitle = foldtitle
63 self.ignorecase = ignorecase
64 self.accelchars = accelchars
65 self.termlength = termlength
66
67 self.sourcelanguage = sourcelanguage
68 self.invert = invert
69
70 self.stopwords = {}
71 self.stoprelist = []
72 self.stopfoldtitle = True
73 self.stopignorecase = False
74
75 if stopfile is None:
76 try:
77 stopfile = file_discovery.get_abs_data_filename('stoplist-%s' % self.sourcelanguage)
78 except:
79 pass
80 self.stopfile = stopfile
81 self.parse_stopword_file()
82
83
84 self.formatpat = re.compile(r"%(?:\([^)]+\)|[0-9]+\$)?[-+#0]*[0-9.*]*(?:[hlLzjt][hl])?[EFGXc-ginoprsux]")
85
86 self.xmlelpat = re.compile(r"<(?:![[-]|[/?]?[A-Za-z_:])[^>]*>")
87
88 self.xmlentpat = re.compile(r"&(?:#(?:[0-9]+|x[0-9a-f]+)|[a-z_:][\w.-:]*);",
89 flags=re.UNICODE|re.IGNORECASE)
90
91 self.units = 0
92 self.glossary = {}
93
95
96 actions = { '+': frozenset(), ':': frozenset(['skip']),
97 '<': frozenset(['phrase']), '=': frozenset(['word']),
98 '>': frozenset(['word','skip']),
99 '@': frozenset(['word','phrase']) }
100
101 stopfile = open(self.stopfile, "r")
102 line = 0
103 try:
104 for stopline in stopfile:
105 line += 1
106 stoptype = stopline[0]
107 if stoptype == '#' or stoptype == "\n":
108 continue
109 elif stoptype == '!':
110 if stopline[1] == 'C':
111 self.stopfoldtitle = False
112 self.stopignorecase = False
113 elif stopline[1] == 'F':
114 self.stopfoldtitle = True
115 self.stopignorecase = False
116 elif stopline[1] == 'I':
117 self.stopignorecase = True
118 else:
119 logging.warning("%s line %d - bad case mapping directive", (self.stopfile, line))
120 elif stoptype == '/':
121 self.stoprelist.append(re.compile(stopline[1:-1]+'$'))
122 else:
123 self.stopwords[stopline[1:-1]] = actions[stoptype]
124 except KeyError, character:
125 logging.warning("%s line %d - bad stopword entry starts with", (self.stopfile, line))
126 logging.warning("%s line %d all lines after error ignored", (self.stopfile, line + 1))
127 stopfile.close()
128
130 """returns the cleaned string that contains the text to be matched"""
131 for accelerator in self.accelchars:
132 string = string.replace(accelerator, "")
133 string = self.formatpat.sub(" ", string)
134 string = self.xmlelpat.sub(" ", string)
135 string = self.xmlentpat.sub(" ", string)
136 string = string.strip()
137 return string
138
140 """return case-mapped stopword for input word"""
141 if self.stopignorecase or (self.stopfoldtitle and word.istitle()):
142 word = word.lower()
143 return word
144
146 """return stoplist frozenset for input word"""
147 return self.stopwords.get(self.stopmap(word),defaultset)
148
150 """adds (sub)phrases with non-skipwords and more than one word"""
151 if (len(words) > skips + 1 and
152 'skip' not in self.stopword(words[0]) and
153 'skip' not in self.stopword(words[-1])):
154 self.glossary.setdefault(' '.join(words), []).append(translation)
155 if partials:
156 part = list(words)
157 while len(part) > 2:
158 if 'skip' in self.stopword(part.pop()):
159 skips -= 1
160 if (len(part) > skips + 1 and
161 'skip' not in self.stopword(part[0]) and
162 'skip' not in self.stopword(part[-1])):
163 self.glossary.setdefault(' '.join(part), []).append(translation)
164
166 sourcelang = lang_factory.getlanguage(self.sourcelanguage)
167 rematchignore = frozenset(('word','phrase'))
168 defaultignore = frozenset()
169 for unit in units:
170 self.units += 1
171 if unit.isheader():
172 continue
173 if unit.hasplural():
174 continue
175 if not self.invert:
176 source = self.clean(unit.source)
177 target = self.clean(unit.target)
178 else:
179 target = self.clean(unit.source)
180 source = self.clean(unit.target)
181 if len(source) <= 1:
182 continue
183 for sentence in sourcelang.sentences(source):
184 words = []
185 skips = 0
186 for word in sourcelang.words(sentence):
187 stword = self.stopmap(word)
188 if self.ignorecase or (self.foldtitle and word.istitle()):
189 word = word.lower()
190 ignore = defaultignore
191 if stword in self.stopwords:
192 ignore = self.stopwords[stword]
193 else:
194 for stopre in self.stoprelist:
195 if stopre.match(stword) != None:
196 ignore = rematchignore
197 break
198 translation = (source, target, unit, fullinputpath)
199 if 'word' not in ignore:
200
201 root = word
202 if len(word) > 3 and word[-1] == 's' and word[0:-1] in self.glossary:
203 root = word[0:-1]
204 elif len(root) > 2 and root + 's' in self.glossary:
205 self.glossary[root] = self.glossary.pop(root + 's')
206 self.glossary.setdefault(root, []).append(translation)
207 if self.termlength > 1:
208 if 'phrase' in ignore:
209
210 while len(words) > 2:
211 if 'skip' in self.stopword(words.pop(0)):
212 skips -= 1
213 self.addphrases(words, skips, translation)
214 words = []
215 skips = 0
216 else:
217 words.append(word)
218 if 'skip' in ignore:
219 skips += 1
220 if len(words) > self.termlength + skips:
221 while len(words) > self.termlength + skips:
222 if 'skip' in self.stopword(words.pop(0)):
223 skips -= 1
224 self.addphrases(words, skips, translation)
225 else:
226 self.addphrases(words, skips, translation, partials=False)
227 if self.termlength > 1:
228
229 while self.termlength > 1 and len(words) > 2:
230
231 if 'skip' in self.stopword(words.pop(0)):
232 skips -= 1
233 self.addphrases(words, skips, translation)
234
238 terms = {}
239 locre = re.compile(r":[0-9]+$")
240 print >> sys.stderr, ("%d terms from %d units" %
241 (len(self.glossary), self.units))
242 for term, translations in self.glossary.iteritems():
243 if len(translations) <= 1:
244 continue
245 filecounts = {}
246 sources = set()
247 locations = set()
248 sourcenotes = set()
249 transnotes = set()
250 targets = {}
251 fullmsg = False
252 bestunit = None
253 for source, target, unit, filename in translations:
254 sources.add(source)
255 filecounts[filename] = filecounts.setdefault(filename, 0) + 1
256
257 if term.lower() == self.clean(unit.source).lower():
258 fullmsg = True
259 target = self.clean(unit.target)
260 if self.ignorecase or (self.foldtitle and target.istitle()):
261 target = target.lower()
262 unit.target = target
263 if target != "":
264 targets.setdefault(target, []).append(filename)
265 if term.lower() == unit.source.strip().lower():
266 sourcenotes.add(unit.getnotes("source code"))
267 transnotes.add(unit.getnotes("translator"))
268 unit.source = term
269 bestunit = unit
270
271
272 for loc in unit.getlocations():
273 locations.add(locre.sub("", loc))
274
275 numsources = len(sources)
276 numfiles = len(filecounts)
277 numlocs = len(locations)
278 if numfiles < inputmin or numlocs < locmin:
279 continue
280 if fullmsg:
281 if numsources < fullmsgmin:
282 continue
283 elif numsources < substrmin:
284 continue
285
286 locmax = 2 * locmin
287 if numlocs > locmax:
288 locations = list(locations)[0:locmax]
289 locations.append("(poterminology) %d more locations"
290 % (numlocs - locmax))
291
292 termunit = create_termunit(term, bestunit, targets, locations,
293 sourcenotes, transnotes, filecounts)
294 terms[term] = ((10 * numfiles) + numsources, termunit)
295
296 termlist = terms.keys()
297 print >> sys.stderr, "%d terms after thresholding" % len(termlist)
298 termlist.sort(lambda x, y: cmp(len(x), len(y)))
299 for term in termlist:
300 words = term.split()
301 if len(words) <= 2:
302 continue
303 while len(words) > 2:
304 words.pop()
305 if terms[term][0] == terms.get(' '.join(words), [0])[0]:
306 del terms[' '.join(words)]
307 words = term.split()
308 while len(words) > 2:
309 words.pop(0)
310 if terms[term][0] == terms.get(' '.join(words), [0])[0]:
311 del terms[' '.join(words)]
312 print >> sys.stderr, "%d terms after subphrase reduction" % len(terms.keys())
313 termitems = terms.values()
314 while len(sortorders) > 0:
315 order = sortorders.pop()
316 if order == "frequency":
317 termitems.sort(lambda x, y: cmp(y[0], x[0]))
318 elif order == "dictionary":
319 termitems.sort(lambda x, y: cmp(x[1].source.lower(), y[1].source.lower()))
320 elif order == "length":
321 termitems.sort(lambda x, y: cmp(len(x[1].source), len(y[1].source)))
322 else:
323 logging.warning("unknown sort order %s", order)
324 return termitems
325
326
328 """a specialized Option Parser for the terminology tool..."""
329
331 """parses the command line options, handling implicit input/output args"""
332 (options, args) = optrecurse.optparse.OptionParser.parse_args(self, args, values)
333
334 if args and not options.input:
335 if not options.output and not options.update and len(args) > 1:
336 options.input = args[:-1]
337 args = args[-1:]
338 else:
339 options.input = args
340 args = []
341
342
343 if args and not options.output and not options.update:
344 if os.path.lexists(args[-1]) and not os.path.isdir(args[-1]):
345 self.error("To overwrite %s, specify it with -o/--output or -u/--update" % (args[-1]))
346 options.output = args[-1]
347 args = args[:-1]
348 if options.output and options.update:
349 self.error("You cannot use both -u/--update and -o/--output")
350 if args:
351 self.error("You have used an invalid combination of -i/--input, -o/--output, -u/--update and freestanding args")
352 if not options.input:
353 self.error("No input file or directory was specified")
354 if isinstance(options.input, list) and len(options.input) == 1:
355 options.input = options.input[0]
356 if options.inputmin == None:
357 options.inputmin = 1
358 elif not isinstance(options.input, list) and not os.path.isdir(options.input):
359 if options.inputmin == None:
360 options.inputmin = 1
361 elif options.inputmin == None:
362 options.inputmin = 2
363 if options.update:
364 options.output = options.update
365 if isinstance(options.input, list):
366 options.input.append(options.update)
367 elif options.input:
368 options.input = [options.input, options.update]
369 else:
370 options.input = options.update
371 if not options.output:
372 options.output = "pootle-terminology.pot"
373 return (options, args)
374
376 """sets the usage string - if usage not given, uses getusagestring for each option"""
377 if usage is None:
378 self.usage = "%prog " + " ".join([self.getusagestring(option) for option in self.option_list]) + \
379 "\n input directory is searched for PO files, terminology PO file is output file"
380 else:
381 super(TerminologyOptionParser, self).set_usage(usage)
382
395
397 """recurse through directories and process files"""
398 if self.isrecursive(options.input, 'input') and getattr(options, "allowrecursiveinput", True):
399 if isinstance(options.input, list):
400 inputfiles = self.recurseinputfilelist(options)
401 else:
402 inputfiles = self.recurseinputfiles(options)
403 else:
404 if options.input:
405 inputfiles = [os.path.basename(options.input)]
406 options.input = os.path.dirname(options.input)
407 else:
408 inputfiles = [options.input]
409 if os.path.isdir(options.output):
410 options.output = os.path.join(options.output,"pootle-terminology.pot")
411
412 self.initprogressbar(inputfiles, options)
413 for inputpath in inputfiles:
414 self.files += 1
415 fullinputpath = self.getfullinputpath(options, inputpath)
416 success = True
417 try:
418 self.processfile(None, options, fullinputpath)
419 except Exception, error:
420 if isinstance(error, KeyboardInterrupt):
421 raise
422 self.warning("Error processing: input %s" % (fullinputpath), options, sys.exc_info())
423 success = False
424 self.reportprogress(inputpath, success)
425 del self.progressbar
426 self.outputterminology(options)
427
428 - def processfile(self, fileprocessor, options, fullinputpath):
433
435 """saves the generated terminology glossary"""
436 termfile = po.pofile()
437 print >> sys.stderr, ("scanned %d files" % self.files)
438 termitems = self.extractor.extract_terms(inputmin=options.inputmin, fullmsgmin=options.fullmsgmin,
439 substrmin=options.substrmin, locmin=options.locmin, sortorders=options.sortorders)
440 for count, unit in termitems:
441 termfile.units.append(unit)
442 open(options.output, "w").write(str(termfile))
443
445 parser.values.ignorecase = False
446 parser.values.foldtitle = True
447
449 parser.values.ignorecase = parser.values.foldtitle = False
450
452 formats = {"po":("po", None), "pot": ("pot", None), None:("po", None)}
453 parser = TerminologyOptionParser(formats)
454
455 parser.add_option("-u", "--update", type="string", dest="update",
456 metavar="UPDATEFILE", help="update terminology in UPDATEFILE")
457
458 parser.add_option("-S", "--stopword-list", type="string", metavar="STOPFILE", dest="stopfile",
459 help="read stopword (term exclusion) list from STOPFILE (default %s)" %
460 file_discovery.get_abs_data_filename('stoplist-en'))
461
462 parser.set_defaults(foldtitle = True, ignorecase = False)
463 parser.add_option("-F", "--fold-titlecase", callback=fold_case_option,
464 action="callback", help="fold \"Title Case\" to lowercase (default)")
465 parser.add_option("-C", "--preserve-case", callback=preserve_case_option,
466 action="callback", help="preserve all uppercase/lowercase")
467 parser.add_option("-I", "--ignore-case", dest="ignorecase",
468 action="store_true", help="make all terms lowercase")
469
470 parser.add_option("", "--accelerator", dest="accelchars", default="",
471 metavar="ACCELERATORS", help="ignores the given accelerator characters when matching")
472
473 parser.add_option("-t", "--term-words", type="int", dest="termlength", default="3",
474 help="generate terms of up to LENGTH words (default 3)", metavar="LENGTH")
475 parser.add_option("", "--inputs-needed", type="int", dest="inputmin",
476 help="omit terms appearing in less than MIN input files (default 2, or 1 if only one input file)", metavar="MIN")
477 parser.add_option("", "--fullmsg-needed", type="int", dest="fullmsgmin", default="1",
478 help="omit full message terms appearing in less than MIN different messages (default 1)", metavar="MIN")
479 parser.add_option("", "--substr-needed", type="int", dest="substrmin", default="2",
480 help="omit substring-only terms appearing in less than MIN different messages (default 2)", metavar="MIN")
481 parser.add_option("", "--locs-needed", type="int", dest="locmin", default="2",
482 help="omit terms appearing in less than MIN different original source files (default 2)", metavar="MIN")
483
484 sortorders_default = [ "frequency", "dictionary", "length" ]
485 parser.add_option("", "--sort", dest="sortorders", action="append",
486 type="choice", choices=sortorders_default, metavar="ORDER", default=sortorders_default,
487 help="output sort order(s): %s (default is all orders in the above priority)" % ', '.join(sortorders_default))
488
489 parser.add_option("", "--source-language", dest="sourcelanguage", default="en",
490 help="the source language code (default 'en')", metavar="LANG")
491 parser.add_option("-v", "--invert", dest="invert",
492 action="store_true", default=False, help="invert the source and target languages for terminology")
493 parser.set_usage()
494 parser.description = __doc__
495 parser.run()
496
497
498 if __name__ == '__main__':
499 main()
500