Skip to content

Commit

Permalink
FIX: better property based testing for lobe utils
Browse files Browse the repository at this point in the history
  • Loading branch information
davidawad committed Jul 26, 2018
1 parent 642ab2f commit 203e59c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
12 changes: 7 additions & 5 deletions app/nlp_tools/proc_english.py
@@ -1,12 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""
This function can split the entire text of Huckleberry Finn into sentences in about 0.1 seconds
and handles many of the more painful edge cases that make sentence parsing non-trivial.
e.g. 'Mr. John Johnson Jr. was born in the U.S.A but earned his Ph.D. in Israel before joining Nike Inc. as an engineer. He also worked at craigslist.org as a business analyst.'
e.g. 'Mr. John Johnson Jr. was born in the U.S.A but earned his Ph.D. in Israel before joining Nike Inc. as an engineer.'
from here: https://stackoverflow.com/a/31505798
"""
Expand All @@ -20,7 +18,11 @@
acronyms = "([A-Z][.][A-Z][.](?:[A-Z][.])?)"
websites = "[.](com|net|org|io|gov)"


def split_into_sentences(text):
if not text:
return []

text = " " + text + " "
text = text.replace("\n", " ")
text = re.sub(prefixes, "\\1<prd>", text)
Expand All @@ -31,8 +33,8 @@ def split_into_sentences(text):
text = re.sub(caps + "[.]" + caps + "[.]" + caps + "[.]", "\\1<prd>\\2<prd>\\3<prd>", text)
text = re.sub(caps + "[.]" + caps + "[.]", "\\1<prd>\\2<prd>", text)
text = re.sub(" " + suffixes + "[.] " + starters, " \\1<stop> \\2", text)
text = re.sub(" " + suffixes + "[.]"," \\1<prd>", text)
text = re.sub(" " + caps + "[.]"," \\1<prd>", text)
text = re.sub(" " + suffixes + "[.]", " \\1<prd>", text)
text = re.sub(" " + caps + "[.]", " \\1<prd>", text)
if "”" in text: text = text.replace(".”", "”.")
if "\"" in text: text = text.replace(".\"", "\".")
if "!" in text: text = text.replace("!\"", "\"!")
Expand Down
8 changes: 7 additions & 1 deletion app/processing.py
Expand Up @@ -63,10 +63,16 @@ def process_user_message(current_user: User) -> None:
return

if 'TEST' in most_recent_message:

if current_user.state is None:
current_user.send_text("I don't know where you live, send location?")
current_user.request_location()
return

# debugging mode, send request for location
query = most_recent_message.split(' ')[1]

# ask the intern to figure it out.
# send query to intern
laws = utils.chase_lookup(query=query,
state=current_user.state)

Expand Down
12 changes: 10 additions & 2 deletions app/utils.py
Expand Up @@ -44,15 +44,23 @@ def find_state_from_coords(lat, long) -> str:

res = search.by_coordinate(lat, long, radius=30)

if not res:
return None

# get zipcode based on lat / long
zipcode = res[0]['Zipcode']

print(zipcode)

if not zipcode:
return None

# use zipcode object to determine state
zipcode_object = search.by_zipcode(zipcode)

city = zipcode_object['State'] # NY
state = zipcode_object['State'] # NY

return city
return state


def chase_lookup(**kwargs):
Expand Down
36 changes: 19 additions & 17 deletions test/unit/test_utils.py
Expand Up @@ -4,19 +4,20 @@
"""
unit tests for utils
"""

# add parent folders to system path to import classes to testing
import sys
sys.path.append('../')

# testing tools
from hypothesis import given, example
from hypothesis.strategies import text, floats

from nlp_tools.proc_english import split_into_sentences
from processing import determine_reply_from_intent
from utils import find_state_from_coords
from test_conf import *




class TestUtils(object):
"""
Unit Tests for Utils
Expand All @@ -29,18 +30,19 @@ def test_find_state(self):
assert find_state_from_coords(37.483872693672, -122.14900441942) == 'CA'
assert find_state_from_coords(40.483872693672, -73.9321059) == 'NY'

# def test_find_state(self):

# @fixture(scope='function')
# def stuff():
# global counter
# counter = 0


# @given(a=st.none())
# def test_stuff(a, stuff):
# global counter
# counter += 1
# assert counter == 1

@given(floats(min_value=-180.00, max_value=180.00), floats(min_value=-180.00, max_value=180.00))
def test_find_state(self, lat, long):
"""
test for any coordinates
"""
res = find_state_from_coords(lat, long)
assert (isinstance(res, str) or res is None)

@given(text())
@example('')
@example(None)
def test_split_sentences(self, s):
"""
split text should work for all strings
"""
assert isinstance(split_into_sentences(s), list)

0 comments on commit 203e59c

Please sign in to comment.