-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DarkmatterVale-SVO-Logic-Adapter'
- Loading branch information
Showing
5 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Should this index be 2 ?
For eg.
if dependencies1[ 2 ] == dependencies2[ 2 ]: