forked from PaddlePaddle/PaddleHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
77 lines (62 loc) · 2.71 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import shutil
import unittest
import paddlehub as hub
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
class TestHubModule(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.text = "今天是个好日子"
cls.texts = ["今天是个好日子", "天气预报说今天要下雨", "下一班地铁马上就要到了"]
cls.module = hub.Module(name="lac")
@classmethod
def tearDownClass(cls) -> None:
shutil.rmtree('inference')
def test_cut1(self):
results = self.module.cut(text=self.text, use_gpu=False, batch_size=1, return_tag=False)
self.assertEqual(results, ['今天', '是', '个', '好日子'])
def test_cut2(self):
results = self.module.cut(text=self.texts, use_gpu=False, batch_size=1, return_tag=False)
self.assertEqual(results, [{
'word': ['今天', '是', '个', '好日子']
}, {
'word': ['天气预报', '说', '今天', '要', '下雨']
}, {
'word': ['下', '一班', '地铁', '马上', '就要', '到', '了']
}])
def test_cut3(self):
results = self.module.cut(text=self.texts, use_gpu=False, batch_size=2, return_tag=False)
self.assertEqual(results, [{
'word': ['今天', '是', '个', '好日子']
}, {
'word': ['天气预报', '说', '今天', '要', '下雨']
}, {
'word': ['下', '一班', '地铁', '马上', '就要', '到', '了']
}])
def test_cut4(self):
results = self.module.cut(text=self.texts, use_gpu=True, batch_size=2, return_tag=False)
self.assertEqual(results, [{
'word': ['今天', '是', '个', '好日子']
}, {
'word': ['天气预报', '说', '今天', '要', '下雨']
}, {
'word': ['下', '一班', '地铁', '马上', '就要', '到', '了']
}])
def test_cut5(self):
results = self.module.cut(text=self.texts, use_gpu=True, batch_size=2, return_tag=True)
self.assertEqual(results, [{
'word': ['今天', '是', '个', '好日子'],
'tag': ['TIME', 'v', 'q', 'n']
}, {
'word': ['天气预报', '说', '今天', '要', '下雨'],
'tag': ['n', 'v', 'TIME', 'v', 'v']
}, {
'word': ['下', '一班', '地铁', '马上', '就要', '到', '了'],
'tag': ['f', 'm', 'n', 'd', 'v', 'v', 'xc']
}])
def test_save_inference_model(self):
self.module.save_inference_model('./inference/model')
self.assertTrue(os.path.exists('./inference/model.pdmodel'))
self.assertTrue(os.path.exists('./inference/model.pdiparams'))
if __name__ == '__main__':
unittest.main()