-
Notifications
You must be signed in to change notification settings - Fork 2
/
conceptualWeighting.py
executable file
·229 lines (184 loc) · 6.83 KB
/
conceptualWeighting.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python
# PyElly - scripting tool for analyzing natural language
#
# conceptualWeighting.py : 31mar2017 CPM
# ------------------------------------------------------------------------------
# Copyright (c) 2013, 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.
# -----------------------------------------------------------------------------
"""
compute closeness of concept pairs relative to a given conceptual hierarchy
or a single concept relative to a statistical profile of the most important
concepts seen in current discourse
"""
import conceptualHierarchy
NNC = 12 # nominal number of concepts to list in context profile
class ConceptualWeighting(object):
"""
for conceptual context
attributes:
hiery - saved conceptual hierarchy
totln - total count of concept references
minmm - minimum count for concept to be considered
maxmm - maximum count for all concepts
index - keep track of all concepts referenced
topcs - current top concepts (=topics)
"""
def __init__ ( self , hier ):
"""
initialize from conceptual hierarchy
arguments:
self -
hier - predefined hierarchy
"""
self.hiery = hier
self.totln = 0
self.minmm = 1
self.maxmm = 0
self.index = { }
self.topcs = [ ]
def recanvass ( self ):
"""
select currently most important concepts
arguments:
self
"""
kys = self.index.keys() # all concepts being tracked
if len(self.topcs) > NNC and self.minmm < self.maxmm:
self.minmm += 1 # be more selective
topcs = [ ]
for ky in kys:
n = self.index[ky] # count for next concept
if n < self.minmm: continue # if too small, just disregard
k = len(topcs) - 1
while k >= 0: # selection sort loop
r = topcs[k] # get concepts and counts already selected
if r[1] >= n: break # compare previous selected concept with lowest count
topcs[k+1] = r # make room for insertion
k -= 1 # move up in selection listing
topcs[k+1] = [ ky , n ] # insert concept and count into listing
if self.maxmm < n: self.maxmm = n # update maximum count if needed
self.topcs = topcs # replace topics
def interpretConcept ( self , cn ):
"""
compute semantic importance score of specified concept
arguments:
self -
cn - concept as name string
returns:
integer importance score
"""
if cn == conceptualHierarchy.NOname: return 0
# print 'importance ' + cn + ':'
mink = 10000000
minc = conceptualHierarchy.NOname
for c in self.topcs:
k = self.hiery.isA(cn,c)
if mink > k and k > 0:
mink = k
minc = c
if minc == conceptualHierarchy.NOname: return 0
self.noteConcept(minc)
return self.index[minc] - self.minmm
def relateConceptPair ( self , cna , cnb ):
"""
compute relatedness score for two specified concepts
arguments:
self -
cna - first concept as name string
cnb - second
returns:
integer relatedness score
"""
if cna == conceptualHierarchy.NOname or cnb == conceptualHierarchy.NOname: return 0
# print 'relatedness ' + cna + ':' + cnb
rel = self.hiery.relatedness(cna,cnb)
# print '=' , rel
inx = self.hiery.intersection()
# print 'intersect at' , inx
if inx != '^':
self.noteConcept(inx)
return rel
def getIntersection ( self ):
"""
get intersection for last relatedness
arguments:
self -
returns:
concept name as string
"""
inx = self.hiery.intersection()
# print 'get intersection' , inx
return conceptualHierarchy.NOname if inx == None else inx.name
def noteConcept ( self , cn ):
"""
keep statistics on specified concepts
arguments:
self -
cn - specified concept as name string
"""
if cn == None or cn == conceptualHierarchy.TOP:
return
elif not cn in self.index:
self.index[cn] = 0
self.index[cn] += 1
#
# unit test
#
if __name__ == '__main__':
import sys
import ellyDefinitionReader
data = [
"^ > cXXXX" ,
"cXXXX > cYYYY0" ,
"cXXXX > cYYYY1" ,
"cXXXX > cYYYY2" ,
"cYYYY1> cZZZZ1" ,
"cYYYY2> cZZZZ2" ,
"cZZZZ1> cAAAA" ,
"cZZZZ1> cBBBB"
]
src = sys.argv[1] if len(sys.argv) > 1 else data
inp = ellyDefinitionReader.EllyDefinitionReader(src)
if inp.error != None:
print >> sys.stderr, inp.error
sys.exit(1)
hy = conceptualHierarchy.ConceptualHierarchy(inp)
wt = ConceptualWeighting(hy)
si = sys.stdin
so = sys.stdout
so.write('> ')
while True:
line = si.readline()
l = line.decode('utf8')
if len(l) == 0 or l[0] == '\n': break
ki = 0
while l[ki] == ' ': ki += 1
while l[ki] != ' ': ki += 1
a = l[:ki].strip().upper()
b = l[ki:].strip().upper()
arb = wt.relateConceptPair(a,b)
so.write(a + ":" + b + ", relatedness=" + str(arb) +
' @' + str(wt.getIntersection()) + "\n")
so.write('> ')