def to_method_object(self): """Convert the enum to an instance of `BaselineMethod`.""" if self == self.TF_IDF: return keyword_based.TfIdfMethod() elif self == self.BM25: return keyword_based.BM25Method() elif self == self.USE_SIM: return vector_based.VectorSimilarityMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder/2")) elif self == self.USE_LARGE_SIM: return vector_based.VectorSimilarityMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder-large/3")) elif self == self.ELMO_SIM: return vector_based.VectorSimilarityMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/elmo/1")) elif self == self.USE_MAP: return vector_based.VectorMappingMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder/2")) elif self == self.USE_LARGE_MAP: return vector_based.VectorMappingMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder-large/3")) elif self == self.ELMO_MAP: return vector_based.VectorMappingMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/elmo/1")) raise ValueError("Unknown method {}".format(self))
def test_train_test(self): """Check that it can correctly rank a simple example.""" method = keyword_based.TfIdfMethod() method.train(["hello how are you", "hello how are"], ["hello how", "hello"]) predictions = method.rank_responses(["hello", "how", "are", "you"], ["you", "are", "how", "hello"]) self.assertEqual(list(predictions), [3, 2, 1, 0])
def test_train_test_idf(self): """Check that the keyword with higher idf counts for more.""" method = keyword_based.TfIdfMethod() method.train(["hello how are you", "hello how are"], ["hello how", "hello"]) predictions = method.rank_responses(["hello you", "hello you"], ["hello", "you"]) self.assertEqual( list(predictions), [1, 1] # you is more informative than 'hello'. )
def to_method_object(self): """Convert the enum to an instance of `BaselineMethod`.""" if self == self.TF_IDF: return keyword_based.TfIdfMethod() elif self == self.BM25: return keyword_based.BM25Method() elif self == self.USE_SIM: return vector_based.VectorSimilarityMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder/2")) elif self == self.USE_LARGE_SIM: return vector_based.VectorSimilarityMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder-large/3")) elif self == self.ELMO_SIM: return vector_based.VectorSimilarityMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/elmo/1")) elif self == self.USE_MAP: return vector_based.VectorMappingMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder/2")) elif self == self.USE_LARGE_MAP: return vector_based.VectorMappingMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder-large/3")) elif self == self.ELMO_MAP: return vector_based.VectorMappingMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/elmo/1")) elif self == self.BERT_SMALL_SIM: return vector_based.VectorSimilarityMethod( encoder=vector_based.BERTEncoder( "https://tfhub.dev/google/" "bert_uncased_L-12_H-768_A-12/1")) elif self == self.BERT_SMALL_MAP: return vector_based.VectorMappingMethod( encoder=vector_based.BERTEncoder( "https://tfhub.dev/google/" "bert_uncased_L-12_H-768_A-12/1")) elif self == self.BERT_LARGE_SIM: return vector_based.VectorSimilarityMethod( encoder=vector_based.BERTEncoder( "https://tfhub.dev/google/" "bert_uncased_L-24_H-1024_A-16/1")) elif self == self.BERT_LARGE_MAP: return vector_based.VectorMappingMethod( encoder=vector_based.BERTEncoder( "https://tfhub.dev/google/" "bert_uncased_L-24_H-1024_A-16/1")) elif self == self.USE_QA: return vector_based.VectorSimilarityMethod( encoder=vector_based.TfHubEncoder( "https://tfhub.dev/google/" "universal-sentence-encoder-multilingual-qa/1", is_dual=True)) raise ValueError("Unknown method {}".format(self))