-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpymln.py
More file actions
91 lines (64 loc) · 2.72 KB
/
pymln.py
File metadata and controls
91 lines (64 loc) · 2.72 KB
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
#
# Python implementation of Unsupervised Semantic Parsing system, from:
#
# Hoifung Poon and Pedro Domingos (2009). "Unsupervised Semantic Parsing",
# in Proceedings of the Conference on Empirical Methods in Natural Language
# Processing (EMNLP), 2009. http://alchemy.cs.washington.edu/usp.
#
import argparse
import os
from semantic import Parse
from semantic.MLN import MLN
def read_input_files(DIR):
'''Read files given by list of names '''
files = set()
for file in os.listdir(DIR):
if file.endswith(".dep"):
files.add(file)
return files
def run(params):
if os.path.isabs(params['data_dir']):
data_dir = params['data_dir']
else:
data_dir = os.path.join(os.getcwd(), params['data_dir'])
if os.path.isabs(params['results_dir']):
results_dir = params['results_dir']
else:
results_dir = os.path.join(os.getcwd(), params['results_dir'])
priorNumParam = params['priorNumParam']
priorNumConj = params['priorNumConj']
parser = Parse.Parse()
# Get files
input_files = read_input_files(data_dir)
# Parse files into MLN knowledge base
parser.parse(input_files)
# Save knowledge base files to disk
MLN.printModel(results_dir)
return None
if __name__ == '__main__':
prs = argparse.ArgumentParser(description='Parse scientific articles into'
' Markov Logic Network knowledge base. \n'
'Usage: python -m pymln.py [-d dataDir] '
'[-r resultDir] [-p priorNumParam] [-c '
'priorNumConj]')
prs.add_argument('-d', '--data_dir',
help='Directory of source files. If not specified, '
'defaults to the current working directory.')
prs.add_argument('-r', '--results_dir',
help='Directory to save results files. If not specified,'
' defaults to the current working directory.')
prs.add_argument('-p', '--priorNumParam',
help='Prior on parameter number. If not specified,'
' defaults to 5.')
prs.add_argument('-c', '--priorNumConj',
help='Prior on number of conjunctive parts assigned to '
'same cluster. If not specified, defaults to 10.')
args = vars(prs.parse_args())
# Default argument values
params = {'priorNumParam': 5, 'priorNumConj': 10, 'data_dir': os.getcwd(),
'results_dir': os.getcwd()}
# If specified in call, override defaults
for par in params:
if args[par] is not None:
params[par] = args[par]
run(params)