Skip to content

Commit

Permalink
Fix SVO logic adapter try gunthercox#1
Browse files Browse the repository at this point in the history
Removed regex4dummies & just used Pattern
  • Loading branch information
DarkmatterVale committed Nov 9, 2015
1 parent 9750da1 commit ef58be1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
64 changes: 45 additions & 19 deletions chatterbot/adapters/logic/closest_svo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .logic import LogicAdapter

from regex4dummies import Toolkit
from pattern.en import parse
import re


class ClosestSVOAdapter(LogicAdapter):
Expand All @@ -9,40 +10,31 @@ 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()
# Instantiating similarity variable
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' )
dependencies1 = self.simplify( text=parse( string1, relations=True ) )
dependencies2 = self.simplify( text=parse( string2, relations=True ) )

print dependencies1
print dependencies2

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

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

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

# Returning the found similarity
Expand Down Expand Up @@ -75,3 +67,37 @@ def get(self, text, list_of_statements):
closest_statement = statement

return closest_statement


def simplify( self, **kwargs ):
"""
Returns the simplified version of the input text.
"""

# Instantiating variables
processed_text = kwargs.get("text")
svo_simplified = []
subject = ""
verb = ""
mobject = ""

# Getting the subject
for word in processed_text.split( ' ' ):
# If the word is the subject of the sentence, add it as the subject
if "SBJ-" in word:
subject += " " + re.sub( '/.*', '', word )
elif "VP-" in word:
verb += " " + re.sub( '/.*', '', word )
elif "OBJ-" in word:
mobject += " " + re.sub( '/.*', '', word )

# Removing extra spaces
subject = subject[ 1 : ]
verb = verb[ 1 : ]
mobject = mobject[ 1 : ]

# Assembling information
svo_simplified = [ [ "subject", subject ], [ "verb", verb ], [ "object", mobject ] ]

# Returning information
return svo_simplified
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ jsondatabase>=0.0.6
pymongo==3.0.3
requests==2.7.0
requests-oauthlib==0.5.0
regex4dummies==1.4.4
pattern==2.6
2 changes: 1 addition & 1 deletion tests/logic_adapter_tests/test_closest_svo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_get_closest_statement(self):

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

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

def test_no_choices(self):
possible_choices = []
Expand Down

0 comments on commit ef58be1

Please sign in to comment.