-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpkuseg_jieba.py
More file actions
38 lines (31 loc) · 1.32 KB
/
Copy pathpkuseg_jieba.py
File metadata and controls
38 lines (31 loc) · 1.32 KB
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
from jieba.analyse.tfidf import TFIDF as JiebaTFIDF
import pkuseg
import jieba
class Pkuseg(pkuseg.pkuseg):
"""Customed pkuseg class. Custom the output of this class to be
same as the jieba.cut interface.
"""
def __init__(self, model_name="default", user_dict="default", postag=False):
super().__init__(model_name, user_dict, postag)
def cut(self, text):
"""Custom the output of this method to be same as the jieba.cut method."""
res = super().cut(text)
if len(res) > 0 and isinstance(res[0], tuple):
res = [jieba.posseg.pair(*it) for it in res]
return res
class CustomTFIDF(JiebaTFIDF):
"""Customed TFIDF class. Inherited from jieba.analyse.tfidf.TFIDF.
This class can support the pukseg word cutting api. This class is
implemented with single instance mode.
"""
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(CustomTFIDF, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self, use_pkuseg=True, idf_path=None):
"""Custom this class to suppoer the pkuseg word cutting api."""
super().__init__(idf_path=idf_path)
if use_pkuseg:
self.tokenizer = Pkuseg()
self.postokenizer = Pkuseg(postag=True)