~ubuntu-branches/ubuntu/trusty/frog/trusty

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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python

"""
Constraint satisfaction inference for dependency parsing.

usage: %prog [options] [file...]

-mDIST, --max-dist=DIST: maximum distance between head and dependent
-x, --exclude-non-scoring: do not generate instance for non-scoring
                           tokens

Constraints:
--dep=FILENAME: classifier output for the C_dep constraint
--dir=FILENAME: classifier output for the C_dir constraint
--mod=FILENAME: classifier output for the C_mod constraint

--out=FILENAME: output file name

Parsing algorithm:
--non-projective: approximate non-projective parsing

---
:Refinements:
-m: type='int', dest='maxDist'
-x: action='store_true', default=False, dest='skipNonScoring'
"""

import fileinput
import sys

from itertools import izip, imap, groupby, ifilter
from operator import itemgetter, attrgetter

from sentences import sentenceIterator

import cky
import csiparse2 as csiparse
import deptree

from common import *


def leftIncomplete(chart, s, t, sentence):
	r = chart[s, t, "l", False].r
	label = chart[s, t, "l", False].edgeLabel
	if r is not None:
		assert s > 0
		sentence[s - 1][DEPREL] = label
		sentence[s - 1][HEAD] = t
		rightComplete(chart, s, r, sentence)
		leftComplete(chart, r + 1, t, sentence)


def rightIncomplete(chart, s, t, sentence):
	r = chart[s, t, "r", False].r
	label = chart[s, t, "r", False].edgeLabel
	if r is not None:
		assert t > 0
		sentence[t - 1][DEPREL] = label
		sentence[t - 1][HEAD] = s
		rightComplete(chart, s, r, sentence)
		leftComplete(chart, r + 1, t, sentence)


def leftComplete(chart, s, t, sentence):
	r = chart[s, t, "l", True].r
	if r is not None:
		leftComplete(chart, s, r, sentence)
		leftIncomplete(chart, r, t, sentence)


def rightComplete(chart, s, t, sentence):
	r = chart[s, t, "r", True].r
	if r is not None:
		rightIncomplete(chart, s, r, sentence)
		rightComplete(chart, r, t, sentence)


def cyclic(sentence, head, dependent):
	if head == dependent:
		return True
	x = sentence[head - 1][HEAD]
	while x > 0:
		if x == dependent:
			return True
		else:
			x = sentence[x - 1][HEAD]

	return False


def evaluateTree(sentence, parser):
	score = 0
	
	for i in xrange(len(sentence)):
		inDeps = set(x[DEPREL] for x in sentence if x[HEAD] == i + 1)
		outDep = sentence[i][DEPREL]

		# C_mod
		for constraint in parser.inDepConstraints[i + 1]:
			if constraint.relType in inDeps:
				score += constraint.weight

		# C_dir
		for constraint in parser.outDepConstraints[i + 1]:
			if (0 < sentence[i][HEAD] < i + 1 and \
				constraint.direction == constraint.LEFT) or \
				(sentence[i][HEAD] > i + 1 and \
				 constraint.direction == constraint.RIGHT) or \
				(sentence[i][HEAD] == 0 and \
				 constraint.direction == constraint.ROOT):
				score += constraint.weight

		# C_dep
		if True: #sentence[i][HEAD] > 0:
			for constraint in parser.edgeConstraints[i + 1][sentence[i][HEAD]]:
				if constraint.relType == sentence[i][DEPREL]:
					score += constraint.weight

	return score


def scoreDiff(sentence, parser, dependent, newHead):
	result = 0
	
	oldHead = sentence[dependent - 1][HEAD]
	oldRel = sentence[dependent - 1][DEPREL]

	# C_dep diff
	if True: #oldHead >= 0:
		depRels = parser.edgeConstraints[dependent][oldHead]
		assert len(depRels) <= 1
		if depRels and depRels[0].relType == oldRel:
			result -= depRels[0].weight
	
	if True: #newHead >= 0:
		depRels = parser.edgeConstraints[dependent][newHead]
		assert len(depRels) <= 1
		if depRels:
			newRel = depRels[0].relType
			result += depRels[0].weight
		else:
			newRel = "unk" if newHead > 0 else "ROOT"

	# C_mod diff
	if oldHead > 0:
		dependents = [x
					  for x in sentence
					  if x[HEAD] == oldHead and x[DEPREL] == oldRel]
		if len(dependents) == 1:
			for constraint in parser.inDepConstraints[oldHead]:
				if constraint.relType == oldRel:
					result -= constraint.weight

	if newHead > 0:
		dependents = [x
					  for x in sentence
					  if x[HEAD] == newHead and x[DEPREL] == newRel]
		if len(dependents) == 0:
			for constraint in parser.inDepConstraints[newHead]:
				if constraint.relType == newRel:
					result += constraint.weight

	# C_dir diff
	if True: #(dependent - oldHead) * (dependent - newHead) < 0:
		for constraint in parser.outDepConstraints[dependent]:
			if (0 < oldHead < dependent and \
				constraint.direction == constraint.LEFT) or \
				(oldHead > dependent and \
				 constraint.direction == constraint.RIGHT) or \
				(oldHead == 0 and \
				 constraint.direction == constraint.ROOT):
				result -= constraint.weight

			if (0 < newHead < dependent and \
				constraint.direction == constraint.LEFT) or \
				(newHead > dependent and \
				 constraint.direction == constraint.RIGHT) or \
				(newHead == 0 and \
				 constraint.direction == constraint.ROOT):
				result += constraint.weight

	return result


