Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cygwin_root = os.popen('cygpath -w /').read().strip().replace('\\', '/')
STATIC_ROOT = cygwin_root + STATIC_ROOT[len('/usr'):]
HANLP_JAR_PATH = cygwin_root + HANLP_JAR_PATH[len('/usr'):]
PATH_CONFIG = cygwin_root + PATH_CONFIG[len('/usr'):]
elif STATIC_ROOT.startswith('/cygdrive'):
driver = STATIC_ROOT.split('/')
cygwin_driver = '/'.join(driver[:3])
win_driver = driver[2].upper() + ':'
HANLP_JAR_PATH = HANLP_JAR_PATH.replace(cygwin_driver, win_driver)
STATIC_ROOT = STATIC_ROOT.replace(cygwin_driver, win_driver)
PATH_CONFIG = PATH_CONFIG.replace(cygwin_driver, win_driver)
JAVA_JAR_CLASSPATH = "-Djava.class.path=%s%s%s" % (
HANLP_JAR_PATH, pathsep, STATIC_ROOT)
# 加载插件jar
for jar in glob.glob(os.path.join(STATIC_ROOT, '*.jar')):
if HANLP_JAR_PATH.endswith(jar):
continue
JAVA_JAR_CLASSPATH = JAVA_JAR_CLASSPATH + pathsep + os.path.join(STATIC_ROOT, jar)
if HANLP_VERBOSE: print("设置 JAVA_JAR_CLASSPATH [%s]" % JAVA_JAR_CLASSPATH)
# 启动JVM
startJVM(
jvm_path,
JAVA_JAR_CLASSPATH,
"-Xms%s" %
HANLP_JVM_XMS,
"-Xmx%s" %
HANLP_JVM_XMX, convertStrings=True)
# 确保启动正常
try:
JClass('com.hankcs.hanlp.HanLP')
except java.lang.NoClassDefFoundError as e:
from pyhanlp.static import install_hanlp_jar
else:
from pyhanlp.static import HANLP_JAR_PATH
if "HANLP_JVM_XMS" in ENVIRON:
HANLP_JVM_XMS = ENVIRON["HANLP_JVM_XMS"]
else:
HANLP_JVM_XMS = "1g"
if "HANLP_JVM_XMX" in ENVIRON:
HANLP_JVM_XMX = ENVIRON["HANLP_JVM_XMX"]
else:
HANLP_JVM_XMX = "2g"
PATH_CONFIG = os.path.join(STATIC_ROOT, 'hanlp.properties')
if not os.path.exists(HANLP_JAR_PATH):
raise ValueError(
"配置错误: HANLP_JAR_PATH=%s 不存在" %
HANLP_JAR_PATH)
elif not os.path.isfile(HANLP_JAR_PATH) or not HANLP_JAR_PATH.endswith('.jar'):
raise ValueError(
"配置错误: HANLP_JAR_PATH=%s 不是jar文件" %
HANLP_JAR_PATH)
elif not os.path.exists(STATIC_ROOT):
raise ValueError(
"配置错误: STATIC_ROOT=%s 不存在" %
STATIC_ROOT)
elif not os.path.isdir(HANLP_DATA_PATH):
if HANLP_DATA_PATH.startswith(STATIC_ROOT):
raise ValueError(
"配置错误: STATIC_ROOT=%s 目录下没有data文件夹" %
STATIC_ROOT)
else:
raise ValueError(
"配置错误: 数据包 %s 不存在,请修改配置文件中的root" %
HANLP_DATA_PATH)