Skip to content

Commit

Permalink
lint passed
Browse files Browse the repository at this point in the history
  • Loading branch information
xuchen committed Jun 18, 2015
1 parent 1fd0a32 commit 056f73a
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 44 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ lint:
flake8 parsetron test

test:
py.test
# py.test
py.test --genscript=runtests.py
python2.7 runtests.py
pypy runtests.py
rm runtests.py

test-all:
tox
Expand Down
23 changes: 21 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Parsetron - A natural language semantic parser
.. image:: https://pypip.in/d/parsetron/badge.png
:target: https://pypi.python.org/pypi/parsetron

.. image:: https://readthedocs.org/projects/parsetron/badge/
:alt: Documentation Status
:scale: 100%
:target: https://readthedocs.org/projects/parsetron/


A natural language semantic parser

Expand All @@ -19,12 +24,26 @@ Installation

``pip install parsetron``

Parsetron is tested under Python 2.7.
Parsetron is tested under Python 2.7 and Pypy. It doesn't support Python 3 yet.

Dependencies
------------

None. Parsetron is a single ``parsetron.py`` file
None. Parsetron is a single ``parsetron.py`` file.

Grammar Modules
---------------

Parsetron supports modularized grammars: each grammar focuses on an individual
small domain and can be imported via, for instance::

from parsetron.grammars.colors import ColorsGrammar

class YourCustomizedGrammar(Grammar):
color = ColorsGrammar.GOAL


You are welcome to contribute your own grammar here. Send us a pull request!

Development
-----------
Expand Down
8 changes: 5 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ Parsetron, a Robust Semantic Parser

Parsetron is a robust incremental natural language parser utilizing semantic
grammars for small focused domains. It is mainly used to convert natural
language command to executable API calls (e.g., *"blink my light twice"*).
Parsetron is written in pure Python (2.7), portable (a single ``parsetron.py``
file), and can be used in conjunction with a speech recognizer.
language command to executable API calls (e.g.,
*"set my bedroom light to red"* --> ``set_light('bedroom', [255, 0, 0])``).
Parsetron is written in pure Python (2.7), portable (a single
``parsetron.py`` file), and can be used in conjunction with a speech
recognizer.

:Author: Xuchen Yao from `KITT.AI <http://kitt.ai>`_

Expand Down
2 changes: 1 addition & 1 deletion parsetron/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import parsetron
from parsetron import *
from parsetron import * # NOQA

__author__ = 'KITT.AI'
__version__ = parsetron.__version__
3 changes: 2 additions & 1 deletion parsetron/grammars/colored_light.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from parsetron import *
from parsetron import Set, Grammar, Optional, ZeroOrMore, OneOrMore, \
RobustParser
from parsetron.grammars.times import TimesGrammar
from parsetron.grammars.colors import ColorsGrammar

Expand Down
2 changes: 1 addition & 1 deletion parsetron/grammars/colors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from parsetron import *
from parsetron import Grammar, Set, RobustParser

__author__ = 'Xuchen Yao'

Expand Down
6 changes: 4 additions & 2 deletions parsetron/grammars/numbers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from parsetron import *
from parsetron import String, Grammar, Regex, Or, Optional, ZeroOrMore, \
OneOrMore, RobustParser

__author__ = 'Xuchen Yao'


Expand All @@ -10,7 +12,7 @@ def result_sum(r):
# result could either be a list or a single item
try:
r.set(sum(r.get()))
except TypeError: # not a list
except TypeError: # not a list
r.set(r.get())


Expand Down
3 changes: 2 additions & 1 deletion parsetron/grammars/times.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from parsetron import *
from parsetron import Grammar, Or, Set, RobustParser
from parsetron.grammars.numbers import replaced_string, NumbersGrammar

__author__ = 'Xuchen Yao'


class TimesGrammar(Grammar):

