1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """A set of autocorrect functions that fix common punctuation and space problems automatically"""
22
23 from translate.filters import decoration
24
26 """Runs a set of easy and automatic corrections
27
28 Current corrections include:
29 - Ellipses - align target to use source form of ellipses (either three dots or the Unicode ellipses characters)
30 - Missing whitespace and start or end of the target
31 - Missing punction (.:?) at the end of the target
32 """
33 assert isinstance(source, unicode)
34 assert isinstance(target, unicode)
35 if target == "":
36 return target
37 if "..." in source and u"…" in target:
38 return target.replace(u"…", "...")
39 if u"…" in source and "..." in target:
40 return target.replace("...", u"…")
41 if decoration.spacestart(source) != decoration.spacestart(target) or decoration.spaceend(source) != decoration.spaceend(target):
42 return decoration.spacestart(source) + target.strip() + decoration.spaceend(source)
43 punctuation = (".", ":", ". ", ": ", "?")
44 puncendid = decoration.puncend(source, punctuation)
45 puncendstr = decoration.puncend(target, punctuation)
46 if puncendid != puncendstr:
47 if not puncendstr:
48 return target + puncendid
49 if source[:1].isalpha() and target[:1].isalpha():
50 if source[:1].isupper() and target[:1].islower():
51 return target[:1].upper() + target[1:]
52 elif source[:1].islower() and target[:1].isupper():
53 return target[:1].lower() + target[1:]
54 return None
55