Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: Java package '...' is not valid 해결법 #427

Open
younggam opened this issue Jun 28, 2023 · 0 comments
Open

AttributeError: Java package '...' is not valid 해결법 #427

younggam opened this issue Jun 28, 2023 · 0 comments

Comments

@younggam
Copy link

JPype가 JVM을 프로세스당 1번만 실행할 수 있는데,
똑같이 JPype를 사용하는 다른 dependency에서 JVM을 미리 실행해버리면

konlpy관련 classpath지정에 실패해서 문제가 생기는 거 같습니다.

이미 다른 라이브러리에서 JVM을 실행했다고 가정하면
_komoran.py등에서 URLClassLoaderURL을 import 하시고
jpype.JClass("pacakge.class",classLoader) 이런식으로 jpype.JPackage()를 대체할 수 있습니다

대신 부작용인지 Java.lang.String 관련 오류가 많이 뜨더라고요
komoran 같은 경우는 startswith->startsWith으로 바꿔져야 한다던지
에러 보시면서 고치면 될꺼 같습니다.

예시) 안될수도 있습니다 아무튼 대충 이렇게 하시면 됨

# _komoran.py
import jpype.imports
from java.net import URLClassLoader, URL

    def __init__(self, jvmpath=None, userdic=None, modelpath=None, max_heap_size=1024):
        if not jpype.isJVMStarted():
            jvm.init_jvm(jvmpath, max_heap_size)

        if modelpath:
            self.modelpath = modelpath
        else:
            # FIXME: Cannot execute without sudoing
            # java.lang.NoClassDefFoundErrorPyRaisable: java.lang.NoClassDefFoundError: kr/co/shineware/nlp/komoran/core/analyzer/Komoran
            self.modelpath = os.path.join(utils.installpath, 'java', 'data', 'models')
        self.tagset = utils.read_json('%s/data/tagset/komoran.json' % utils.installpath)

        folder_suffix = [
            # JAR
            '{0}',
            # Java sources
            '{0}{1}bin',
            '{0}{1}bin{1}kr{1}lucypark{1}okt{1}',
            '{0}{1}*',
            # Hannanum
            '{0}{1}jhannanum-0.8.4.jar',
            # Kkma
            '{0}{1}kkma-2.0.jar',
            # Komoran3
            '{0}{1}aho-corasick.jar',
            '{0}{1}shineware-common-1.0.jar',
            '{0}{1}shineware-ds-1.0.jar',
            '{0}{1}komoran-3.0.jar',
            # Twitter (Okt)
            '{0}{1}snakeyaml-1.12.jar',
            '{0}{1}scala-library-2.12.3.jar',
            '{0}{1}open-korean-text-2.1.0.jar',
            '{0}{1}twitter-text-1.14.7.jar',
            '{0}{1}*'
        ]

        javadir = '%s%s%s%sjava' % ('file:', os.sep, utils.installpath, os.sep)

        args = [javadir, os.sep]
        classpath = [URL(f.format(*args)) for f in folder_suffix]

        classLoader = URLClassLoader(classpath)
        komoranJavaPackage = jpype.JClass('kr.co.shineware.nlp.komoran.core.Komoran', classLoader)

        try:
            self.jki = komoranJavaPackage.Komoran(self.modelpath)
        except TypeError:  # Package kr.lucypark.komoran.KomoranInterface is not Callable
            raise IOError("Cannot access komoran-dic. Please leave an issue at https://github.com/konlpy/konlpy/issues")

        if userdic:
            self.jki.setUserDic(userdic)

#396 #334

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant