我有包含多种语言的文本。我想要一个字数计数,它只记录使用python3的希腊unicode字符集出现的单词。比如:
wordCount = 0
theText = open(file.txt, 'r')
for word in theText.split():
if GreekUnicodeCheck(word):
wordCount += 1
print(wordCount)
我考虑过检查每个单词中是否都有希腊字母,比如:
wordCount = 0
theText = open(file.txt, 'r')
greekChars = ['α', 'β', 'γ', 'δ', 'ε', ...]
for word in theText.split():
if any(letter in word for letter in greekChars):
wordCount += 1
print(wordCount)
我认为这应该行得通,但是当你考虑到所有的大写、变音符号等组合时,字符集会变得相当大(我在用古希腊语)。有没有更优雅的解决方案?你知道吗
不过,也不算多。看看this website。所有希腊字母都列在那里,我使用python脚本将其转换为以下列表:
仅使用Python标准库获取所有可能的希腊字符列表的一种简单方法是使用unicodedata库按字符名搜索。你知道吗
相关问题 更多 >
编程相关推荐