OpenVoiceV23 / count.py
XuminYu's picture
init
e23742d
raw
history blame
1.22 kB
import re
def count_chars_words(sentence):
# 使用正则表达式分割句子,其中中文按字分割,英文按词分割
segments = re.findall(r'[\u4e00-\u9fa5]+|\w+', sentence)
# 统计字符数和词数
char_count = 0
word_count = 0
for segment in segments:
# print(segment)
if re.match(r'[\u4e00-\u9fa5]+', segment): # 中文部分,每个汉字算一个字符
char_count += len(segment)
else: # 英文部分,每个单词算一个词
word_count += len(segment.split())
return char_count + word_count
sentence = "如果您 want to deploy the 模型并进行推理"
count = count_chars_words(sentence)
print(f"字符数:{count}")
sentence = "今天天气真好,我们一起出去吃饭吧。"
count = count_chars_words(sentence)
print(f"字符数:{count}")
sentence = "我最近在学习machine learning,希望能够在未来的artificial intelligence领域有所建树。"
count = count_chars_words(sentence)
print(f"字符数:{count}")
sentence = "El resplandor del sol acaricia las olas, pintando el cielo con una paleta deslumbrante。"
count = count_chars_words(sentence)
print(f"字符数:{count}")