[hltdi-l3] 2 new revisions pushed by onlysk...@gmail.com on 2014-05-20 06:41 GMT

3 views
Skip to first unread message

hltd...@googlecode.com

unread,
May 20, 2014, 2:41:14 AM5/20/14
to hltdi-...@googlegroups.com
2 new revisions:

Revision: 34f0d5c1509f
Branch: default
Author: Michael Gasser <gas...@cs.indiana.edu>
Date: Tue May 20 06:19:15 2014 UTC
Log: Lots of unnecessary variables and constraints eliminated.
http://code.google.com/p/hltdi-l3/source/detail?r=34f0d5c1509f

Revision: 13b733f9d3d8
Branch: default
Author: Michael Gasser <gas...@cs.indiana.edu>
Date: Tue May 20 06:40:51 2014 UTC
Log: Alignment inferred if not explicit.
http://code.google.com/p/hltdi-l3/source/detail?r=13b733f9d3d8

==============================================================================
Revision: 34f0d5c1509f
Branch: default
Author: Michael Gasser <gas...@cs.indiana.edu>
Date: Tue May 20 06:19:15 2014 UTC
Log: Lots of unnecessary variables and constraints eliminated.
http://code.google.com/p/hltdi-l3/source/detail?r=34f0d5c1509f

Modified:
/hiiktuu.py
/hiiktuu/cs.py
/hiiktuu/language.py
/hiiktuu/sentence.py

=======================================
--- /hiiktuu.py Mon May 19 06:57:46 2014 UTC
+++ /hiiktuu.py Tue May 20 06:19:15 2014 UTC
@@ -36,7 +36,17 @@
#import cProfile
#import pstats

-def piece_of_mind_parse_ung(verbosity=0):
+def test(verbosity=0):
+ piece_of_mind_parse_ung(verbosity=verbosity)
+ piece_of_mind_trans(verbosity=verbosity)
+ kick_the_bucket(verbosity=verbosity)
+ end_of_world(verbosity=verbosity)
+ never_eaten_fish(verbosity=verbosity)
+ never_eaten_fish_ungr(verbosity=verbosity)
+ cantar_las_cuarenta_I(verbosity=verbosity)
+ cantar_las_cuarenta_she(verbosity=verbosity)
+
+def piece_of_mind_parse_ung(verbosity=0, all_sols=True):
"""
Eng parse.
Illustrates
@@ -46,11 +56,12 @@
s = hiiktuu.Sentence(raw='Mary gives them a piece of my mind',
language=eng,
verbosity=verbosity)
+# print("Parsing: {}".format(s.raw))
s.initialize(verbosity=verbosity)
- s.solve(translate=False, verbosity=verbosity)
+ s.solve(translate=False, verbosity=verbosity, all_sols=all_sols)
return s

-def piece_of_mind_trans(verbosity=0):
+def piece_of_mind_trans(verbosity=0, all_sols=True):
"""
Eng->Spa
Illustrates
@@ -62,11 +73,12 @@
s = hiiktuu.Sentence(raw='Mary gives them a piece of her mind',
language=eng, target=spa,
verbosity=verbosity)
+# print("Translating {} to {}".format(s.raw, s.target))
s.initialize(verbosity=verbosity)
- s.solve(translate=True, verbosity=verbosity)
+ s.solve(translate=True, verbosity=verbosity, all_sols=all_sols)
return s

-def kick_the_bucket(verbosity=0):
+def kick_the_bucket(verbosity=0, all_sols=True):
"""
Eng->Spa
Illustrates
@@ -76,11 +88,12 @@
eng, spa = hiiktuu.Language.load('eng', 'spa')
s = hiiktuu.Sentence(raw='John kicked the bucket', language=eng,
target=spa,
verbosity=verbosity)
+# print("Translating {} to {}".format(s.raw, s.target))
s.initialize(verbosity=verbosity)
- s.solve(verbosity=verbosity)
+ s.solve(verbosity=verbosity, all_sols=all_sols)
return s

-def end_of_world(verbosity=0):
+def end_of_world(verbosity=0, all_sols=True):
"""
Eng->Spa
it's the end of the world -> es el fin del mundo
@@ -90,11 +103,12 @@
eng, spa = hiiktuu.Language.load('eng', 'spa')
s = hiiktuu.Sentence(raw="it's the end of the world", language=eng,
target=spa,
verbosity=verbosity)
+# print("Translating {} to {}".format(s.raw, s.target))
s.initialize(verbosity=verbosity)
- s.solve(verbosity=verbosity)
+ s.solve(verbosity=verbosity, all_sols=all_sols)
return s

