-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentityExtractor.py
executable file
·166 lines (140 loc) · 5.21 KB
/
entityExtractor.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
#!/usr/bin/python3
# PyElly - rule-based tool for analyzing natural language (Python v3.8)
#
# entityExtractor.py : 14nov2019 CPM
# ------------------------------------------------------------------------------
# Copyright (c) 2019, Clinton Prentiss Mah
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
"""
runs extraction methods and generates phrases
"""
import ellyBits
import ellyChar
import ellyConfiguration
import syntaxSpecification
import featureSpecification
noBits = ellyBits.EllyBits()
class EntityExtractor(object):
"""
handler for information extraction (IE)
attributes:
ptr - parse tree for extracted information
sym - saved symbol table
exs - extraction procedure list
"""
def __init__ ( self , ptr , sym ):
"""
initialization
arguments:
self -
ptr - parse tree
sym - symbol table
exceptions:
FormatFailure on error
"""
self.ptr = ptr
self.sym = sym
self.exs = [ ]
for x in ellyConfiguration.extractors:
proc = x[0]
synt = syntaxSpecification.SyntaxSpecification(sym,x[1].lower())
entry = [ proc , synt.catg , synt.synf.positive ]
if len(x) > 2:
f = None if x[2] == '-' else x[2].lower()
smnt = featureSpecification.FeatureSpecification(sym,f,True)
entry.append(smnt.positive)
if len(x) > 3:
entry.append(x[3])
self.exs.append(entry)
def dump ( self ):
"""
show defined extractors
arguments:
self
"""
for ex in self.exs:
print ( ex[0] , end=' ' )
print ( 'syntax cat=' , self.sym.getSyntaxTypeName(ex[1]).upper() , end=' ' )
print ( ' fet=' , ex[2].hexadecimal(False) , end=' ' )
if len(ex) > 3:
print ( ' sem=' , ex[3].hexadecimal(False) , end=' ' )
if len(ex) > 4:
print ( ' pls=' , str(ex[4]) , end=' ' )
print ()
def run ( self , segm ):
"""
execute each extractor and store results
arguments:
self -
segm - input buffer
returns:
returns number of chars matched on success, 0 otherwise
"""
mx = 0
ms = [ ]
capd = ellyChar.isUpperCaseLetter(segm[0])
for xr in self.exs: # try each extraction procedure in order
m = xr[0](segm) #
if m > 0: # match?
if mx > m: # if so, it has to be longer than the longest previous
continue
elif mx < m: # if longer than longest previous, discard the previous
mx = m
ms = [ ]
ms.append(xr[1:]) # add to match list
nmatch = 0
if mx > 0: # any matches?
for mr in ms: # if so, make phrases for them
sbs = mr[2] if len(mr) > 2 else noBits
bia = mr[3] if len(mr) > 3 else 0
if self.ptr.addLiteralPhraseWithSemantics(mr[0],mr[1],sbs,bia,None,False,capd):
self.ptr.lastph.lens = mx
nmatch += 1
return mx if nmatch > 0 else 0
#
# unit test
#
if __name__ == '__main__':
import parseTest
import sys
print ( 'test entity extraction' )
utre = parseTest.Tree()
uctx = parseTest.Context()
ee = EntityExtractor(utre,uctx.syms)
print ( 'procedures:' )
ee.dump()
print ()
while True:
sys.stdout.write('> ')
sys.stdout.flush()
l = sys.stdin.readline()
if len(l) <= 1: break
b = list(l)
mn = ee.run(b)
print ( 'match n=' , mn )
if mn > 0:
print ( utre.lastph )
print ( 'new buffer:' , b )
sys.stdout.write('\n')