def synset_distance(statement, other_statement):
 from nltk.corpus import wordnet
 from nltk import word_tokenize
 from chatterbot import utils
 import itertools
 tokens1 = word_tokenize(statement.text.lower())
 tokens2 = word_tokenize(other_statement.text.lower())
 tokens1 = utils.remove_stopwords(tokens1, language='english')
 tokens2 = utils.remove_stopwords(tokens2, language='english')
 max_possible_similarity = max(len(statement.text.split()), len(other_statement.text.split()))
 max_similarity = 0.0
 for combination in itertools.product(*[tokens1, tokens2]):
  synset1 = wordnet.synsets(combination[0])
  synset2 = wordnet.synsets(combination[1])
  if (synset1 and synset2):
   for synset in itertools.product(*[synset1, synset2]):
    similarity = synset[0].path_similarity(synset[1])
    if (similarity and (similarity > max_similarity)):
     max_similarity = similarity
 if (max_possible_similarity == 0):
  return 0
 return (max_similarity / max_possible_similarity)

def synset_distance ( statement , other_statement ) : from nltk . corpus import wordnet from nltk import word_tokenize from chatterbot import utils import itertools tokens1 = word_tokenize ( statement . text . lower ( ) ) tokens2 = word_tokenize ( other_statement . text . lower ( ) ) tokens1 = utils . remove_stopwords ( tokens1 , language = 'english' ) tokens2 = utils . remove_stopwords ( tokens2 , language = 'english' ) max_possible_similarity = max ( len ( statement . text . split ( ) ) , len ( other_statement . text . split ( ) ) ) max_similarity = 0.0 for combination in itertools . product ( * [ tokens1 , tokens2 ] ) : synset1 = wordnet . synsets ( combination [ 0 ] ) synset2 = wordnet . synsets ( combination [ 1 ] ) if ( synset1 and synset2 ) : for synset in itertools . product ( * [ synset1 , synset2 ] ) : similarity = synset [ 0 ] . path_similarity ( synset [ 1 ] ) if ( similarity and ( similarity > max_similarity ) ) : max_similarity = similarity if ( max_possible_similarity == 0 ) : return 0 return ( max_similarity / max_possible_similarity )
calculate the similarity of two statements .