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
43
45 """Manages statistics for storage objects."""
46
47 - def __init__(self, sourcelanguage='en', targetlanguage='en', checkerstyle=None):
54
60
62 """Return a list of fuzzy units."""
63 if not self.classification:
64 self.classifyunits()
65 units = self.getunits()
66 return [units[item] for item in self.classification["fuzzy"]]
67
69 """Returns the number of fuzzy units."""
70 return len(self.fuzzy_units())
71
73 """Return a list of translated units."""
74 if not self.classification:
75 self.classifyunits()
76 units = self.getunits()
77 return [units[item] for item in self.classification["translated"]]
78
80 """Returns the number of translated units."""
81 return len(self.translated_units())
82
84 """Return a list of untranslated units."""
85 if not self.classification:
86 self.classifyunits()
87 units = self.getunits()
88 return [units[item] for item in self.classification["blank"]]
89
91 """Returns the number of untranslated units."""
92
93 return len(self.untranslated_units())
94
96 """Returns a list of all units in this object."""
97 return []
98
99 - def get_source_text(self, units):
100 """Joins the unit source strings in a single string of text."""
101 source_text = ""
102 for unit in units:
103 source_text += unit.source + "\n"
104 plurals = getattr(unit.source, "strings", [])
105 if plurals:
106 source_text += "\n".join(plurals[1:])
107 return source_text
108
110 """Returns the number of words in the given text."""
111 return len(self.language.words(text))
112
117
123
129
131 """Returns a list of the classes that the unit belongs to.
132
133 @param unit: the unit to classify
134 """
135 classes = ["total"]
136 if unit.isfuzzy():
137 classes.append("fuzzy")
138 if unit.gettargetlen() == 0:
139 classes.append("blank")
140 if unit.istranslated():
141 classes.append("translated")
142
143 source = unit.source
144 target = unit.target
145 if isinstance(source, str) and isinstance(target, unicode):
146 source = source.decode(getattr(unit, "encoding", "utf-8"))
147
148
149 checkresult = {}
150 for checkname, checkmessage in checkresult.iteritems():
151 classes.append("check-" + checkname)
152 return classes
153
155 """Makes a dictionary of which units fall into which classifications.
156
157 This method iterates over all units.
158 """
159 self.classification = {}
160 self.classification["fuzzy"] = []
161 self.classification["blank"] = []
162 self.classification["translated"] = []
163 self.classification["has-suggestion"] = []
164 self.classification["total"] = []
165
166
167 for item, unit in enumerate(self.unit_iter()):
168 classes = self.classifyunit(unit)
169
170
171 for classname in classes:
172 if classname in self.classification:
173 self.classification[classname].append(item)
174 else:
175 self.classification[classname] = item
176 self.countwords()
177
179 """Counts the source and target words in each of the units."""
180 self.sourcewordcounts = []
181 self.targetwordcounts = []
182 for unit in self.unit_iter():
183 self.sourcewordcounts.append([self.wordcount(text) for text in getattr(unit.source, "strings", [""])])
184 self.targetwordcounts.append([self.wordcount(text) for text in getattr(unit.target, "strings", [""])])
185
187 """Updates the classification of a unit in self.classification.
188
189 @param item: an integer that is an index in .getunits().
190 """
191 unit = self.getunits()[item]
192 self.sourcewordcounts[item] = [self.wordcount(text) for text in unit.source.strings]
193 self.targetwordcounts[item] = [self.wordcount(text) for text in unit.target.strings]
194 classes = self.classifyunit(unit)
195
196
197 for classname, matchingitems in self.classification.items():
198 if (classname in classes) != (item in matchingitems):
199 if classname in classes:
200 self.classification[classname].append(item)
201 else:
202 self.classification[classname].remove(item)
203 self.classification[classname].sort()
204
205