From a8abebbca411bfadd6c82381ee0b400bd6c22a46 Mon Sep 17 00:00:00 2001 From: Hai Liang Wang Date: Fri, 23 Mar 2018 19:28:50 +0800 Subject: [PATCH] #7 use wrapper class --- pyhanlp/__init__.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) 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)