Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DIMputs.py « DIMwid « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03589c3b05d1579e7dcee7f868ef5338e2f9e0a1 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# -*- coding: utf-8 -*-

import collections
import re


class DataInput():
    def __init__(self, file_name):
        self.file = open(file_name, "r")
        self.sentences = None

        
    def read_phrase(self):
        self.sentences = []
        sentence = None
        span_reg = re.compile("\|[0-9]+-[0-9]+\|")
        previous = ""
        for line in self.file:
            sentence = Single()
            for word in line.split():
                if span_reg.match(word):
                    sentence.spans[tuple([int(i) for i in word.strip("|").split("-")])] = previous.strip()
                    previous = " "
                else:
                    previous += word + " "
            sentence.set_length()
            self.sentences.append(sentence)
            sentence.number = len(self.sentences)

    def read_syntax(self):
        self.sentences = []
        sentence = None
        number = -1
        for line in self.file:
            if int(line.split()[2]) != number:
                if sentence is not None:
                    sentence.set_length()
                    self.sentences.append(sentence)
                sentence = Single()
                sentence.number = int(line.split()[2])
                number = sentence.number
            sentence.spans[tuple([int(i) for i in line.split()[3].strip(":[]").split("..")])] \
 = line.strip()

        if sentence is not None:
            sentence.set_length()
            self.sentences.append(sentence)
                # = tuple([line.split(":")[1], line.split(":")[2], line.split(":")[3]])
                
    
    def read_syntax_cubes(self, cell_limit):
        self.sentences = []
        sentence = None
        number = -1
        new_item = False
        for line in self.file:
            if  line.startswith("Chart Cell"):
                pass  # we dont care for those lines
            elif line.startswith("---------"):
                new_item = True
            elif line.startswith("Trans Opt") and new_item is True:
                new_item = False
                if int(line.split()[2]) != number:
                    if sentence is not None:
                        sentence.set_length()
                        self.sentences.append(sentence)
                    sentence = Multiple()
                    sentence.number = int(line.split()[2])
                    number = sentence.number
                span = tuple([int(i) for i in line.split()[3].strip(":[]").split("..")])                
                if len(sentence.spans[span]) < cell_limit:
                    sentence.spans[span].append(line.strip())
        if sentence is not None:
            sentence.set_length()
            self.sentences.append(sentence)
    
    def read_phrase_stack_flag(self, cell_limit):
        self.sentences = []
        sentence = None
        number = -1
        for line in self.file:
            if len(line.split()) < 6:
                pass
#            elif re.match("recombined=[0-9]+", line.split()[6]):
#                pass
            else:
                if int(line.split()[0]) != number:
                    if sentence is not None:
                        sentence.set_length()
                        self.sentences.append(sentence)
                    sentence = Multiple()
                    sentence.number = int(line.split()[0])
                    number = sentence.number
#                span = tuple([int(i) for i in line.split()[8].split("=")[1].split("-")])
                span = re.search(r"covered=([0-9]+\-[0-9]+)", line).expand("\g<1>")
                # print span.expand("\g<1>")
                span = tuple([int(i) for i in span.split("-")])
                if len(sentence.spans[span]) < cell_limit:
                    sentence.spans[span].append(line.strip())
        if sentence is not None:
            sentence.set_length()
            self.sentences.append(sentence)
            
    def read_phrase_stack_verbose(self, cell_limit):
        self.sentences = []
        sentence = None
        number = -1
        span_input = False
        for line in self.file:
            if line.startswith("Translating: "):
                if sentence is not None:
                    sentence.set_length()
                    self.sentences.append(sentence)
                    
                number += 1
                sentence = Multiple()
                sentence.number = number
            else:
                if re.match("\[[A-Z,a-z,\ ]+;\ [0-9]+-[0-9]+\]", line):
                    span = tuple([int(i) for i in line.split(";")[1].strip().strip("]").split("-")])
                    sentence.spans[span].append(line.strip())
                    span_input = True
#                    print line,
                elif span_input is True:
                    if line.strip() == "":
                        span_input = False
#                        print "X"
                    else:
                        if len(sentence.spans[span]) < cell_limit:
                            sentence.spans[span].append(line.strip())
