日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python实现多语言语种识别_用Python进行语言检测

發(fā)布時(shí)間:2024/7/23 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实现多语言语种识别_用Python进行语言检测 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近正好碰到這個(gè)需求,總結(jié)一下用Python進(jìn)行語(yǔ)言檢測(cè)的方法。

1.用unicode編碼檢測(cè)

漢字、韓文、日文等都有對(duì)應(yīng)的unicode字符集范圍,只要用正則表達(dá)式匹配出來(lái)即可。

在判斷的時(shí)候,往往需要去掉一些特殊字符,例如中英文標(biāo)點(diǎn)符號(hào)。可以用下列方法去除:

# 方法一,自定義需要去掉的標(biāo)點(diǎn)符號(hào),注意這個(gè)字符串的首尾出現(xiàn)的[]不是標(biāo)點(diǎn)符號(hào)'[]',

# 而是正則表達(dá)式中的中括號(hào),表示定義匹配的字符范圍

remove_nota = u'[’·°–!"#$%&\'()*+,-./:;<=>?@,。?★、…【】()《》?“”‘’![\\]^_`{|}~]+'

sentence = '測(cè)試。,[].?'

print(re.sub(remove_nota, '', sentence))

?

# 方法二,只能去掉英文標(biāo)點(diǎn)符號(hào)

remove_punctuation_map = dict((ord(char), None) for char in string.punctuation)

print(sentence.translate(remove_punctuation_map))

輸出:

測(cè)試

測(cè)試。,

還可以把數(shù)字也去掉:

# 方法一

sentence = re.sub('[0-9]', '', sentence).strip()

?

# 方法二

remove_digits = str.maketrans('', '', string.digits)

sentence = sentence.translate(remove_digits)

然后就可以進(jìn)行語(yǔ)言檢測(cè)了。

這里的思路是匹配句子的相應(yīng)語(yǔ)言字符,然后替換掉,如果替換后字符串為空,表示這個(gè)句子是純正的該語(yǔ)言(即不摻雜其它語(yǔ)言)。

也可以用正則表達(dá)式查詢出句子中屬于該語(yǔ)言的字符

s = "English Test"

re_words = re.compile(u"[a-zA-Z]")

res = re.findall(re_words, s) # 查詢出所有的匹配字符串

print(res)

?

res2 = re.sub('[a-zA-Z]', '', s).strip()

print(res2) # 空字符串

if len(res2) <= 0:

print("This is English")

輸出:

['E', 'n', 'g', 'l', 'i', 's', 'h', 'T', 'e', 's', 't']

?

This is English

匹配英文用u"[a-zA-Z]"

中文用u"[\u4e00-\u9fa5]+"

韓文用u"[\uac00-\ud7ff]+"

日文用u"[\u30a0-\u30ff\u3040-\u309f]+" (包括平假名和片假名)

如果想只保留需要的內(nèi)容,比如保留中英文及數(shù)字:

# 只保留中文、英文、數(shù)字(會(huì)去掉法語(yǔ)德語(yǔ)韓語(yǔ)日語(yǔ)等)

rule = re.compile(u"[^a-zA-Z0-9\u4e00-\u9fa5]")

sentence = rule.sub('', sentence)

完整代碼:

import re

import string

?

remove_nota = u'[’·°–!"#$%&\'()*+,-./:;<=>?@,。?★、…【】()《》?“”‘’![\\]^_`{|}~]+'

remove_punctuation_map = dict((ord(char), None) for char in string.punctuation)

def filter_str(sentence):

sentence = re.sub(remove_nota, '', sentence)

sentence = sentence.translate(remove_punctuation_map)

return sentence.strip()

# 判斷中日韓英

def judge_language(s):

# s = unicode(s) # python2需要將字符串轉(zhuǎn)換為unicode編碼,python3不需要

s = filter_str(s)

result = []

s = re.sub('[0-9]', '', s).strip()

# unicode english

re_words = re.compile(u"[a-zA-Z]")

res = re.findall(re_words, s) # 查詢出所有的匹配字符串

res2 = re.sub('[a-zA-Z]', '', s).strip()

if len(res) > 0:

result.append('en')

if len(res2) <= 0:

return 'en'

?

# unicode chinese

re_words = re.compile(u"[\u4e00-\u9fa5]+")

res = re.findall(re_words, s) # 查詢出所有的匹配字符串

res2 = re.sub(u"[\u4e00-\u9fa5]+", '', s).strip()

if len(res) > 0:

result.append('zh')

if len(res2) <= 0:

return 'zh'

?

# unicode korean

re_words = re.compile(u"[\uac00-\ud7ff]+")

res = re.findall(re_words, s) # 查詢出所有的匹配字符串

res2 = re.sub(u"[\uac00-\ud7ff]+", '', s).strip()

if len(res) > 0:

result.append('ko')

if len(res2) <= 0:

return 'ko'

?

# unicode japanese katakana and unicode japanese hiragana

