nltk sys.platform.startswith('win') 不是检测操作系统的可靠方法,

cbjzeqam  于 2个月前  发布在  其他
关注(0)|答案(6)|浏览(33)

我已经在使用NLTK、IronPython和Windows进行一些工作,并发现我们经常会遇到文件路径的问题。似乎我们主要使用以下方法来判断我们是否在Windows上:
windows = sys.platform.startswith('win')
不幸的是,IronPython的sys.platform是"cli"。我还没有找到这个历史原因,但我已经找到了几种替代的方法来识别平台:

>>> os.name
'nt'
>>> platform.system()
'Windows'

这两种方法似乎都比"sys.platform"更可靠。有人知道为什么这些方法没有被使用吗?如果没有人反对,我可以创建一个pull request来提交这些更改。

jum4pzuy

jum4pzuy1#

@doweaver 感谢您提出这个问题。我认为IronPython有一些不符合规范的注意事项。
但是,最新版本的IronPython sys.platform 与规范的 CPython sys.platform 非常相似。
您正在使用哪个版本的IronPython?
我认为 cli 是一个在后续版本中得到纠正的错误。尝试更新IronPython并查看 cli 是否仍然存在。

k2fxgqgv

k2fxgqgv2#

我正在使用IronPython 2.7.7,根据ironpython.net/downloads,这是最新稳定版本,但我仍然看到'cli':

IronPython 2.7.7 (2.7.7.0) on .NET 4.0.30319.42000 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.platform
'cli'

我还在Arch Linux上安装了IronPython(版本2.7.5),并看到'cli'输出为sys.platform。
我猜想IronPython的作者希望sys.platform在所有操作系统上的IronPython中保持一致。Python文档中的sys.platform并没有真正让我了解到这是否是正确的做法:)

afdcj2ne

afdcj2ne3#

@doweaver 我对IronPython不太熟悉,不知道该如何继续。
一种可能的方法是按照以下步骤进行:

$ grep -n -r "sys.platform.startswith('win')" *
Binary file nltk/__pycache__/data.cpython-35.pyc matches
nltk/data.py:80:if sys.platform.startswith('win'):
nltk/data.py:114:    >>> windows = sys.platform.startswith('win')
nltk/data.py:141:    >>> windows = sys.platform.startswith('win')
nltk/data.py:202:    >>> windows = sys.platform.startswith('win')
nltk/data.py:221:    if sys.platform.startswith('win'):
nltk/data.py:233:    if sys.platform.startswith('win') and os.path.isabs(resource_name):
Binary file nltk/data.pyc matches
nltk/parse/malt.py:222:        classpaths_separator = ';' if sys.platform.startswith('win') else ':'

并分别将 sys.platform.startswith('win') 更改为:

sys.platform.startswith('win') or (sys.platform == "cli" and platform.python_implementation() == 'IronPython')

但这意味着 nltk 正在非正式地支持 IronPython 。所以我不确定这是否会引发另一个问题 =)
也许对 IronPython 了解更好的人应该给出建议。

r55awzrz

r55awzrz4#

我认为我们不希望在那里添加一个"cli"检查的主要原因是,"cli"也是Linux上的值,所以我们只是将NLTK在IronPython上从在Windows机器上失败转移到在Linux机器上失败。
我的临时解决方法是将检查切换为:
os.name == 'nt'

nfs0ujit

nfs0ujit5#

我刚刚在尝试从IronPython调用nltk.pos_tag(words)时遇到了这个问题。我的解决方案是这样的:

import sys
sys.platform = 'win32'

至少对于上面的调用有效。

4xy9mtcn

4xy9mtcn6#

我刚刚遇到了这个问题,试图从IronPython中调用nltk.pos_tag(words)。我的解决方案是这样的:

import sys
sys.platform = 'win32'

至少对于上面的调用有效。
非常感谢。这个对我有用

相关问题