#                        print line,
        if sentence is not None:
            sentence.set_length()
            self.sentences.append(sentence)
            
            

    def read_syntax_cube_flag(self, cell_limit):
        self.sentences = []
        sentence = None
        number = -1
        for line in self.file:
            if len(line.split()) < 6:
                pass
            else:
                if int(line.split()[0]) != number:
                    if sentence is not None:
                        sentence.set_length()
                        self.sentences.append(sentence)
                    sentence = Multiple()  # 
                    sentence.number = int(line.split()[0])
                    number = sentence.number
                span = re.search(r"\[([0-9]+)\.\.([0-9]+)\]", line).expand("\g<1> \g<2>")
                span = tuple([int(i) for i in span.split()])
                if len(sentence.spans[span]) < cell_limit:
                    sentence.spans[span].append(line.strip())
        if sentence is not None:
            sentence.set_length()
            self.sentences.append(sentence)
        
        
    def read_mbot(self, cell_limit):
        self.sentences = []
        sentence = None
        number = -1
        hypo = False
        rule = False
        popping = False
        target = ""
        source = ""
        source_parent = ""
        target_parent = ""
        alignment = ""
        for line in self.file:
            if line.startswith("Translating:"):
                if sentence is not None:
                    sentence.set_length()
                    self.sentences.append(sentence)
                sentence = Multiple()
                sentence.number = number + 1
                number = sentence.number
            elif line.startswith("POPPING"):
                popping = True 
            elif popping is True:
                popping = False
                span = tuple([int(i) for i in line.split()[1].strip("[").split("]")[0].split("..")])
                hypo = True
            elif hypo is True:
                if line.startswith("Target Phrases"):
                    target = line.split(":", 1)[1].strip()
                
                elif line.startswith("Alignment Info"):
                    alignment = line.split(":", 1)[1].strip()
                    if alignment == "":
                        alignment = "(1)"
                    
                elif line.startswith("Source Phrase"):
                    source = line.split(":", 1)[1].strip()
    
                elif line.startswith("Source Left-hand-side"):
                    source_parent = line.split(":", 1)[1].strip()
    
                elif line.startswith("Target Left-hand-side"):
                    target_parent = line.split(":", 1)[1].strip()
    
                    # Input stored: now begin translation into rule-format
                    alignment = re.sub(r"\([0-9]+\)", "||", alignment)
                    align_blocks = alignment.split("||")[:-1]
                    target = re.sub(r"\([0-9]+\)", "||", target)
                    target = [x.split() for x in target.split("||")][:-1]
                    source = source.split()
    
                    for i in range(len(source)):
                        if source[i].isupper():
                            source[i] = "[" + source[i] + "]"
                            for k in range(len(align_blocks)):
                                align_pairs = [tuple([int(y) for y in x.split("-")]) for x in align_blocks[k].split()]
                                for j in filter(lambda x: x[0] == i, align_pairs):
                                    source[i] = source[i] + "[" + target[k][j[1]] + "]"
    
                    for i in range(len(target)):
                        for j in range(len(target[i])):
                            align_pairs = [tuple([int(y) for y in x.split("-")]) for x in align_blocks[i].split()]
                            for k in filter(lambda x: x[1] == j, align_pairs):
                                target[i][j] = source[k[0]].split("]")[0] + "][" + target[i][j] + "]"
                
                
                
                    target = " || ".join([" ".join(x) for x in target]) + " ||"
    
                    source = " ".join(source)
                    source = source + "  [" + source_parent + "]"
    
                    tp = re.sub(r"\([0-9]+\)", "", target_parent).split()
                    for i in tp:
                        target = target.replace("||", " [" + i + "] !!", 1)
                    target = target.replace("!!", "||")
                        
                    rule = False
                    search_pattern = "|||  " + source + " ||| " + target + "| ---  ||| " + alignment + "|" 
    
                    sentence.spans[span].append(search_pattern)
#                    print search_pattern, span
                    if len(sentence.spans[span]) < cell_limit:
                        sentence.spans[span].append(search_pattern)
            else:
                pass
        if sentence is not None:
            sentence.set_length()
            self.sentences.append(sentence)




class Single():
    def __init__(self):
        self.number = None
        self.spans = {}
        self.length = None

    def set_length(self):
        self.length = max([x[1] for x in self.spans.keys()])
    
    def __str__(self):
        number = str(self.number)
        length = str(self.length)
        spans = "\n"
        for i in self.spans.keys():
            spans += str(i) + " - " + str(self.spans[i]) + "\n"
        return str((number, length, spans))

class Multiple():
    def __init__(self):
        self.number = None
        self.spans = collections.defaultdict(list)
        self.length = None

    def set_length(self):
        self.length = max([x[1] for x in self.spans.keys()])
    
    def __str__(self):
        number = str(self.number)
        length = str(self.length)
        spans = "\n"
        for i in self.spans.keys():
            spans += str(i) + " - " + str(self.spans[i]) + "\n"
        return str((number, length, spans))