1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Module to provide statistics and related functionality.
23
24 @organization: Zuza Software Foundation
25 @copyright: 2007 Zuza Software Foundation
26 @license: U{GPL <http://www.fsf.org/licensing/licenses/gpl.html>}
27 """
28
29 from translate import lang
30 from translate.lang import factory
31
32
33
34
35
36
37
38
39
40
41
42
44 """Manages statistics for storage objects."""
45
46 - def __init__(self, sourcelanguage='en', targetlanguage='en', checkerstyle=None):
47 self.sourcelanguage = sourcelanguage
48 self.targetlanguage = targetlanguage
49 self.language = lang.factory.getlanguage(self.sourcelanguage)
50
51
52 self.classification = {}
53
59
61 """Return a list of fuzzy units."""
62 if not self.classification:
63 self.classifyunits()
64 units = self.getunits()
65 return [units[item] for item in self.classification["fuzzy"]]
66
68 """Returns the number of fuzzy units."""
69 return len(self.fuzzy_units())
70
72 """Return a list of translated units."""
73 if not self.classification:
74 self.classifyunits()
75 units = self.getunits()
76 return [units[item] for item in self.classification["translated"]]
77
79 """Returns the number of translated units."""
80 return len(self.translated_units())
81
83 """Return a list of untranslated units."""
84 if not self.classification:
85 self.classifyunits()
86 units = self.getunits()
87 return [units[item] for item in self.classification["blank"]]
88
90 """Returns the number of untranslated units."""
91
92 return len(self.untranslated_units())
93
95 """Returns a list of all units in this object."""
96 return []
97
98 - def get_source_text(self, units):
99 """Joins the unit source strings in a single string of text."""
100 source_text = ""
101 for unit in units:
102 source_text += unit.source + "\n"
103 plurals = getattr(unit.source, "strings", [])
104 if plurals:
105 source_text += "\n".join(plurals[1:])
106 return source_text
107
109 """Returns the number of words in the given text."""
110 return len(self.language.words(text))
111
116
122
128
152
154 """Makes a dictionary of which units fall into which classifications.
155
156 This method iterates over all units.
157 """
158 self.classification = {}
159 self.classification["fuzzy"] = []
160 self.classification["blank"] = []
161 self.classification["translated"] = []
162 self.classification["has-suggestion"] = []
163 self.classification["total"] = []
164
165
166 for item, unit in enumerate(self.unit_iter()):
167 classes = self.classifyunit(unit)
168
169
170 for classname in classes:
171 if classname in self.classification:
172 self.classification[classname].append(item)
173 else:
174 self.classification[classname] = item
175 self.countwords()
176
178 """Counts the source and target words in each of the units."""
179 self.sourcewordcounts = []
180 self.targetwordcounts = []
181 for unit in self.unit_iter():
182 self.sourcewordcounts.append([self.wordcount(text) for text in getattr(unit.source, "strings", [""])])
183 self.targetwordcounts.append([self.wordcount(text) for text in getattr(unit.target, "strings", [""])])
184
186 """Updates the classification of a unit in self.classification.
187
188 @param item: an integer that is an index in .getunits().
189 """
190 unit = self.getunits()[item]
191 self.sourcewordcounts[item] = [self.wordcount(text) for text in unit.source.strings]
192 self.targetwordcounts[item] = [self.wordcount(text) for text in unit.target.strings]
193 classes = self.classifyunit(unit)
194
195
196 for classname, matchingitems in self.classification.items():
197 if (classname in classes) != (item in matchingitems):
198 if classname in classes:
199 self.classification[classname].append(item)
200 else:
201 self.classification[classname].remove(item)
202 self.classification[classname].sort()
203
204