def _parse_suggestion(line): def extract(start, end): start = line.index(start) + len(start) end = line.index(end, start) return line[start:end] return Namespace( memory=int(extract('ScoreEntry{memory=', ', language=')), language=extract(', language=', ', sentence='), sentence=extract(', sentence=[', '], translation=[').replace(', ', ' '), translation=extract('], translation=[', '], score=').replace(', ', ' '), score=float(extract('], score=', '}')) )
def __exit__(self, exc_type, exc_val, exc_tb): for line in this.log_lines_iter(start=self._start): if 'Translation received from neural decoder:' not in line: continue lines = line.strip().splitlines(keepends=False) sentence = lines[1][lines[1].index(' = ') + 3:] translation = lines[2][lines[2].index(' = ') + 3:] # alignment is skipped suggestions = [self._parse_suggestion(x) for x in lines[5:-1]] self.translation = Namespace(sentence=sentence, translation=translation, suggestions=suggestions) break