Calling Java From Python
Java
Python
jpype
pyjnius
lingua
text analysis
language detection
This article discusses the process of calling Java code from Python using jpype or pyjnius libraries, allowing for seamless integration between the two languages. Additionally, it covers the usage of the com.github.pemistahl.lingua.api library to detect the language within a given text.
sample code for jpype:
from jpype import *
import jpype.imports # this is needed! shit.
"/root/Desktop/works/pyjom/tests/karaoke_effects/classpath/lingua.jar")
addClassPath(
startJVM(getDefaultJVMPath())"Calling Java Print from Python using Jpype!")
java.lang.System.out.println(from com.github.pemistahl.lingua.api import *
# detector = LanguageDetectorBuilder.fromAllLanguages().withLowAccuracyMode().build()
= LanguageDetectorBuilder.fromAllLanguages().build() # 3.5GB just for detecting language! it is somehow crazy.
detector = 'hello world'
sample = detector.detectLanguageOf(sample)
result print(result, type(result)) # <java class 'com.github.pemistahl.lingua.api.Language'>
# but we can convert it into string.
= str(result)
strResult print(strResult, type(strResult))
import math
print("CALLING MATH: %d" % math.sqrt(4))
shutdownJVM()
sample for pyjnius:
import jnius_config
# jnius_config.add_options('-Xrs', '-Xmx4096')
'.', "/root/Desktop/works/pyjom/tests/karaoke_effects/classpath/lingua.jar")
jnius_config.set_classpath(import jnius
'java.lang.System').out.println('Hello world')
jnius.autoclass(= jnius.autoclass('com.github.pemistahl.lingua.api.LanguageDetectorBuilder').fromAllLanguages().build()
detector = 'hello world'
sample = detector.detectLanguageOf(sample)
result print(result, type(result))
# breakpoint()
= result.toString()
strResult print(strResult, type(strResult))