-def ate_fish(verbosity=0):
+def ate_fish(verbosity=0, all_sols=True):
"""
Amh->Orm
አሳ በላ (he ate fish) -> qurxummii nyaate.
@@ -103,11 +117,12 @@
"""
amh, orm = hiiktuu.Language.load('amh', 'orm')
s = hiiktuu.Sentence(raw="አሳ በላ", language=amh, target=orm,
verbosity=verbosity)
+# print("Translating {} to {}".format(s.raw, s.target))
s.initialize(verbosity=verbosity)
- s.solve(verbosity=verbosity)
+ s.solve(verbosity=verbosity, all_sols=all_sols)
return s

-def never_eaten_fish(verbosity=0, trans=True):
+def never_eaten_fish(verbosity=0, trans=True, all_sols=True):
"""
Amh አሳ በልቶ አያውቅም 'he's never eaten fish'
Either parse (trans=False) or translate -> Orm: qurxummii nyaate hin
beeku.
@@ -120,12 +135,14 @@
verbosity=verbosity)
s.initialize(verbosity=verbosity)
if trans:
- s.solve(verbosity=verbosity)
+# print("Translating {} to {}".format(s.raw, s.target))
+ s.solve(verbosity=verbosity, all_sols=all_sols)
else:
- s.parse(verbosity=verbosity)
+# print("Parsing: {}".format(s.raw))
+ s.solve(translate=False, verbosity=verbosity, all_sols=all_sols)
return s

-def never_eaten_fish_ungr(trans=True, verbosity=0):
+def never_eaten_fish_ungr(trans=True, verbosity=0, all_sols=True):
"""
Amh አሳ በልተው አያውቅም 'he's never eaten fish' (ungrammatical because the
በልተው is 3rd person *plural* so it doesn't agree with አያውቅም).
@@ -135,11 +152,12 @@
amh, orm = hiiktuu.Language.load('amh', 'orm')
s = hiiktuu.Sentence(raw="አሳ በልተው አያውቅም", language=amh, target=orm,
verbosity=verbosity)
+# print("Attempting to translate {} to {}".format(s.raw, s.target))
s.initialize(verbosity=verbosity)
- s.solve(verbosity=verbosity)
+ s.solve(verbosity=verbosity, all_sols=all_sols)
return s

-def cantar_las_cuarenta_she(trans=True, verbosity=0):
+def cantar_las_cuarenta_she(trans=True, verbosity=0, all_sols=True):
"""
Spa->Eng
Paula les cantó las cuarenta -> Paula read them the riot act.
@@ -155,11 +173,12 @@
s = hiiktuu.Sentence(raw="Paula les cantó las cuarenta",
language=spa, target=eng if trans else None,
verbosity=verbosity)
+# print("Translating {} to {}".format(s.raw, s.target))
s.initialize(verbosity=verbosity)
- s.solve(translate=trans, verbosity=verbosity)
+ s.solve(translate=trans, verbosity=verbosity, all_sols=all_sols)
return s

-def cantar_las_cuarenta_I(trans=True, verbosity=0):
+def cantar_las_cuarenta_I(trans=True, verbosity=0, all_sols=True):
"""
Spa->Eng
les canté las cuarenta -> read them the riot act.
@@ -175,8 +194,9 @@
s = hiiktuu.Sentence(raw="les canté las cuarenta",
language=spa, target=eng if trans else None,
verbosity=verbosity)
+# print("Translating {} to {}".format(s.raw, s.target))
s.initialize(verbosity=verbosity)
- s.solve(translate=trans, verbosity=verbosity)
+ s.solve(translate=trans, verbosity=verbosity, all_sols=all_sols)
return s

def ui():
=======================================
--- /hiiktuu/cs.py Fri May 16 04:02:37 2014 UTC
+++ /hiiktuu/cs.py Tue May 20 06:19:15 2014 UTC
@@ -76,7 +76,8 @@
ambiguity = False
while not fringe.empty() and n < cutoff:
if n > 0 and not ambiguity:
- print("Ambiguity: expanding from best state")
+ if expand_verbosity:
+ print("Ambiguity: expanding from best state")
ambiguity = True
if (n+1) % 50 == 0 or test_verbosity or expand_verbosity:
if test_verbosity or expand_verbosity:
=======================================
--- /hiiktuu/language.py Mon May 19 06:57:46 2014 UTC
+++ /hiiktuu/language.py Tue May 20 06:19:15 2014 UTC
@@ -48,7 +48,7 @@
"""Dictionaries of words, lexemes, grammatical features, and
lexical classes."""

