如何将压缩语法文件加载到 NLTK 中?我可以很好地加载未压缩的语法文件:
import nltk
parser = nltk.load_parser('grammar.fcfg')
但是由于我的语法文件接近 100MB,我想加载更小的压缩版本,只有 1MB,但是失败了:
import nltk
parser = nltk.load_parser('grammar.tar.gz')
ValueError: Could not determine format for file:///grammar.tar.gz based on its file extension; use the "format" argument to specify the format explicitly.
不幸的是,查看nltk.data.FORMATS
并没有列出任何压缩格式。
最佳答案
通过检查 the source of load_parser
可以看到,它relies上ntlk.data.find
打开 file:///
URL。
该函数自动检测 GZipped 数据 by checking if the path ends with .gz
和 reads it与 gzip.GZipFile
.
但是,要实现此目的,数据应直接使用 gzip
打包,而不是通过 tar
或其他方式打包。
关于python - 如何从 GZIP 文件中读取 NLTK 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43459949/