lazycoder / app.py
lixudong4
init project
9470d49
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()