jordimas commited on
Commit
217f640
1 Parent(s): 3fa91d6

Update scripts

Browse files
Files changed (4) hide show
  1. raw/convert.sh +6 -0
  2. raw/po-to-tsv.py +82 -0
  3. raw/requirements.txt +1 -0
  4. raw/txt-to-tsv.py +0 -36
raw/convert.sh ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FILE=tots-tm.po.zip
2
+ rm -r $FILE
3
+ wget https://static.softcatala.org/memories/$FILE
4
+ unzip -o $FILE tots-tm.po
5
+
6
+ python3 po-to-tsv.py
raw/po-to-tsv.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import polib
2
+ import re
3
+
4
+ def _remove_accelerators(result):
5
+ CHARS = (
6
+ '_', '&', '~' # Accelerators.
7
+ )
8
+ for c in CHARS:
9
+ result = result.replace(c, '')
10
+
11
+ return result.strip()
12
+
13
+ def _remove_tags(text):
14
+ clean = re.sub("<[^>]*>", "", text)
15
+ return clean
16
+
17
+ def _is_non_localized_string(src, trg):
18
+ words_src_len = len(src.split())
19
+ words_trg_len = len(trg.split())
20
+ if words_src_len > 2 and words_trg_len > 2:
21
+ return src == trg
22
+
23
+ return False
24
+
25
+ def _is_invalid(src, trg):
26
+ if len(src) < 2 or len(trg) < 2:
27
+ return True
28
+
29
+ if '\n' in src or '\n' in trg:
30
+ return True
31
+
32
+ if '@@image' in src or '@@image' in trg:
33
+ return True
34
+
35
+ if _is_non_localized_string(src, trg):
36
+ return True
37
+
38
+ return False
39
+
40
+
41
+ def generate_tsv(po_filename, output_filename):
42
+
43
+ SEPARATOR = '\t'
44
+
45
+ input_po = polib.pofile(po_filename)
46
+ entries = 0
47
+ words = 0
48
+
49
+ with open(po_filename, "r") as source,\
50
+ open(output_filename, "w") as output:
51
+
52
+ for entry in input_po:
53
+ src = _remove_accelerators(entry.msgid)
54
+ trg = _remove_accelerators(entry.msgstr)
55
+
56
+ src = _remove_tags(src)
57
+ trg = _remove_tags(trg)
58
+
59
+ if _is_invalid(src, trg):
60
+ continue
61
+
62
+ if SEPARATOR in src or SEPARATOR in trg:
63
+ continue
64
+
65
+ src = src.strip()
66
+ trg = trg.strip()
67
+
68
+
69
+ output.write(f"{src}{SEPARATOR}{trg}\n")
70
+ words += len(src.split())
71
+ entries += 1
72
+
73
+ print(f"Generated {entries} entries with {words}")
74
+
75
+
76
+ def main():
77
+
78
+ print("Generate TSV from a PO file")
79
+ generate_tsv("tots-tm.po", "MemoriesProjectesLliures.tsv")
80
+
81
+ if __name__ == "__main__":
82
+ main()
raw/requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ polib
raw/txt-to-tsv.py DELETED
@@ -1,36 +0,0 @@
1
-
2
-
3
-
4
-
5
- def generate_tsv(source_filename, target_filename, output_filename):
6
-
7
- SEPARATOR = '\t'
8
-
9
- with open(source_filename, "r") as source,\
10
- open(target_filename, "r") as target,\
11
- open(output_filename, "w") as output:
12
-
13
- while True:
14
-
15
- src = source.readline()
16
- trg = target.readline()
17
-
18
- if not (src and trg):
19
- break
20
-
21
- if SEPARATOR in src or SEPARATOR in trg:
22
- continue
23
-
24
- src = src.strip()
25
- trg = trg.strip()
26
-
27
- output.write(f"{src}{SEPARATOR}{trg}\n")
28
-
29
-
30
- def main():
31
-
32
- print("Generate TSV from a two text files")
33
- generate_tsv("MemoriesProjectesLliures.en-ca.en", "MemoriesProjectesLliures.en-ca.ca", "MemoriesProjectesLliures.tsv")
34
-
35
- if __name__ == "__main__":
36
- main()