コード例 #1
0
 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))
コード例 #2
0
    def test_rank_responses(self):
        mock_encoder = mock.Mock()
        mock_encoder.encode.return_value = np.asarray([
            [1, 0, 0],
            [0, 1, 0],
            [0, 1, 1],
        ],
                                                      dtype=np.float32)

        method = vector_based.VectorSimilarityMethod(mock_encoder)
        assignments = method.rank_responses(["x", "y", "z"], ["a", "b", "c"])
        np.testing.assert_allclose([0, 1, 2], assignments)
        mock_encoder.encode.assert_has_calls([
            mock.call(["x", "y", "z"]),
            mock.call(["a", "b", "c"]),
        ])
コード例 #3
0
 def test_train(self):
     vector_based.VectorSimilarityMethod(None).train(["x", "y"], ["a", "b"])
コード例 #4
0
 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))