Python语言检测模块langid和langdetect使用实例

2019-02-1920:34:01后端程序开发Comments2,637 views字数 860阅读模式

使用数据编码风格检测的模块chardet比较多一点,今天提到的两个模块是检测数据的语言类型,比如是:中文还是英文,模块的使用方法也比较简单,我这里只是简单地使用了一下,因为项目中有这个需求,所以拿来用了一下,并没有深入地去研究这两个模块,模块的地址链接我都给出来了,需要的话可以去研究下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9635.html

def langidFunc():
  '''
  https://github.com/yishuihanhan/langid.py
  '''
  print langid.classify("We Are Family")
  print langid.classify("Questa e una prova")
  print langid.classify("我们都有一个家")
  identifier=LanguageIdentifier.from_modelstring(model,norm_probs=True)
  print identifier.classify("We Are Family")
 
def langdetectFunc():
  '''
  https://github.com/yishuihanhan/langdetect
  '''
  s1=u"本篇文章主要介绍两款语言探测工具,用于区分文本到底是什么语言,"
  s2=u'We are pleased to introduce today a new technology'
  print detect(s1)
  print detect(s2)
  print detect_langs(s2)  # detect_langs()输出探测出的所有语言类型及其所占的比例
  print detect_langs("Otec matka syn.")

结果如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9635.html

('en', 9.061840057373047)
('it', -35.41771221160889)
('zh', -85.79573845863342)
('en', 0.16946150595865334)
zh-cn
en
[en:0.999998109575]
[pl:0.571426592237, fi:0.428568772028]文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9635.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9635.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/9635.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定