Example #1
0
 def test_get_field(self):
     ds = DataSet({"x": [[1, 2, 3, 4]] * 10, "y": [[5, 6]] * 10})
     ans = ds.get_field("x")
     self.assertTrue(isinstance(ans, FieldArray))
     self.assertEqual(ans.content, [[1, 2, 3, 4]] * 10)
     ans = ds.get_field("y")
     self.assertTrue(isinstance(ans, FieldArray))
     self.assertEqual(ans.content, [[5, 6]] * 10)
Example #2
0
    def predict(self, content):
        """
        分词接口。

        :param content: str或List[str], 例如: "中文分词很重要!", 返回的结果是"中文 分词 很 重要 !"。 如果传入的为List[str],比如
            [ "中文分词很重要!", ...], 返回的结果["中文 分词 很 重要 !", ...]。
        :return: str或List[str], 根据输入的的类型决定。
        """
        if not hasattr(self, 'pipeline'):
            raise ValueError("You have to load model first.")

        sentence_list = []
        # 1. 检查sentence的类型
        if isinstance(content, str):
            sentence_list.append(content)
        elif isinstance(content, list):
            sentence_list = content

        # 2. 组建dataset
        dataset = DataSet()
        dataset.add_field('raw_sentence', sentence_list)

        # 3. 使用pipeline
        self.pipeline(dataset)

        output = dataset.get_field('output').content
        if isinstance(content, str):
            return output[0]
        elif isinstance(content, list):
            return output