Create oie_readers/tabReader.py
Browse files
evaluation_data/carb/oie_readers/tabReader.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Usage:
|
| 2 |
+
tabReader --in=INPUT_FILE
|
| 3 |
+
|
| 4 |
+
Read a tab-formatted file.
|
| 5 |
+
Each line consists of:
|
| 6 |
+
sent, prob, pred, arg1, arg2, ...
|
| 7 |
+
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from oie_readers.oieReader import OieReader
|
| 11 |
+
from oie_readers.extraction import Extraction
|
| 12 |
+
from docopt import docopt
|
| 13 |
+
import logging
|
| 14 |
+
import ipdb
|
| 15 |
+
|
| 16 |
+
logging.basicConfig(level = logging.DEBUG)
|
| 17 |
+
|
| 18 |
+
class TabReader(OieReader):
|
| 19 |
+
|
| 20 |
+
def __init__(self):
|
| 21 |
+
self.name = 'TabReader'
|
| 22 |
+
|
| 23 |
+
def read(self, fn):
|
| 24 |
+
"""
|
| 25 |
+
Read a tabbed format line
|
| 26 |
+
Each line consists of:
|
| 27 |
+
sent, prob, pred, arg1, arg2, ...
|
| 28 |
+
"""
|
| 29 |
+
d = {}
|
| 30 |
+
ex_index = 0
|
| 31 |
+
with open(fn) as fin:
|
| 32 |
+
for line in fin:
|
| 33 |
+
if not line.strip():
|
| 34 |
+
continue
|
| 35 |
+
data = line.strip().split('\t')
|
| 36 |
+
text, confidence, rel = data[:3]
|
| 37 |
+
curExtraction = Extraction(pred = rel,
|
| 38 |
+
head_pred_index = None,
|
| 39 |
+
sent = text,
|
| 40 |
+
confidence = float(confidence),
|
| 41 |
+
question_dist = "./question_distributions/dist_wh_sbj_obj1.json",
|
| 42 |
+
index = ex_index)
|
| 43 |
+
ex_index += 1
|
| 44 |
+
|
| 45 |
+
for arg in data[3:]:
|
| 46 |
+
curExtraction.addArg(arg)
|
| 47 |
+
|
| 48 |
+
d[text] = d.get(text, []) + [curExtraction]
|
| 49 |
+
self.oie = d
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
args = docopt(__doc__)
|
| 54 |
+
input_fn = args["--in"]
|
| 55 |
+
tr = TabReader()
|
| 56 |
+
tr.read(input_fn)
|