File size: 1,105 Bytes
9470d49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
import re
import gradio as gr


def analyze_text(text):
    # 字符总数
    chars = len(text)
    # 去掉空格后的字符数
    chars_no_space = len(text.replace(" ", "").replace("\n", ""))
    # 单词数(英文分词 + 中文逐字)
    words = len(re.findall(r'\b\w+\b', text)) + len(re.findall(r'[\u4e00-\u9fff]', text))
    # 标点数
    punctuations = len(re.findall(r'[.,!?;:,。?!;:]', text))
    # 句子数
    sentences = len(re.findall(r'[.!?。?!]', text))

    return {
        "字符数(含空格)": chars,
        "字符数(不含空格)": chars_no_space,
        "单词/汉字数": words,
        "标点数": punctuations,
        "句子数": sentences
    }


demo = gr.Interface(
    fn=analyze_text,
    inputs=gr.Textbox(lines=10, placeholder="请输入一段文本...", label="输入文本"),
    outputs=gr.JSON(label="分析结果"),
    title="高级文本分析工具",
    description="输入一段中文或英文文本,系统会统计字数、单词数、标点符号数和句子数。"
)

if __name__ == "__main__":
    demo.launch()