Skip to content

Commit

Permalink
Add convenience methods for deriv (pre)terminals
Browse files Browse the repository at this point in the history
Fixes #105
  • Loading branch information
goodmami committed Jun 1, 2017
1 parent 831cd77 commit 688b487
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* `delphin.itsdb.ItsdbProfile.exists()` (#112)
* `delphin.itsdb.ItsdbProfile.size()` (#112)
* `--in-place` option to the `delphin mkprof` command (#109)
* `delphin.derivation.UdfNode.preterminals()` (#105)
* `delphin.derivation.UdfNode.terminals()` (#105)

### Changed

Expand Down
26 changes: 26 additions & 0 deletions delphin/derivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,32 @@ def lexical_type(self):
warnings.warn('Deprecated; try UdfNode.type', DeprecationWarning)
return self.type

# Convenience methods

def preterminals(self):
"""
Return the list of preterminals (i.e. lexical grammar-entities).
"""
nodes = []
for dtr in self.daughters:
if isinstance(dtr, UdfTerminal):
nodes.append(self)
else:
nodes.extend(dtr.preterminals())
return nodes

def terminals(self):
"""
Return the list of terminals (i.e. lexical units).
"""
nodes = []
for dtr in self.daughters:
if isinstance(dtr, UdfTerminal):
nodes.append(dtr)
else:
nodes.extend(dtr.terminals())
return nodes

class Derivation(UdfNode):
"""
A class for reading, writing, and storing derivation trees. Objects
Expand Down
32 changes: 32 additions & 0 deletions tests/derivation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,38 @@ def test_lexical_type(self):
assert node.daughters[0].lexical_type() == 'a-type_le'
assert node.daughters[1].lexical_type() == 'b-type_le'

def test_preterminals(self):
a = D.from_string('(root (1 some-thing -1 -1 -1'
' (2 a-thing -1 -1 -1 ("a"))'
' (3 b-thing -1 -1 -1 ("b"))))')
assert [t.id for t in a.preterminals()] == [2, 3]
a = D.from_string('(root'
' (1 some-thing@some-type 0.4 0 5'
' (2 a-lex@a-type 0.8 0 1'
' ("a b"'
' 3 "token [ +FORM \\"a\\" ]"'
' 4 "token [ +FORM \\"b\\" ]"))'
' (5 b-lex@b-type 0.9 1 2'
' ("b"'
' 6 "token [ +FORM \\"b\\" ]"))))')
assert [t.id for t in a.preterminals()] == [2, 5]

def test_terminals(self):
a = D.from_string('(root (1 some-thing -1 -1 -1'
' (2 a-thing -1 -1 -1 ("a"))'
' (3 b-thing -1 -1 -1 ("b"))))')
assert [t.form for t in a.terminals()] == ['a', 'b']
a = D.from_string('(root'
' (1 some-thing@some-type 0.4 0 5'
' (2 a-lex@a-type 0.8 0 1'
' ("a b"'
' 3 "token [ +FORM \\"a\\" ]"'
' 4 "token [ +FORM \\"b\\" ]"))'
' (5 b-lex@b-type 0.9 1 2'
' ("b"'
' 6 "token [ +FORM \\"b\\" ]"))))')
assert [t.form for t in a.terminals()] == ['a b', 'b']

def test_to_udf(self):
s = '(1 some-thing -1 -1 -1 ("token"))'
assert D.from_string(s).to_udf(indent=None) == s
Expand Down

0 comments on commit 688b487

Please sign in to comment.