是否有任何好的开源引擎来检测文本的语言,也许是概率度量?我可以在本地运行并且不查询 Google 或 Bing 的一种?我想检测大约 1500 万页 OCR 文本中每一页的语言。
并非所有文档都包含使用拉丁字母的语言。
是否有任何好的开源引擎来检测文本的语言,也许是概率度量?我可以在本地运行并且不查询 Google 或 Bing 的一种?我想检测大约 1500 万页 OCR 文本中每一页的语言。
并非所有文档都包含使用拉丁字母的语言。
根据您所做的工作,您可能需要查看 python 自然语言处理工具包 (NLTK),它对贝叶斯学习算法有一些支持。
一般来说,字母和单词频率可能是最快的评估,但是如果您需要做任何超出语言识别的事情,NLTK(或一般的贝叶斯学习算法)可能会很有用。如果您发现前两种方法的错误率太高,贝叶斯方法也可能很有用。
你当然可以建立你自己的,给定一些关于你的目标语言的字母频率、二合字母频率等的统计数据。
然后将其作为开源发布。瞧,你有一个用于检测文本语言的开源引擎!
为了将来参考,我最终使用的引擎是 libtextcat,它是在 BSD 许可下,但似乎自 2003 年以来就没有维护过。不过,它做得很好并且很容易集成到我的工具链中
试试 CLD2:
安装
export CPPFLAGS="-std=c++98" # https://github.com/CLD2Owners/cld2/issues/47
pip install cld2-cffi --user
跑
import cld2
res = cld2.detect("This is a sample text.")
print(res)
res = cld2.detect("Dies ist ein Beispieltext.")
print(res)
res = cld2.detect("Je ne peut pas parler cette language.")
print(res)
res = cld2.detect(" هذه هي بعض النصوص العربية")
print(res)
res = cld2.detect("这是一些阿拉伯文字") # Chinese?
print(res)
res = cld2.detect("これは、いくつかのアラビア語のテキストです")
print(res)
print("Supports {} languages.".format(len(cld2.LANGUAGES)))
给
Detections(is_reliable=True, bytes_found=23, details=(Detection(language_name=u'ENGLISH', language_code=u'en', percent=95, score=1675.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0)))
Detections(is_reliable=True, bytes_found=27, details=(Detection(language_name=u'GERMAN', language_code=u'de', percent=96, score=1496.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0)))
Detections(is_reliable=True, bytes_found=38, details=(Detection(language_name=u'FRENCH', language_code=u'fr', percent=97, score=1134.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0)))
Detections(is_reliable=True, bytes_found=48, details=(Detection(language_name=u'ARABIC', language_code=u'ar', percent=97, score=1263.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0)))
Detections(is_reliable=False, bytes_found=29, details=(Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0)))
Detections(is_reliable=True, bytes_found=63, details=(Detection(language_name=u'Japanese', language_code=u'ja', percent=98, score=3848.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0), Detection(language_name=u'Unknown', language_code=u'un', percent=0, score=0.0)))
Supports 282 languages.
我认为您不需要任何非常复杂的东西 - 例如,检测文档是否为英文,具有相当高的确定性,只需测试它是否包含 N 个最常见的英文单词 - 例如:
"the a an is to are in on in it"
如果它包含所有这些,我会说它几乎肯定是英语。
您也可以尝试使用 Ruby 的 WhatLanguage gem,它既好用又简单,我已将其用于 Twitter 数据分析。查看:http ://www.youtube.com/watch?v=lNqZ2cqOReo&list=UUJ_3fstMOH-g4yBxtvgAWkw&index=0&feature= plcp 快速演示
在 Github 上查看法郎。它是用 JavaScript 编写的,因此您可以在浏览器中使用,也可以在 Node 中使用。
- franc 支持的语言比任何其他库或 Google 都多;
- 法郎很容易分叉以支持 335 种语言;法郎就像
- 像竞争对手一样快。