re_words = re.compile(u"[\u30a0-\u30ff\u3040-\u309f]+")

res = re.findall(re_words, s) # 查詢出所有的匹配字符串

res2 = re.sub(u"[\u30a0-\u30ff\u3040-\u309f]+", '', s).strip()

if len(res) > 0:

result.append('ja')

if len(res2) <= 0:

return 'ja'

return ','.join(result)

這里的judge_language函數(shù)實(shí)現(xiàn)的功能是:針對(duì)一個(gè)字符串,返回其所屬語(yǔ)種,如果存在多種語(yǔ)言,則返回多種語(yǔ)種(只能檢測(cè)出中日英韓)

測(cè)試一下效果:

s1 = "漢語(yǔ)是世界上最優(yōu)美的語(yǔ)言,正則表達(dá)式是一個(gè)很有用的工具"

s2 = "正規(guī)表現(xiàn)は非常に役に立つツールテキストを操作することです"

s3 = "あアいイうウえエおオ"

s4 = "?? ???? ?? ??? ?? ???? ???? ????"

s5 = "Regular expression is a powerful tool for manipulating text."

s6 = "Regular expression 正則表達(dá)式 あアいイうウえエおオ ?? ????"

print(judge_language(s1))

print(judge_language(s2))

print(judge_language(s3))

print(judge_language(s4))

print(judge_language(s5))

print(judge_language(s6))

輸出:

zh

zh,ja

ja

ko

en

en,zh,ko,ja

因?yàn)閟2中包括了漢字,所以輸出結(jié)果中有zh。

2.用工具包檢測(cè)

(1)langdetect

from langdetect import detect

from langdetect import detect_langs

s1 = "漢語(yǔ)是世界上最優(yōu)美的語(yǔ)言,正則表達(dá)式是一個(gè)很有用的工具"

s2 = "正規(guī)表現(xiàn)は非常に役に立つツールテキストを操作することです"

s3 = "あアいイうウえエおオ"

s4 = "?? ???? ?? ??? ?? ???? ???? ????"

s5 = "Regular expression is a powerful tool for manipulating text."

s6 = "Regular expression 正則表達(dá)式 あアいイうウえエおオ ?? ????"

print(detect(s1))

print(detect(s2))

print(detect(s3))

print(detect(s4))

print(detect(s5))

print(detect(s6)) # detect()輸出探測(cè)出的語(yǔ)言類型

print(detect_langs(s6)) # detect_langs()輸出探測(cè)出的所有語(yǔ)言類型及其所占的比例

輸出:

zh-cn

ja

ja

ko

en

ca # 加泰隆語(yǔ)

[ca:0.7142837837746273, ja:0.2857136751343887]

emmm...最后一句話識(shí)別的不準(zhǔn)

(2)langid

import langid

s1 = "漢語(yǔ)是世界上最優(yōu)美的語(yǔ)言,正則表達(dá)式是一個(gè)很有用的工具"

s2 = "正規(guī)表現(xiàn)は非常に役に立つツールテキストを操作することです"

s3 = "あアいイうウえエおオ"

s4 = "?? ???? ?? ??? ?? ???? ???? ????"

s5 = "Regular expression is a powerful tool for manipulating text."

s6 = "Regular expression 正則表達(dá)式 あアいイうウえエおオ ?? ????"

print(langid.classify(s1))

print(langid.classify(s2))

print(langid.classify(s3))

print(langid.classify(s4))

print(langid.classify(s5))

print(langid.classify(s6))

# langid.classify(s6)輸出探測(cè)出的語(yǔ)言類型及其confidence score,

# 其confidence score計(jì)算方式方法見:https://jblevins.org/log/log-sum-exp

輸出:

('zh', -370.64875650405884)

('ja', -668.9920794963837)

('ja', -213.35927987098694)

('ko', -494.80780935287476)

('en', -56.482327461242676)

('ja', -502.3459689617157)

兩個(gè)包都把最后一句話識(shí)別成了英文,他們給出的結(jié)果都是ISO 639-1標(biāo)準(zhǔn)的語(yǔ)言代碼。

再來(lái)看幾個(gè)其他語(yǔ)言的例子:

s = "ру?сский язы?к" # Russian

print(detect(s))

print(langid.classify(s))

s = " " # Arabic

print(detect(s))

print(langid.classify(s))

s = "bonjour" # French

print(detect(s))

print(langid.classify(s))

輸出:

ru

('ru', -194.25553131103516)

ar

('ar', -72.63771915435791)

hr # 克羅地亞語(yǔ)

('en', -22.992373943328857)

法語(yǔ)沒判斷出來(lái)。langdetect的判斷結(jié)果依舊比較離譜...

沒事可以多玩玩這兩個(gè)包,O(∩_∩)O哈哈~

參考資料:

歡迎關(guān)注我的微信公眾號(hào)~

總結(jié)

以上是生活随笔為你收集整理的python实现多语言语种识别_用Python进行语言检测的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。