简体   繁体   English

Python子流程语言环境设置

[英]Python subprocess locale settings

When executing opennlp POSTagger with subprocess.call in python, the result goes wrong. 当执行opennlp POSTaggersubprocess.call在python,结果出错。 But when I put the same command into my terminal, the result is correct. 但是,当我在终端中输入相同的命令时,结果是正确的。

After some testing , I think this is because opennlp failed to load the model file correctly, so what's the problem? 经过一些测试,我认为这是因为opennlp无法正确加载模型文件,这是什么问题? The model is trained in Chinese and I use python 2.7. 该模型是中文培训的,我使用python 2.7。

OpenNLP runs without any warnings or errors, but it tags the input sentence totally wrong. OpenNLP的运行没有任何警告或错误,但是它将输入句子标记为完全错误。 It gives the correct tags in the terminal. 它在终端中提供正确的标签。 I guess it's an encoding problem but I'm not sure. 我想这是编码问题,但我不确定。

Here is the code. 这是代码。 It's nothing special and contains only ascii chars. 它没什么特别的,仅包含ascii字符。 Print this command and copy it to terminal, the result is correct. 打印此命令并将其复制到终端,结果正确。

Now I know it's the locale/encoding problem (debug the script by strace). 现在,我知道这是语言环境/编码问题(通过strace调试脚本)。 But it's of no use to set the python locale to en_US.utf-8 or zh_CN.utf-8 . 但是将python语言环境设置为en_US.utf-8zh_CN.utf-8是没有用的。 My shell locale setting is zh_CN.utf-8 . 我的外壳语言环境设置是zh_CN.utf-8

opennlp_path = './opennlp/bin/opennlp'
pos_model = 'train.pos.model'
pos_predict_cmd = [opennlp_path, 'POSTagger', pos_model]
subproc = call(pos_predict_cmd)

首先,看看http://docs.python.org/library/subprocess.html#using-the-subprocess-module ,阅读一次或两次,然后尝试使用call(pos_predict_cmd, shell=True)看看是否这样可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM