中文检测算法优化

AI

为了实现这个需求,我们可以在contains_chinese()函数中添加一个条件来检查字符串是否包含日文片假名。这里是一个修改后的版本:

import unicodedata

def contains_chinese(s):

has_chinese = False

for ch in s:

if u’\u4e00’ <= ch <= u’\u9fff’ and ‘CJK UNIFIED IDEOGRAPH’ in unicodedata.name(ch):

has_chinese = True

if u’\u30A0’ <= ch <= u’\u30FF’ or u’\uFF65’ <= ch <= u’\uFF9F’: # 检查日文片假名

return False

return has_chinese

#测试算法

text = “鉄道ファン/鉄道路線擬人化一次創作 #HighSpeedMetropoliStars / #riramemo #riramemotrip / 談論風発”

print(contains_chinese(text)) # 输出 False

这个修改后的函数首先检查字符串中是否有中文字符,并用变量has_chinese来记录。接着,它会检查字符串中是否有日文片假名。如果发现日文片假名,函数将立即返回False。最后,如果没有日文片假名且存在中文字符,则返回True。

Reply to this note

Please Login to reply.

Discussion

No replies yet.