File size: 1,221 Bytes
e23742d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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}")