Skip to content

Commit

Permalink
Merge pull request #26 from unnonouno/regexp
Browse files Browse the repository at this point in the history
Support regexp
  • Loading branch information
unnonouno committed Apr 15, 2016
2 parents 4f78ce2 + 9f94857 commit bf9b0be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python:
- "3.4"

install:
- pip install coverage==3.7.1
- pip install tornado
- pip install coveralls
- wget https://mecab.googlecode.com/files/mecab-0.996.tar.gz
Expand Down
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ Pattern
`<pos=XXX>`
matches morphemes whose POS are `XXX`

`<feature=XXX>`
matches morphemes whose features are `XXX`

`<feature=~XXX>`
matches morphemes whose features maches a RegExp pattern `XXX`

`X*`
matches repetiion of a pattern X

Expand Down
21 changes: 15 additions & 6 deletions mrep/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,22 @@ def term(s, pos):
p = consume(s, p, ')')
return (p, t)
elif c == '<':
m = re.match(r'<([^>]+)=~([^>]+)>', s[pos:])
if m:
p = pos + m.end()
key = m.group(1)
pat = m.group(2)
return p, pattern.Condition(
lambda x: key in x and re.search(pat, x[key]))

m = re.match(r'<([^>]+)=([^>]+)>', s[pos:])
if not m:
raise InvalidPattern(pos)
p = pos + m.end()
key = m.group(1)
value = m.group(2)
return p, pattern.Condition(lambda x: key in x and x[key] == value)
if m:
p = pos + m.end()
key = m.group(1)
value = m.group(2)
return p, pattern.Condition(lambda x: key in x and x[key] == value)

raise InvalidPattern(pos)

elif c == '.':
return pos + 1, pattern.Condition(lambda x: True)
Expand Down
9 changes: 9 additions & 0 deletions test/builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ def testCondition(self):
t.match([{'x': 'y'}], 0, lambda s, p: ps.append(p))
self.assertEqual([1], ps)

def testPatternCondition(self):
p, t = mrep.builder.term('<x=~y>', 0)
self.assertIsNotNone(t)
self.assertEqual(6, p)
self.assertEqual('.', repr(t))
ps = []
t.match([{'x': 'zyw'}], 0, lambda s, p: ps.append(p))
self.assertEqual([1], ps)

def testParen(self):
p, t = mrep.builder.term('(.)', 0)
self.assertIsNotNone(t)
Expand Down

0 comments on commit bf9b0be

Please sign in to comment.