-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_cfg.py
63 lines (56 loc) · 1.78 KB
/
lib_cfg.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import re
def build_cfg_map(cfg_path, oneline_cfg=False):
cfg_lines = []
if oneline_cfg:
cfg_lines = list(map(str.strip, open(cfg_path)))
else:
buf = []
for line in open(cfg_path):
line = line.strip()
if not line:
if buf:
cfg_lines.append(" ".join(buf))
buf.clear()
else:
pass
else:
buf.append(line)
# building CFG map
cfg_map = {}
for cfg_line in cfg_lines:
words = re.findall(r" ([^\) ]+)\)", cfg_line)
sent = "".join(words)
sent = sent.replace("-LRB-", "(")
sent = sent.replace("-RRB-", ")")
sent = sent.replace("-LSB-", "[")
sent = sent.replace("-RSB-", "]")
sent = sent.replace("`", "'")
sent = sent.replace("’", "'")
sent = sent.replace(".", "")
cfg_map[sent] = cfg_line
return cfg_map
def align_cfg_oneline(sent_path, cfg_path, oneline_cfg=False):
sents = map(str.strip, open(sent_path))
cfg_map = build_cfg_map(cfg_path, oneline_cfg=True)
cfg_onelines = []
fails = 0
for sent in sents:
key = sent.replace(" ", "")
key = key.replace("'", "'")
key = key.replace(""", "''")
key = key.replace("\"", "''")
key = key.replace("’", "'")
key = key.replace(".", "")
key = key.replace("—", "--")
if key in cfg_map:
cfg_onelines.append(cfg_map[key])
else:
cfg_onelines.append("")
fails += 1
print("fails", fails)
return cfg_onelines