def approxNonProjective(sentence, parser):
	#currentScore = 0 #evaluateTree(sentence, parser)
	currentScore = evaluateTree(sentence, parser)

	while True:
		bestScore = float("-inf")
		bestHead = None
		bestDependent = None

		for j in xrange(1, len(sentence) + 1):
			for i in xrange(0, len(sentence) + 1):
				assert type(sentence[j - 1][HEAD]) == int
				if not sentence[j - 1][HEAD] == i and \
					   not cyclic(sentence, i, j):

					score = currentScore + scoreDiff(sentence, parser, j, i)
					########
					#newSentence = [t[:] for t in sentence]
					#newSentence[j - 1][HEAD] = i
					#if True: #i > 0:
					#	depRels = parser.edgeConstraints[j][i]
					#	assert len(depRels) <= 1
					#	if depRels:
					#		newSentence[j - 1][DEPREL] = depRels[0].relType
					#	else:
					#		newSentence[j - 1][DEPREL] = "unk" if i > 0 else "ROOT"
					#assert abs(evaluateTree(newSentence, parser) - score) < 0.0001, ("%s %s" % (j, i))
					#######

					if score > bestScore:
						bestScore = score
						bestHead = i
						bestDependent = j

		if bestScore > currentScore:
#			sys.stderr.write("+")
			currentScore = bestScore
			sentence[bestDependent - 1][HEAD] = bestHead
			if True: #bestHead > 0:
				depRels = parser.edgeConstraints[bestDependent][bestHead]
				assert len(depRels) <= 1
				if depRels:
					sentence[bestDependent - 1][DEPREL] = depRels[0].relType
				else:
					sentence[bestDependent - 1][DEPREL] = "unk" if bestHead > 0 else "ROOT"
			#else:
			#	sentence[bestDependent - 1][DEPREL] = "ROOT"
		else:
			return


def main(args):
	import cmdline
	main_(*cmdline.parse(__doc__, args))


def main_(options, args):
	assert options.dep
	assert options.out
	pairsStream = open(options.dep)
	pairsIterator = csiparse.instanceIterator(pairsStream)
#	print >> sys.stderr, "C_dep constraints enabled"

	if options.dir:
		dirStream = open(options.dir)
		dirIterator = csiparse.instanceIterator(dirStream)
#		print >> sys.stderr, "C_dir constraints enabled"
	else:
		dirIterator = None

	if options.mod:
		relsStream = open(options.mod)
		relsIterator = csiparse.instanceIterator(relsStream)
#		print >> sys.stderr, "C_mod constraints enabled"
	else:
		relsIterator = None
	
	
	for sentence in sentenceIterator(fileinput.input(args)):
		outfile = open(options.out, "w" )

		domains, constraints = csiparse.formulateWCSP(sentence,
													  dirIterator,
													  relsIterator,
													  pairsIterator,
													  options)

		parser = cky.CKYParser(len(sentence))
		for constraint in constraints:
			parser.addConstraint(constraint)

		chart = parser.parse()

		#item = chart[0, self.numTokens - 1, "r", True]

		#for token in sentence:
		#	token[DEPREL] = "__"
		#	token[HEAD] = "__"
		for token in sentence:
			if len(token) <= DEPREL:
				token.extend([None, None])  

		#print chart[0, len(sentence) - 1, "r", True].r + 1
		rightComplete(chart, 0, len(sentence) - 1 + 1, sentence)

		if options.non_projective:
			approxNonProjective(sentence, parser)

		for token in sentence:
			outfile.write( " ".join(map(str, token)) )
			outfile.write("\n")
		
#		sys.stderr.write(".")


if __name__ == "__main__":
	import cmdline
	main(*cmdline.parse())