- languages = []
+ languages = {}

def __init__(self,
name, abbrev,
@@ -76,7 +76,7 @@
# Dictionary of morphologically generated words:
# {lexeme: {(feat, val): {(feat, val): wordform,...}, ...}, ...}
self.genforms = genforms or {}
- Language.languages.append(abbrev)
+ Language.languages[abbrev] = self

def __repr__(self):
"""Print name."""
@@ -210,14 +210,18 @@
def load(*abbrevs):
languages = []
for abbrev in abbrevs:
- path = os.path.join(LANGUAGE_DIR, abbrev + '.lg')
- try:
- language = Language.read(path)
+ if abbrev in Language.languages:
+ language = Language.languages[abbrev]
languages.append(language)
- print("Loading language {}".format(language))
- except IOError:
- print("That language doesn't seem to exist.")
- return
+ else:
+ path = os.path.join(LANGUAGE_DIR, abbrev + '.lg')
+ try:
+ language = Language.read(path)
+ languages.append(language)
+ print("Loading language {}".format(language))
+ except IOError:
+ print("That language doesn't seem to exist.")
+ return
return languages

### Basic setters. Create entries (dicts) for item. For debugging
purposes, include name
=======================================
--- /hiiktuu/sentence.py Mon May 19 06:57:46 2014 UTC
+++ /hiiktuu/sentence.py Tue May 20 06:19:15 2014 UTC
@@ -62,6 +62,9 @@
# handled during node merging.
# 2014.05.18
# -- Target-language within-group agreement ("GIVES them a piece of
HER/HIS mind")
+# 2014.05.19
+# -- all_sols argument to solve() and other methods that search finds all
+# solutions without querying user.

import itertools, copy
from .ui import *
@@ -133,10 +136,10 @@
for solution in solutions:
solution.display(word_width=word_width)

- def parse(self, verbosity=0):
- print("Attempting to parse {}".format(self))
- if self.initialize(verbosity=verbosity):
- self.solve(verbosity=verbosity)
+# def parse(self, verbosity=0, all_sols=False):
+# print("Attempting to parse {}".format(self))
+# if self.initialize(verbosity=verbosity):
+# self.solve(verbosity=verbosity, all_sols=all_sols)

def initialize(self, verbosity=0):
"""Things to do before running constraint satisfaction."""
@@ -152,7 +155,7 @@
self.create_constraints(verbosity=verbosity)
return True

- def solve(self, translate=True, verbosity=0):
+ def solve(self, translate=True, all_sols=False, verbosity=0):
"""Generate solutions and translations."""
generator = self.solver.generator(test_verbosity=verbosity,
expand_verbosity=verbosity)
@@ -164,11 +167,19 @@
if verbosity:
print('FOUND ANALYSIS', solution)
if translate:
- solution.translate(verbosity=verbosity)
+ solution.translate(verbosity=verbosity,
all_sols=all_sols)
+ else:
+ # Display the parse
+ self.display()
+ if all_sols:
+ continue
if not input('SEARCH FOR ANOTHER ANALYSIS? [yes/NO] '):
proceed = False
except StopIteration:
- print('No more solutions')
+ if verbosity:
+ print('No more solutions')
+ if not self.solutions:
+ print("NO SOLUTIONS FOUND for {}".format(self))

def tokenize(self, verbosity=0):
"""Segment the sentence string into tokens, analyze them
morphologically,
@@ -297,11 +308,11 @@
catnodes.add(node.sent_index)
else:
instnodes.add(node.sent_index)
- # Snodes that are merged with catnodes
- merged_snodes = set()
- for gn_index in catnodes:
- gn = self.gnodes[gn_index]
- merged_snodes.update(gn.snode_indices)
+# # Snodes that are merged with catnodes
+# merged_snodes = set()
+# for gn_index in catnodes:
+# gn = self.gnodes[gn_index]
+# merged_snodes.update(gn.snode_indices)

self.svar('groups', set(), set(range(len(self.groups))),
# At least 1, at most all groups
@@ -316,10 +327,10 @@
self.variables['snodes'] = DetVar('snodes', covered_snodes)
# Category (abstract) nodes
self.svar('catgnodes', set(), catnodes)
- # Instance gnodes that are merged with catnodes
- self.svar('merged_gnodes', set(), instnodes, 0, len(catnodes))
- # Snodes that involve merger of gnodes (that have two associated
gnodes)
- self.svar('merged_snodes', set(), merged_snodes, 0, len(catnodes))
+# # Instance gnodes that are merged with catnodes
+# self.svar('merged_gnodes', set(), instnodes, 0, len(catnodes))
+# # Snodes that involve merger of gnodes (that have two associated
gnodes)
+# self.svar('merged_snodes', set(), merged_snodes, 0, len(catnodes))
# Position pairs
pos_pairs = set()
for group in self.groups:
@@ -353,66 +364,66 @@

group.variables['cgnodes_pos']]).constraints)
# For each group, the set of groups merged with it +
itself is the union of the
# set of groups merged with it and the set consisting of
its index
-
self.constraints.extend(Union([group.variables['merged_groups_incl'],
-
group.variables['merged_groups_excl'],
-
DetVar('g{}'.format(group.index), {group.index})]).constraints)
+#
self.constraints.extend(Union([group.variables['merged_groups_incl'],
+#
group.variables['merged_groups_excl'],
+#
DetVar('g{}'.format(group.index), {group.index})]).constraints)
# The set of merged gnodes for the group is the union of
the merged nodes for all
# abstract gnodes in the group
-
self.constraints.append(UnionSelection(group.variables['merged_gnodes'],
-
group.variables['agnodes'],
-
[gn.variables['merge_cgn'] for gn in self.gnodes]))
+#
self.constraints.append(UnionSelection(group.variables['merged_gnodes'],
+#
group.variables['agnodes'],
+#
[gn.variables['merge_cgn'] for gn in self.gnodes]))
# The set of groups merged with the group is the union of
groups associated with the
# gnodes that are merged with the group's abstract nodes
-
self.constraints.append(UnionSelection(group.variables['merged_groups_excl'],
-
group.variables['merged_gnodes'],
-
[DetIVar("gn{}->g".format(gn.sent_index), gn.ginst.index) for gn in
self.gnodes]))
+#
self.constraints.append(UnionSelection(group.variables['merged_groups_excl'],
+#
group.variables['merged_gnodes'],
+#
[DetIVar("gn{}->g".format(gn.sent_index), gn.ginst.index) for gn in
self.gnodes]))
# The tree under this group consists of the union of the
snodes associated with this group
# and those merged with it
-
self.constraints.append(UnionSelection(group.variables['tree'],
-
group.variables['merged_groups_incl'],
-
[g.variables['gnodes_pos'] for g in self.groups]))
- for gnode in group.nodes:
- if gnode.cat:
- # The gnodes that this abstract merges with must
be in the set of selected gnodes
-
self.constraints.extend(Inclusion([gnode.variables['merge_cgn'],
-
self.variables['gnodes']]).constraints)
+#
self.constraints.append(UnionSelection(group.variables['tree'],
+#
group.variables['merged_groups_incl'],
+#
[g.variables['gnodes_pos'] for g in self.groups]))
+# for gnode in group.nodes:
+# if gnode.cat:
+# # The gnodes that this abstract merges with must
be in the set of selected gnodes
+#
self.constraints.extend(Inclusion([gnode.variables['merge_cgn'],
+#
self.variables['gnodes']]).constraints)
# The set of category (abstract) nodes used is the union of the
category nodes of the groups used
self.constraints.append(UnionSelection(self.variables['catgnodes'],
self.variables['groups'],
[g.variables['agnodes'] for
g in self.groups]))
# The set of merged gnodes used is the union of the merged nodes
of the selected category nodes
-
self.constraints.append(UnionSelection(self.variables['merged_gnodes'],
- self.variables['catgnodes'],
- [gn.variables['merge_cgn']
for gn in self.gnodes]))
+#
self.constraints.append(UnionSelection(self.variables['merged_gnodes'],
+#
self.variables['catgnodes'],
+# [gn.variables['merge_cgn']
for gn in self.gnodes]))
# The set of merged gnodes used is the union of the merged gnodes
of all merging snodes
#
self.constraints.append(UnionSelection(self.variables['merged_gnodes'],
#
self.variables['merged_snodes'],
# [sn.variables['mgnodes']
for sn in self.nodes]))
# The set of category gnodes used is the union of the category
nodes associated with all merged snodes
- self.constraints.append(UnionSelection(self.variables['catgnodes'],
-
self.variables['merged_snodes'],
- [sn.variables['agnodes']
for sn in self.nodes]))
+#
self.constraints.append(UnionSelection(self.variables['catgnodes'],
+#
self.variables['merged_snodes'],
+# [sn.variables['agnodes']
for sn in self.nodes]))
# The set of category gnodes used is the union of the category
nodes associated with all merged gnodes
- self.constraints.append(UnionSelection(self.variables['catgnodes'],
-
self.variables['merged_gnodes'],
- [gn.variables['merge_agn']
for gn in self.gnodes]))
+#
self.constraints.append(UnionSelection(self.variables['catgnodes'],
+#
self.variables['merged_gnodes'],
+# [gn.variables['merge_agn']
for gn in self.gnodes]))
# The set of merged snodes used is the union of the snodes
associated with all category nodes used
-
self.constraints.append(UnionSelection(self.variables['merged_snodes'],
- self.variables['catgnodes'],
- [gn.variables['merge_cw']
for gn in self.gnodes]))
+#
self.constraints.append(UnionSelection(self.variables['merged_snodes'],
+#
self.variables['catgnodes'],
+# [gn.variables['merge_cw']
for gn in self.gnodes]))
# The set of merged snodes used is the union of the snodes
associated with all merged gnodes
-
self.constraints.append(UnionSelection(self.variables['merged_snodes'],
-
self.variables['merged_gnodes'],
- [gn.variables['merge_aw']
for gn in self.gnodes]))
+#
self.constraints.append(UnionSelection(self.variables['merged_snodes'],
+#
self.variables['merged_gnodes'],
+# [gn.variables['merge_aw']
for gn in self.gnodes]))
# The set of merged gnodes must be a subset of the set of used
gnodes
- self.constraints.extend(Inclusion([self.variables['merged_gnodes'],
-
self.variables['gnodes']]).constraints)
+#
self.constraints.extend(Inclusion([self.variables['merged_gnodes'],
+#
self.variables['gnodes']]).constraints)
# All snodes must have distinct category nodes
self.constraints.extend(Disjoint([sn.variables['agnodes'] for sn
in self.nodes]).constraints)
# All snodes must have distinct concrete merged nodes nodes
# self.constraints.extend(Disjoint([sn.variables['mgnodes'] for sn
in self.nodes]).constraints)
# All concrete gnodes must have distinct category nodes
- self.constraints.extend(Disjoint([gn.variables['merge_agn'] for gn
in self.gnodes]).constraints)
+# self.constraints.extend(Disjoint([gn.variables['merge_agn'] for
gn in self.gnodes]).constraints)
# All position constraints for snodes

self.constraints.append(PrecedenceSelection(self.variables['gnode_pos'],

[gn.variables['snodes'] for gn in self.gnodes]))
@@ -463,9 +474,9 @@
#
selvars=[g.variables['gnodes_pos'] for g in self.groups],
#
seqvars=[s.variables['gnodes'] for s in self.nodes],
#
mainvars=[g.variables['gnodes'] for g in self.groups]))
- # Set convexity (projectivity) within each group tree
-
self.constraints.append(ComplexSetConvexity(self.variables['groups'],
- [g.variables['tree']
for g in self.groups]))
+# # Set convexity (projectivity) within each group tree
+#
self.constraints.append(ComplexSetConvexity(self.variables['groups'],
+# [g.variables['tree']
for g in self.groups]))
# Agreement
# print("snode variables")
# for sn in self.nodes:
@@ -535,7 +546,6 @@
trees.sort(key=lambda x: x[0])
# Just keep the snode indices in each tree
trees = [x[1][2] for x in trees]
-# trees = [list(g.variables['tree'].get_value(dstore=dstore)) for g
in ginsts]
# Get the indices of the GNodes for each SNode
solution = Solution(self, ginsts, s2gnodes, len(self.solutions),
trees=trees)
self.solutions.append(solution)
@@ -749,7 +759,7 @@
"""Return agr constraints for group, converted to tuples."""
result = []
if self.group.agr:
- for a in self.group.agr[:]:
+ for a in copy.deepcopy(self.group.agr):
feats = [tuple(pair) for pair in a[2:]]
a[2:] = feats
# Convert gnode positions to sentence positions
@@ -806,34 +816,34 @@
set(), set(cand_snodes), self.ncgnodes,
self.ncgnodes)
# set(), set(range(nsnodes)), self.ncgnodes,
self.ncgnodes)
# Other GInsts merged with this one, excluding and including itself
- if self.nanodes == 0:
- # No abstract nodes, so this is a determined variable with one
value (its own index)
- self.variables['merged_groups_incl'] =
DetVar('g{}->mgroups_in'.format(self.index), {self.index})
- self.variables['merged_groups_excl'] = EMPTY
- self.variables['merged_gnodes'] = EMPTY
- else:
-
self.svar('merged_groups_incl', 'g{}->mgroups_in'.format(self.index),
- {self.index}, set(range(ngroups)), 1, self.nanodes+1)
-
self.svar('merged_groups_excl', 'g{}->mgroups_ex'.format(self.index),
- set(), set(range(ngroups)) - {self.index}, 0,
self.nanodes)
+# if self.nanodes == 0:
+# # No abstract nodes, so this is a determined variable with
one value (its own index)
+# self.variables['merged_groups_incl'] =
DetVar('g{}->mgroups_in'.format(self.index), {self.index})
+# self.variables['merged_groups_excl'] = EMPTY
+# self.variables['merged_gnodes'] = EMPTY
+# else:
+#
self.svar('merged_groups_incl', 'g{}->mgroups_in'.format(self.index),
+# {self.index}, set(range(ngroups)), 1,
self.nanodes+1)
+#
self.svar('merged_groups_excl', 'g{}->mgroups_ex'.format(self.index),
+# set(), set(range(ngroups)) - {self.index}, 0,
self.nanodes)
# Set of all gnodes that are merged with abstract gnodes in
this group
# upper bound is all gnodes not in this group
- self.svar('merged_gnodes', 'g{}->mgnodes'.format(self.index),
- set(), set(range(len(self.sentence.gnodes))) -
{gn.sent_index for gn in self.nodes})
+# self.svar('merged_gnodes', 'g{}->mgnodes'.format(self.index),
+# set(), set(range(len(self.sentence.gnodes))) -
{gn.sent_index for gn in self.nodes})
# Trees under GInst head (including self)
- if self.nanodes == 0:
- # No abstract gnodes, so same as gnodes
- v = self.variables['gnodes_pos']
- self.variables['tree'] = v
- # Make this an essential variable
- v.essential=True
- self.sentence.dstore.ess_undet.append(v)
- else:
- self.svar('tree', 'g{}->tree'.format(self.index),
- # at least as long as the number of self's nodes
- set(), set(cand_snodes), self.ngnodes,
len(cand_snodes))
-#, ess=True)
-# set(), set(range(nsnodes)), self.ngnodes, nsnodes,
ess=True)
+# if self.nanodes == 0:
+# # No abstract gnodes, so same as gnodes
+# v = self.variables['gnodes_pos']
+# self.variables['tree'] = v
+# # Make this an essential variable
+# v.essential=True
+# self.sentence.dstore.ess_undet.append(v)
+# else:
+# self.svar('tree', 'g{}->tree'.format(self.index),
+# # at least as long as the number of self's nodes
+# set(), set(cand_snodes), self.ngnodes,
len(cand_snodes))
+##, ess=True)
+## set(), set(range(nsnodes)), self.ngnodes, nsnodes,
ess=True)
# Determined variable for within-source agreement constraints,
gen: 0}
agr = self.get_agr()
if agr:
@@ -927,27 +937,27 @@
nsnodes = len(self.sentence.nodes)
# SNode index for this GNode
self.ivar('snodes', "gn{}->w".format(self.sent_index),
set(self.snode_indices))
- if self.cat:
- # Concrete nodes merged with this abstract node
- self.svar('merge_cgn', 'gn{}_cgmerge'.format(self.sent_index),
- set(), {gn.sent_index for gn in self.sentence.gnodes
if not gn.cat},
- 0, 1)
- self.svar('merge_cw', 'gn{}_cwmerge'.format(self.sent_index),
- set(), set(range(nsnodes)),
- 0, 1)
- self.variables['merge_agn'] = EMPTY
- self.variables['merge_aw'] = EMPTY
- else:
- # Abstract nodes merged with this concrete node
- self.svar('merge_agn', 'gn{}_agmerge'.format(self.sent_index),
- # indices of all abstract nodes
- set(), {gn.sent_index for gn in self.sentence.gnodes
if gn.cat},
- 0, 1)
- self.svar('merge_aw', 'gn{}_awmerge'.format(self.sent_index),
- set(), set(range(nsnodes)),
- 0, 1)
- self.variables['merge_cgn'] = EMPTY
- self.variables['merge_cw'] = EMPTY
+# if self.cat:
+# # Concrete nodes merged with this abstract node
+# self.svar('merge_cgn', 'gn{}_cgmerge'.format(self.sent_index),
+# set(), {gn.sent_index for gn in
self.sentence.gnodes if not gn.cat},
+# 0, 1)
+# self.svar('merge_cw', 'gn{}_cwmerge'.format(self.sent_index),
+# set(), set(range(nsnodes)),
+# 0, 1)
+# self.variables['merge_agn'] = EMPTY
+# self.variables['merge_aw'] = EMPTY
+# else:
+# # Abstract nodes merged with this concrete node
+# self.svar('merge_agn', 'gn{}_agmerge'.format(self.sent_index),
+# # indices of all abstract nodes
+# set(), {gn.sent_index for gn in
self.sentence.gnodes if gn.cat},
+# 0, 1)
+# self.svar('merge_aw', 'gn{}_awmerge'.format(self.sent_index),
+# set(), set(range(nsnodes)),
+# 0, 1)
+# self.variables['merge_cgn'] = EMPTY
+# self.variables['merge_cw'] = EMPTY

class TNode:

@@ -1002,14 +1012,14 @@
for g in self.ginsts:
g.display(word_width=word_width, s2gnodes=self.s2gnodes)

- def translate(self, verbosity=0):
+ def translate(self, verbosity=0, all_sols=False):
"""Do everything you need to create the translation."""
self.merge_nodes(verbosity=verbosity)
for ginst in self.ginsts:
ginst.set_translations(verbosity=verbosity)
- self.make_translations(verbosity=verbosity)
+ self.make_translations(verbosity=verbosity, all_sols=all_sols)

- def make_translations(self, verbosity=0, display=True):
+ def make_translations(self, verbosity=0, display=True, all_sols=False):
"""Combine GInsts for each translation in translation products, and
separate gnodes into a dict for each translation."""
if verbosity:
@@ -1018,13 +1028,16 @@
for index, translation in enumerate(translations):
t = Translation(self, translation, index,
trees=copy.deepcopy(self.trees), verbosity=verbosity)
t.initialize(verbosity=verbosity)
- t.realize(verbosity=verbosity, display=display)
+ t.realize(verbosity=verbosity, display=display,
all_sols=all_sols)
# if display:
# t.display_all()
self.translations.append(t)
+ if all_sols:
+ continue
if not input('SEARCH FOR ANOTHER TRANSLATION FOR THIS
ANALYSIS? [yes/NO] '):
return
- print("No more translations for analysis")
+ if verbosity:
+ print("No more translations for analysis")

def merge_nodes(self, verbosity=0):
"""Merge the source features of cat and inst GNodes associated
with each SNode."""
@@ -1396,7 +1409,7 @@
# for c in self.constraints:
# print(c)

- def realize(self, verbosity=0, display=True):
+ def realize(self, verbosity=0, display=True, all_sols=False):
"""Run constraint satisfaction on the order and disjunction
constraints,
and convert variable values to sentence positions."""
generator = self.solver.generator(test_verbosity=verbosity,
@@ -1414,7 +1427,10 @@
self.display(len(self.outputs)-1)
if verbosity:
print('FOUND REALIZATION {}'.format(self.outputs[-1]))
+ if all_sols:
+ continue
if not input('SEARCH FOR ANOTHER REALIZATION FOR
TRANSLATION? [yes/NO] '):
proceed = False
except StopIteration:
- print('No more realizations for translation')
+ if verbosity:
+ print('No more realizations for translation')

==============================================================================
Revision: 13b733f9d3d8
Branch: default
Author: Michael Gasser <gas...@cs.indiana.edu>
Date: Tue May 20 06:40:51 2014 UTC
Log: Alignment inferred if not explicit.
http://code.google.com/p/hltdi-l3/source/detail?r=13b733f9d3d8

Modified:
/hiiktuu/languages/amh.lg
/hiiktuu/languages/eng.lg
/hiiktuu/languages/spa.lg
/hiiktuu/sentence.py

=======================================
--- /hiiktuu/languages/amh.lg Mon May 19 06:57:46 2014 UTC
+++ /hiiktuu/languages/amh.lg Tue May 20 06:40:51 2014 UTC
@@ -7,8 +7,7 @@
trans:
orm:
- [$food.nyaate_,
- {align: [0, 1],
- agr: [false, {sp: prs, sn: num, sg: gen}]}]
+ {agr: [false, {sp: prs, sn: num, sg: gen}]}]
አወቀ_:
- words: [$vb, አወቀ_]
features: [{tam: ger}, {tam: imf, pol: neg}]
@@ -24,8 +23,7 @@
trans:
orm:
- [$fact.beeke_,
- {align: [0, 1],
- agr: [False, {sp: prs, sn: num, sg: gen}]}]
+ {agr: [False, {sp: prs, sn: num, sg: gen}]}]
አሳ_:
- words: [አሳ_]
trans:
=======================================
--- /hiiktuu/languages/eng.lg Mon May 19 06:57:46 2014 UTC
+++ /hiiktuu/languages/eng.lg Tue May 20 06:40:51 2014 UTC
@@ -68,16 +68,16 @@
- words: [kick_v, the, bucket]
trans:
spa:
- - [estirar_.la.pata, {align: [0, 1, 2], agr: [{tns: tmp}, false,
false]}]
+ - [estirar_.la.pata, {agr: [{tns: tmp}, false, false]}]
- words: [kick_v, $concrete]
trans:
spa:
- - [patear_.$concreto, {align: [0, 1], agr: [{tns: tmp}, false]}]
+ - [patear_.$concreto, {agr: [{tns: tmp}, false]}]
"it's":
- words: ["it's"]
trans:
spa:
- - [es, align: [0]]
+ - [es]
end_n:
- words: [the, end, of, the, world]
trans:
@@ -105,12 +105,12 @@
- words: [them]
trans:
spa:
- - [les, {align: [0]}]
+ - [les]
her:
- words: [her]
trans:
spa:
- - [su, {align: [0]}]
+ - [su]
my:
- words: [my]
his:
@@ -121,4 +121,4 @@
- words: [the, bucket]
trans:
spa:
- - [el.balde, {align: [0, 1]}]
+ - [el.balde]
=======================================
--- /hiiktuu/languages/spa.lg Mon May 19 06:57:46 2014 UTC
+++ /hiiktuu/languages/spa.lg Tue May 20 06:40:51 2014 UTC
@@ -32,7 +32,7 @@
- words: [una, canción]
trans:
eng:
- - [a.song, {align: [0, 1]}]
+ - [a.song]
canciones:
- words: [canciones]
les:
@@ -48,7 +48,7 @@
- words: [el, muchacho]
trans:
eng:
- - [the.boy, {align: [0, 1]}]
+ - [the.boy]
balde:
- words: [el, balde]

=======================================
--- /hiiktuu/sentence.py Tue May 20 06:19:15 2014 UTC
+++ /hiiktuu/sentence.py Tue May 20 06:40:51 2014 UTC
@@ -65,6 +65,7 @@
# 2014.05.19
# -- all_sols argument to solve() and other methods that search finds all
# solutions without querying user.
+# -- Alignments is inferred from lexicon if not explicit.

import itertools, copy
from .ui import *
@@ -858,8 +859,10 @@
translations[i] = [t[0], {'align':
list(range(len(self.nodes)))}]
# print("Translations for {}: {}".format(self, translations))
ntokens = len(self.group.tokens)
- for tgroup, alignment in translations:
-# print("TGroup {}', alignment {}".format(tgroup, alignment))
+ for tgroup, s2t_dict in translations:
+# print("TGroup {}', s2t_dict {}".format(tgroup, s2t_dict))
+ # If there's no explicit alignment, it's the obvious default
+ alignment = s2t_dict.get('align', list(range(ntokens)))
if isinstance(tgroup, str):
# First find the target Group object
tgroup = self.target.groupnames[tgroup]
@@ -869,7 +872,7 @@
if nttokens > ntokens:
# Target group has more nodes than source group.
# Indices of groups that are not empty.
- full_t_indices = set(alignment['align'])
+ full_t_indices = set(alignment)
empty_t_indices = set(range(nttokens)) - full_t_indices
for i in empty_t_indices:
empty_t_token = tgroup.tokens[i]
@@ -881,12 +884,11 @@
# Align gnodes with target tokens and features
tokens = tgroup.tokens
features = tgroup.features
- targ_index = alignment['align'][gn_index]
-# print("Alignment: {}".format(alignment['align']))
+ targ_index = alignment[gn_index]
if targ_index < 0:
# This means there's no target language token
continue
- agrs = alignment['agr'][gn_index] if 'agr' in alignment
else None
+ agrs = s2t_dict['agr'][gn_index] if 'agr' in s2t_dict else
None
token = tokens[targ_index]
feats = features[targ_index] if features else None
gnodes.append((gnode, token, feats, agrs, targ_index))
Reply all
Reply to author
Forward
0 new messages