当前位置:   article > 正文

Python 实现语言种类检测_python判断语言种类

python判断语言种类

缘起

项目需要,找一个可以识别语种的方法。
目前Api的方式有华为自然语言处理平台or其他的把,但是大多都需要收费,偶然间发现有一个python的项目
saffsd/langid.py: Stand-alone language identification system (github.com)
但是项目上一次更新在6 years ago,因此又发现了langdetect (55 种语言)库以及fastlid(176 种语言) ,使用过后,在此介绍一下fastlid库。

fastlid 开源链接:

ffreemt/fast-langid: Detect language of a given text, fast (github.com)

pip 安装

这个地方要注意点点,fastlid需要先安装fasttext库。

fasettext 安装

fasttext 依赖C++库文件,因此需要在Python Extension Packages for Windows - Christoph Gohlke (uci.edu)下载对应版本的whl包,通过pip安装即可。

在这里插入图片描述

安装命令:

pip install fasttext*.whl
  • 1

当然,如果你有c++库这些,直接也可

pip install fasttext 
  • 1

fastlid

接下来就直接pip 安装即可

pip install fastlid
  • 1

代码示例

from fastlid import fastlid, supported_langs

# support 176 languages
print(supported_langs, len(supported_langs))
# ['af', 'als', 'am', 'an', 'ar', 'arz', 'as', 'ast', 'av', 'az'] 176

fastlid("test this")
# ('en', 0.765)

fastlid("test this 测试一下", k=2)
# (['zh', 'en'], [0.663, 0.124])

fastlid.set_languages = ['fr', 'zh']
fastlid("test this 测试吧")
# ('zh', 0.01)

fastlid.set_languages = None
fastlid("test this 测试吧")
('en', 0.686)

fastlid.set_languages = ['fr', 'zh', 'en']
fastlid("test this 测试吧", k=3)
(['en', 'zh', 'fr'], [0.686, 0.01, 0.006])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

繁体识别

参见tsroten/hanzidentifier: Python module that identifies Chinese text as being Simplified or Traditional (github.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/571598
推荐阅读
相关标签
  

闽ICP备14008679号