-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherg.py
197 lines (169 loc) · 7.04 KB
/
erg.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Script for analysing ERG
Latest version can be found at https://github.com/letuananh/intsem.fx
@author: Le Tuan Anh <[email protected]>
@license: MIT
'''
# Copyright (c) 2018, Le Tuan Anh <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
########################################################################
import os
import logging
from chirptext import TextReport, Counter
from chirptext.cli import CLIApp, setup_logging
from coolisf import GrammarHub
from coolisf.mappings import PredSense
from coolisf.ergex import read_erg_lex
from coolisf.model import Predicate
# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------
setup_logging('logging.json', 'logs')
TRIVIAL_POS = 'navj'
ghub = GrammarHub()
EI = ghub.ERG_ISF
def getLogger():
return logging.getLogger(__name__)
# ------------------------------------------------------------------------------
# Functions
# ------------------------------------------------------------------------------
def list_preds(cli, args):
rp = TextReport(args.output)
lexdb = read_erg_lex()
keyrels = set(l.keyrel for l in lexdb if l.keyrel)
preds = [Predicate.from_string(p) for p in keyrels]
sorted_preds = sorted(preds, key=lambda x: x.pos or '')
# All preds
with open('data/erg_preds_sorted.txt', 'w') as outfile:
for pred in sorted_preds:
outfile.write('{}\n'.format(pred))
poses = set(p.pos for p in preds)
trivial_preds = [p for p in preds if p.pos and p.pos in TRIVIAL_POS]
if not args.trivial:
preds = [p for p in preds if not p.pos or p.pos not in TRIVIAL_POS]
interesting_poses = set(p.pos for p in preds)
# write interesting preds to file
c = Counter()
with open('data/erg_preds_interesting.txt', 'w') as outfile:
for pred in sorted(preds, key=lambda x: "cqpx".index(x.pos) if x.pos else 0):
c.count(pred.pos if pred.pos else 'NONE')
outfile.write('{}\n'.format(pred))
# report
rp.print("Interesting preds: {}".format(len(preds)))
rp.print("Trivial preds: {}".format(len(trivial_preds)))
rp.print("POS: {}".format(poses))
rp.print("Interesting POS: {}".format(interesting_poses))
c.summarise(rp)
def list_gpreds(cli, args):
rp = TextReport(args.output)
with open('data/erg_preds_sorted.txt', 'r') as infile:
sorted_preds = (Predicate.from_string(l) for l in infile)
for pred in sorted_preds:
if pred.ptype == Predicate.GRAMMARPRED:
rp.print(pred)
pass
def map_preds(cli, args):
rp = TextReport(args.output)
ctx = PredSense.wn.ctx()
not_found = []
pred_file = 'data/erg_preds_interesting.txt'
if args.all:
pred_file = 'data/erg_preds_sorted.txt'
name, ext = os.path.splitext(pred_file)
not_found_file = name + "_notfound" + ext
with open(pred_file, 'r') as infile:
for p_str in infile.read().splitlines():
p = Predicate.from_string(p_str)
candidates = None
if p.pos == 'x' and p.sense == 'subord':
continue # ignore these for now
# if (p.pos == 'x' and p.sense == 'deg') or p.pos == 'p':
if args.all or (p.pos and p.pos in 'xpq'):
rp.header(p, p.lemma, p.pos, p.sense)
candidates = PredSense.search_pred_string(p, ctx=ctx)
for c in candidates:
rp.print(c.ID, c.lemmas, c.definition)
if not candidates:
not_found.append(p_str)
with TextReport(not_found_file, 'w') as outfile:
for p in not_found:
outfile.print(p)
if args.output:
print("Written to: {}".format(args.output))
print("Done")
def analyse(cli, args):
''' Analyse a sentence '''
print("Source: {}".format(args.sent))
if args.erg:
p = ghub.ERG.parse(args.sent).edit(0)
else:
p = ghub.ERG_ISF.parse(args.sent).edit(0)
preds = p.source.preds()
print("Predicates: {}".format(preds))
print("Sense candidates:")
for pred in preds:
candidates = PredSense.search_pred_string(pred)
if candidates:
print("\\item {} -> {}".format(pred.replace('_', '\\_'), ', '.join(str(c.ID) for c in candidates)))
print(" \\begin{itemize}")
for c in candidates:
print(" \\item {}: {}".format(c.ID, c.definition))
print(" \\end{itemize}")
else:
print(pred)
def pred_info(cli, args):
p = Predicate.from_string(args.pred)
print("{}: lemma={} | pos={} | sense={}".format(args.pred, p.lemma, p.pos, p.sense))
candidates = PredSense.search_pred_string(p)
if candidates:
for c in candidates:
print(c.ID, c.lemmas, c.definition)
else:
print("No candidate could be found")
# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
def main():
''' ISF ERG Analyser '''
app = CLIApp(desc='ISF ERG Toolkit', logger=__name__)
# add tasks
task = app.add_task('preds', func=list_preds)
task.add_argument('-o', '--output', help="Output")
task.add_argument('-t', '--trivial', help="Don't remove trivial preds", action="store_true")
# list gpreds
task = app.add_task('gpreds', func=list_gpreds)
task.add_argument('-o', '--output', help="Output")
# map preds
task = app.add_task('map', func=map_preds)
task.add_argument('-o', '--output', help="Output")
task.add_argument('--all', help="Map all preds", action="store_true")
# show predicate info
task = app.add_task('info', func=pred_info)
task.add_argument('pred')
# analyse text
task = app.add_task('text', func=analyse)
task.add_argument('sent')
task.add_argument('-e', '--erg', help='ERG only - no transformation', action='store_true')
# run app
app.run()
if __name__ == "__main__":
main()