forked from MetaphorExtractionTools/mokujin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfsearch.py
50 lines (36 loc) · 1.41 KB
/
lfsearch.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
#!/usr/bin/env python
# coding: utf-8
# Copyright (C) USC Information Sciences Institute
# Author: Vladimir M. Zaytsev <zaytsev@usc.edu>
# URL: <http://nlg.isi.edu/>
# For more information, see README.md
# For license information, see LICENSE
import sys
import logging
import argparse
import cPickle as pickle
from mokujin.index import SimpleObjectIndex
from createlfindex import sent_to_terms
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--index", default="lfindex", help="LF sentences index directory", type=str)
parser.add_argument("-o", "--output", default=None, type=str)
parser.add_argument("-q", "--query", default=None, type=str)
args = parser.parse_args()
i_dir = args.inputdir
o_file = open(args.output, "w") if args.output is not None else sys.stdout
query_term = args.query
logging.info("INPUT DIR: %r" % i_dir)
logging.info("OUT FILE: %r" % o_file)
logging.info("QUERY: %s" % query_term)
obj_to_terms = sent_to_terms
obj_to_str = pickle.dumps
str_to_obj = pickle.loads
index = SimpleObjectIndex(i_dir, obj_to_terms, obj_to_str, str_to_obj)
index.load_all()
results = index.find(query_terms=(query_term,))
o_file.write("FOUND (%d):\n" % len(results))
for sent in results:
o_file.write(sent.raw_text.encode("utf-8"))
o_file.write("\n")