Skip to content

Commit

Permalink
Merge branch 'DarkmatterVale-SVO-Logic-Adapter'
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Nov 8, 2015
2 parents 4ea4936 + 0723672 commit 8620211
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
1 change: 1 addition & 0 deletions chatterbot/adapters/logic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .logic import LogicAdapter
from .closest_match import ClosestMatchAdapter
from .closest_meaning import ClosestMeaningAdapter
from .closest_svo import ClosestSVOAdapter
77 changes: 77 additions & 0 deletions chatterbot/adapters/logic/closest_svo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from .logic import LogicAdapter

from regex4dummies import Toolkit


class ClosestSVOAdapter(LogicAdapter):

def __init__(self):
super(ClosestSVOAdapter, self).__init__()


def get_dependencies(self, text):
"""
Takes a string and converts it to its
dependecies separated in a list
"""
dependences = []

return dependencies


def get_similarity(self, string1, string2):
"""
Calculate the similarity of two statements.
This is based on the total similarity between
each word in each sentence.
"""
import re

# Instantiating variables
similarity_tester = Toolkit()
similarity = 0

# Getting the dependencies of the strings
dependencies1 = similarity_tester.find_dependencies( text=string1, parser='pattern', response_type='simplified' )
dependencies2 = similarity_tester.find_dependencies( text=string2, parser='pattern', response_type='simplified' )

# Comparing the dependencies & generating similarity
if dependencies1[ 0 ] == dependencies2[ 0 ]:
similarity += 1

if dependencies1[ 1 ] == dependencies2[ 1 ]:
similarity += 1

if dependencies1[ 1 ] == dependencies2[ 2 ]:

This comment has been minimized.

Copy link
@pradeephrish

pradeephrish Nov 9, 2015

Should this index be 2 ?

For eg.

if dependencies1[ 2 ] == dependencies2[ 2 ]:

similarity += 1

# Returning the found similarity
return similarity


def get(self, text, list_of_statements):
"""
Takes a statement string and a list of statement strings.
Returns the closest matching statement from the list.
"""

# Check if there is no options
if not list_of_statements:
return text

# Check if an exact match exists
if text in list_of_statements:
return text

closest_statement = list_of_statements[0]
closest_similarity = 0

# For each option in the list of options
for statement in list_of_statements:
similarity = self.get_similarity(text, statement)

if similarity > closest_similarity:
closest_similarity = similarity
closest_statement = statement

return closest_statement
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ nltk<4.0.0
pymongo>=3.0.3,<4.0.0
requests~=2.8.0,<3.0.0
requests-oauthlib==0.5.0
regex4dummies==1.4.4
25 changes: 25 additions & 0 deletions tests/logic_adapter_tests/test_closest_svo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from unittest import TestCase
from chatterbot.adapters.logic import ClosestSVOAdapter


class ClosestSVOAdapterTests(TestCase):

def setUp(self):
self.adapter = ClosestSVOAdapter()

def test_get_closest_statement(self):
possible_choices = [
"This is a lovely bog.",
"This is a beautiful swamp.",
"It smells like swamp."
]

close = self.adapter.get("This is a lovely swamp.", possible_choices)

self.assertEqual("This is a beautiful swamp.", close)

def test_no_choices(self):
possible_choices = []
close = self.adapter.get("What is your quest?", possible_choices)

self.assertEqual("What is your quest?", close)
1 change: 0 additions & 1 deletion tests/test_chatbot_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,3 @@ def test_database_is_not_updated_when_read_only(self):

self.assertFalse(exists_before)
self.assertFalse(exists_after)

0 comments on commit 8620211

Please sign in to comment.