lixudong4 commited on
Commit
9470d49
·
1 Parent(s): f2d71be

init project

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import gradio as gr
3
+
4
+
5
+ def analyze_text(text):
6
+ # 字符总数
7
+ chars = len(text)
8
+ # 去掉空格后的字符数
9
+ chars_no_space = len(text.replace(" ", "").replace("\n", ""))
10
+ # 单词数(英文分词 + 中文逐字)
11
+ words = len(re.findall(r'\b\w+\b', text)) + len(re.findall(r'[\u4e00-\u9fff]', text))
12
+ # 标点数
13
+ punctuations = len(re.findall(r'[.,!?;:,。?!;:]', text))
14
+ # 句子数
15
+ sentences = len(re.findall(r'[.!?。?!]', text))
16
+
17
+ return {
18
+ "字符数(含空格)": chars,
19
+ "字符数(不含空格)": chars_no_space,
20
+ "单词/汉字数": words,
21
+ "标点数": punctuations,
22
+ "句子数": sentences
23
+ }
24
+
25
+
26
+ demo = gr.Interface(
27
+ fn=analyze_text,
28
+ inputs=gr.Textbox(lines=10, placeholder="请输入一段文本...", label="输入文本"),
29
+ outputs=gr.JSON(label="分析结果"),
30
+ title="高级文本分析工具",
31
+ description="输入一段中文或英文文本,系统会统计字数、单词数、标点符号数和句子数。"
32
+ )
33
+
34
+ if __name__ == "__main__":
35
+ demo.launch()