|
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: |
|
|
|
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}") |