diff --git a/pyhanlp/__init__.py b/pyhanlp/__init__.py index 16a9285..ecde73c 100644 --- a/pyhanlp/__init__.py +++ b/pyhanlp/__init__.py @@ -81,30 +81,19 @@ ''' attach_jvm_to_thread = lambda : None if isThreadAttachedToJVM() else attachThreadToJVM() -class CustomDictionaryWrapper(object): - def __init__(self): - self.proxy = JClass('com.hankcs.hanlp.dictionary.CustomDictionary') # HanLP工具类 +class AttachJVMWrapper(object): + def __init__(self, class_name, is_construct = False): + if is_construct: + self.proxy = JClass(class_name)() + else: + self.proxy = JClass(class_name) - def __getattr__(self, attr): - attach_jvm_to_thread() - return getattr(self.proxy, attr) - -class HanLPWrapper(object): - def __init__(self): - self.proxy = JClass('com.hankcs.hanlp.HanLP') # HanLP工具类 - - def __getattr__(self, attr): - attach_jvm_to_thread() - return getattr(self.proxy, attr) - -class PerceptronLexicalAnalyzerWrapper(object): - def __init__(self): - self.proxy = JClass('com.hankcs.hanlp.model.perceptron.PerceptronLexicalAnalyzer')() + # self.proxy = JClass('com.hankcs.hanlp.dictionary.CustomDictionary') # HanLP工具类 def __getattr__(self, attr): attach_jvm_to_thread() return getattr(self.proxy, attr) -CustomDictionary = CustomDictionaryWrapper() -HanLP = HanLPWrapper() -PerceptronLexicalAnalyzer = PerceptronLexicalAnalyzerWrapper() +CustomDictionary = AttachJVMWrapper('com.hankcs.hanlp.dictionary.CustomDictionary') +HanLP = AttachJVMWrapper('com.hankcs.hanlp.HanLP') +PerceptronLexicalAnalyzer = AttachJVMWrapper('com.hankcs.hanlp.model.perceptron.PerceptronLexicalAnalyzer', True)