special_maps = [
Expand Down
6 changes: 3 additions & 3 deletions parsetron/parsetron.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,8 +2242,8 @@ def _most_compact_trees(self, parent_edge, tokens=None):
for child_edge in children_edges]
child_trees_list.append(child_trees)
# we select from whoever's children are the smallest
cc = sorted([(sum([t[0].size() for t in child_trees]),
child_trees) for child_trees in child_trees_list])
cc = sorted([(sum([t[0].size() for t in c_trees]),
c_trees) for c_trees in child_trees_list])
child_trees = cc[0][1]
for t in itertools.product(*child_trees):
trees.append(TreeNode(parent_edge, t, lexicon))
Expand Down Expand Up @@ -2867,7 +2867,7 @@ def print_parse(self, string, all_trees=False, only_goal=True,
goal=self.goal if only_goal else None))
if len(trees) == 0:
raise ParseException("No parse trees found")
except ParseException as e:
except ParseException:
print("can't parse:", string, file=sys.stderr)
return False

Expand Down
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def finalize_options(self):
self.test_suite = True

def run_tests(self):
#import here, cause outside the eggs aren't loaded
# import here, cause outside the eggs aren't loaded
import pytest
errno = pytest.main(self.pytest_args)
sys.exit(errno)
Expand All @@ -51,11 +51,15 @@ def run_tests(self):
url='https://github.com/Kitt-AI/parsetron',
packages=[
'parsetron',
'parsetron.grammars',
],
package_dir={'parsetron': 'parsetron'},
include_package_data=True,
install_requires=[
],
setup_requires=[
"flake8"
],
license='Apache',
zip_safe=False,
keywords='parsetron',
Expand All @@ -65,12 +69,9 @@ def run_tests(self):
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: Implementation :: PyPy',
],
tests_require=['pytest'],
cmdclass = {'test': PyTest},
cmdclass={'test': PyTest},
)
50 changes: 27 additions & 23 deletions test/time_parsetron.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
from parsetron import *
from parsetron import RobustParser, TopDownStrategy, BottomUpStrategy, \
LeftCornerStrategy
import time
import sys

sents = [
# (True, "go hopkins jays quick"),
(True, "lights please on"),
(True, "flash both top and bottom light with red color and middle light with green"),
(True, "flash middle light twice with red and top once"),
(True, "flash middle light twice red top once"),
(True, "on top"),
(True, "flash middle and top light "),
(True, "change my top light to red and middle to yellow then bottom blue"),
(True, "turn on lights please"),
(True, "I want to turn off the top light please"),
(True, "I want to turn off the lights please"),
(True, "I want to blink top lights"),
(True, "change top lights to red"),
(True, "change top to red and bottom to yellow"),
(True, "kill top lights for me"),
(True, "blink top lights twice"),
(True, "turn lights on"),
(True, "blink top"),
# (False, "I want to turn ")
]
(True, "lights please on"),
(True, "flash both top and bottom light with red color and middle light "
"with green"),
(True, "flash middle light twice with red and top once"),
(True, "flash middle light twice red top once"),
(True, "on top"),
(True, "flash middle and top light "),
(True, "change my top light to red and middle to yellow then bottom blue"),
(True, "turn on lights please"),
(True, "I want to turn off the top light please"),
(True, "I want to turn off the lights please"),
(True, "I want to blink top lights"),
(True, "change top lights to red"),
(True, "change top to red and bottom to yellow"),
(True, "kill top lights for me"),
(True, "blink top lights twice"),
(True, "turn lights on"),
(True, "blink top"),
# (False, "I want to turn ")
]

time_grammar_s = time.time()
from parsetron.grammars import colored_light
Expand All @@ -35,12 +36,13 @@
print >> sys.stderr, \
"grammar init time: %.2f seconds" % (time_grammar_e-time_grammar_s)


def test_code(parser):
global sents
for (expect, sent) in sents:
assert expect == parser.print_parse(sent), sent

# warm up
# warm up
try:
if sys.argv[1] == 'warmup':
for i in range(20):
Expand All @@ -50,6 +52,7 @@ def test_code(parser):
except:
pass


def parse_time(strategy):
iterations = 5
if strategy == 0:
Expand Down Expand Up @@ -80,4 +83,5 @@ def parse_time(strategy):
bu_elapsed, bu_per = parse_time(1)
lc_elapsed, lc_per = parse_time(2)

print >> sys.stderr, "\naverage time per parse: %.2f ms" % ((td_per+bu_per+lc_per)/3)
print >> sys.stderr, "\naverage time per parse: %.2f ms" % \
((td_per+bu_per+lc_per)/3)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py26, py27, py33, style, docs
envlist = py27, pypy, style, docs

[testenv]
setenv =
Expand Down

0 comments on commit 056f73a

Please sign in to comment.