-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtext.py
145 lines (123 loc) · 3.82 KB
/
text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
from subprocess import Popen, PIPE
import sys
import nltk
import env
env.assert_valid_env()
def _tokenize(lang_short_code, text):
runner_path = os.path.join(
os.environ['JOSHUA'],
'scripts',
'preparation',
'tokenize.pl'
)
options = ['-l', lang_short_code]
p = Popen(
[runner_path] + options,
stdin=PIPE,
stderr=PIPE,
stdout=PIPE,
env=os.environ
)
out, err = p.communicate(text.encode('utf8'))
sys.stderr.write(err.encode('utf8') + '\n')
return unicode(out.strip(), encoding='utf8').split('\n')
def _detokenize(lang_short_code, text):
runner_path = os.path.join(
os.environ['JOSHUA'],
'scripts',
'preparation',
'detokenize.pl'
)
options = ['-l', lang_short_code]
p = Popen(
[runner_path] + options,
stdin=PIPE,
stderr=PIPE,
stdout=PIPE,
env=os.environ
)
out, err = p.communicate(text.encode('utf8'))
sys.stderr.write(err.encode('utf8') + '\n')
return unicode(out.strip(), encoding='utf8')
def tokenize(lang_short_code, sentences):
"""
Returns a string with tokenized parts separated by a space character
"""
if lang_short_code not in ['en', 'es']:
lang_short_code = 'en'
text = '\n'.join(sentences)
return _tokenize(lang_short_code, text)
def detokenize(sentence):
"""
Returns a string with tokenized parts separated by a space character
"""
pass
class PreProcessor(object):
"""
Prepares raw text for input to a Joshua Decoder:
1. Sentence-splitting
2. tokenization
3. lowercasing
4. joining sentences with '\n'
"""
def __init__(self, lang_aliases):
self._lang = lang_aliases
if lang_aliases.long_english_name != 'arabic':
self._sentence_splitter = nltk.data.load(
'tokenizers/punkt/%s.pickle' % lang_aliases.long_english_name
).tokenize
else:
# a simpler, regular-expression based tokenizer, which splits
# text on whitespace and punctuation.
self._sentence_splitter = nltk.tokenize.wordpunct_tokenize
def prepare(self, text):
paragraphs = text.split('\n')
results = []
for paragraph in paragraphs:
if not paragraph:
results.append('')
continue
sentences = self._sentence_splitter(paragraph)
tokenized_sentences = tokenize(self._lang.short_name, sentences)
lc_tokenized_sentences = [
sent.lower() for sent in tokenized_sentences
]
results.extend(lc_tokenized_sentences)
return '\n'.join(results)
def merge_lines(translation):
"""
Join text in one sentence per line format into paragraph format.
"""
lines = translation.split('\n')
prev_line = ''
result = ''
# The connector after each line depends on the next line.
while lines:
next_line = lines.pop(0)
if prev_line == '':
if next_line == '':
result += u'\n\n'
else:
result += next_line
else:
if next_line == '':
result += u'\n\n'
else:
result = u'{0} {1}'.format(result, next_line)
prev_line = next_line
return result
class PostProcessor(object):
"""
Prepares text returned by the Joshua decoder for response to client
"""
def __init__(self, lang_aliases):
self._lang = lang_aliases
def prepare(self, text):
"""
Expected format of text is one sentence per line
"""
text = _detokenize(self._lang.short_name, text)
lines = text.split('\n')
lines = [line.capitalize() for line in lines]
return merge_lines('\n'.join(lines))