赞
踩
Python检测多国语言
一、基础介绍
在很多场合中,我们获得的语言会很多种?例如,英文,法语,中文,繁体中文,如何对这些语言进行具有针对性的检测和处理尤为重要!
多国语言交流(图片为网图)
在这里,我们采用谷歌的langdetect库进行语言检测
(1)首先,我们先下载langdetect
pip install langdetect
(2)测试代码
# -*- coding: utf-8 -*-
# 需要识别的句子(这里是中文)
str = '中文'
# 需要识别的句子(这里是英文)
# str = 'Otec matka syn.'
# 引用库
from langdetect import detect
from langdetect import detect_langs
# 当文本过短或模糊时,判断出来的结果会不确定;
# 如果要让结果唯一,添加以下两行:
from langdetect import DetectorFactory
DetectorFactory.seed = 0
# 判断语言种类
print(detect(str))
# 概率
print(detect_langs(str))
(3)输出结果
zh-cn
[zh-cn:0.9999945807402004]
二、语言范围介绍
(1)支持的语言类型:
支持检测55种语言: af, ar, bg, bn, ca, cs, cy, da, de, el, en, es, et, fa, fi, fr, gu, he, hi, hr, hu, id, it, ja, kn, ko, lt, lv, mk, ml, mr, ne, nl, no, pa, pl, pt, ro, ru, sk, sl, so, sq, sv, sw, ta, te, th, tl, tr, uk, ur, vi, zh-cn, zh-tw
(2)语言类型解释:
百度百科:iso 639-1
(3)具体
快速查看:
参考文献原文路径
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。