Python 语言检测 : choose between one language or the other only

标签 python nlp language-detection

我正在使用 langdetect 来确定一组字符串的语言,我知道这些字符串是英语或法语。

有时,langdetect 告诉我,对于一个我知道是法语的字符串,语言是罗马尼亚语。

如何让 langdetect 只选择英语或法语,而不是所有其他语言?

谢谢!

最佳答案

选项 1

一个选择是使用包 langid 代替。然后您可以通过方法调用简单地限制语言:

import langid
langid.set_languages(['fr', 'en'])  # ISO 639-1 codes
lang, score = langid.classify('This is a french or english text')
print(lang) # en

选项 2

如果你真的想使用langdetect包,你可以复制包文件夹(如果你不确定它在哪里,使用python -m site --user-site ) 并从文件夹 langdetect\profiles 中删除不需要的配置文件。

虽然这不是一个非常动态的解决方案。

关于Python 语言检测 : choose between one language or the other only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37235932/

相关文章:

python - 如何限制从 python 脚本调用、在 docker 容器中运行的 ffmpeg 的资源?

python - 最好的风格、类方法或全局函数是什么?

python - 网络抓取具有通过ajax加载的动态内容的网页

nlp - 英语到 babel-lang

java - 用于推文语言检测的快速 Java 库?

python - 使用 strptime 将带偏移量的时间戳转换为 datetime obj

python - 如何让word2vec模型的加载时间和内存使用更加高效?

python - 如何过滤掉 Elasticsearch 中的无效输入

python - 在 python : "No features in text" 中使用 langdetect 时出错

android - 如何检测俄语以在 Android 中本地化应用程序