【问题标题】:NLTK and language detectionNLTK 和语言检测
【发布时间】:2011-03-12 01:39:35
【问题描述】:

如何使用 NLTK 检测文本是用什么语言编写的?

我看到的例子使用nltk.detect,但是当我在我的mac上安装它时,我找不到这个包。

【问题讨论】:

标签: python nlp nltk detection


【解决方案1】:

你有没有遇到过如下代码sn-p?

english_vocab = set(w.lower() for w in nltk.corpus.words.words())
text_vocab = set(w.lower() for w in text if w.lower().isalpha())
unusual = text_vocab.difference(english_vocab) 

来自http://groups.google.com/group/nltk-users/browse_thread/thread/a5f52af2cbc4cfeb?pli=1&safe=active

还是下面的演示文件?

https://web.archive.org/web/20120202055535/http://code.google.com/p/nltk/source/browse/trunk/nltk_contrib/nltk_contrib/misc/langid.py

【讨论】:

  • PS,不过它仍然依赖于 nltk.detect。关于如何在 Mac 上安装它的任何想法?
  • 我不认为检测是 nltk 的本机模块。代码如下:docs.huihoo.com/nltk/0.9.5/api/nltk.detect-pysrc.html 你可以下载它并把它放在你的python库中,它可能在:/Library/Python/2.x/site-packages/nltk...
  • 在此服务器上找不到请求的 URL /p/nltk/source/browse/trunk/nltk_contrib/nltk_contrib/misc/langid.py。这就是我们所知道的。
【解决方案2】:

这个库也不是来自 NLTK,但肯定有帮助。

$ sudo pip install langdetect

支持的 Python 版本 2.6、2.7、3.x。

>>> from langdetect import detect

>>> detect("War doesn't show who's right, just who's left.")
'en'
>>> detect("Ein, zwei, drei, vier")
'de'

https://pypi.python.org/pypi/langdetect?

P.S.:不要指望它总是能正常工作:

>>> detect("today is a good day")
'so'
>>> detect("today is a good day.")
'so'
>>> detect("la vita e bella!")
'it'
>>> detect("khoobi? khoshi?")
'so'
>>> detect("wow")
'pl'
>>> detect("what a day")
'en'
>>> detect("yay!")
'so'

【讨论】:

  • 感谢您指出它并不总是有效。 detect("You made it home!") 给了我“fr”。我想知道有没有更好的。
  • 这是另一个有趣的观察结果:似乎每次都没有给出相同的答案。 >>> detect_langs("Hello, I'm christiane amanpour.") [it:0.8571401485770536, en:0.14285811674731527] >>> detect_langs("Hello, I'm christiane amanpour.") [it:0.8571403121803622, fr:0.14285888197332486] >>> detect_langs("Hello, I'm christiane amanpour.") [it:0.999995562246093]
  • langdetect 对于更长的字符串效果更好,因为它可以采样更多的 n-gram ......对于几个单词的短字符串,它非常不可靠。
  • @MarkCramer 该算法是不确定的。如果您每次都想要相同的答案,请设置种子:import DetectorFactory DetectorFactory.seed = 0
  • 安装快捷,使用方便。也许并不完美,但对于我的使用来说,它工作得很好。谢谢!
【解决方案3】:

虽然这不在 NLTK 中,但我在另一个基于 Python 的库中取得了不错的成绩:

https://github.com/saffsd/langid.py

这很容易导入,并且在其模型中包含大量语言。

【讨论】:

    【解决方案4】:

    超级晚了,但你可以在nltkhere 中使用textcat 分类器。这个paper 讨论了算法。

    它返回 ISO 639-3 中的国家代码,所以我会使用 pycountry 来获取全名。

    例如,加载库

    import nltk
    import pycountry
    from nltk.stem import SnowballStemmer
    

    现在让我们看两个短语,以及guess 他们的语言:

    phrase_one = "good morning"
    phrase_two = "goeie more"
    
    tc = nltk.classify.textcat.TextCat() 
    guess_one = tc.guess_language(phrase_one)
    guess_two = tc.guess_language(phrase_two)
    
    guess_one_name = pycountry.languages.get(alpha_3=guess_one).name
    guess_two_name = pycountry.languages.get(alpha_3=guess_two).name
    print(guess_one_name)
    print(guess_two_name)
    
    English
    Afrikaans
    

    然后您可以将它们传递给其他 nltk 函数,例如:

    stemmer = SnowballStemmer(guess_one_name.lower())
    s1 = "walking"
    print(stemmer.stem(s1))
    walk
    

    免责声明显然这并不总是有效,尤其是对于稀疏数据

    极端例子

    guess_example = tc.guess_language("hello")
    print(pycountry.languages.get(alpha_3=guess_example).name)
    Konkani (individual language)
    

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多