import re
def is_chinese_title(line):
if re.match(r'^\s*[\u4e00-\u9fa5]+\s*$', line):
return True # 一行纯中文
if re.search(r'[^\u4e00-\u9fa5\d]+', line):
return True # 包含非中文字符(如符号)
if re.search(r'[^\u4e00-\u9fa5]*[A-Za-z]+[^\u4e00-\u9fa5]*', line):
return True # 包含英文单词
if re.search(r'^\s*[\u4e00-\u9fa5]+\s*[::]$', line):
return True # 带冒号的一行中文
if re.search(r'^\s*[\u4e00-\u9fa5]+\s*\(\d+\)$', line):
return True # 带括号数字的一行中文
if re.search(r'^\s*[\u4e00-\u9fa5]+\s*[::]\s*[\u4e00-\u9fa5]+$', line):
return True # 带冒号后跟中文的一行中文
return False
import jieba
def is_chinese_title(line):
words = jieba.lcut(line.strip())
if len(words) < 3:
return False # 不足3个词的不算标题
if len(words) > 30:
return False # 超过30个词的不算标题
if words[0] in ['第一', '首个', '第一篇', '第一部分', '导言', '前言', '绪论', '引言', '概述']:
return True # 开头特殊词语的一般都是标题
if words[0][-1] in ['?', '!', '。', ':', ';', '、'] and len(words[0]) > 1:
return True # 开头有标点符号的一般都是标题
return
def is_chinese_title(line):
if len(line.strip()) > 0:
# 判断是否使用特殊的标点符号
if line.endswith(':') or line.endswith('—') or line.endswith('-') or line.endswith('.'):
return True
# 判断是否使用较大的字号或粗体字显示
if line.startswith('#') or line.startswith('##') or line.startswith('###'):
return True
# 判断是否使用特殊的排版格式
if line.strip().center(len(line)) == line.strip():
return True
return False
import re
def is_chinese_title(line):
# 标题一般比较短,一行字数不超过20个
if len(line) > 20:
return False
# 标题一般用较大的字号和粗体字来强调
if not re.search(r'\d{0,2}px', line):
return False
if not re.search(r'font-weight:\s*bold', line):
return False
# 标题一般出现在文章开头或章节开头
if not re.match(r'^\s*第*\d*章*\s*', line):
if not re.match(r'^\s*第*\d*节*\s*', line):
if not re.match(r'^\s*第*\d*部*\s*', line):
if not re.match(r'^\s*第*\d*篇*\s*', line):
if not re.match(r'^\s*第*\d*回*\s*', line):
if not re.match(r'^\s*第*\d*期*\s*', line):
if not re.match(r'^\s*第*\d*卷*\s*', line):
return False
# 符合以上条件的就是标题
return True
import re
# 定义中文标题的正则表达式
chinese_title_pattern = re.compile(r'^[\u4e00-\u9fa5]{2,}[\s,。:;!?]$')
# 测试字符串
test_str = '这是一个中文标题,'
# 判断字符串是否为中文标题
if chinese_title_pattern.match(test_str):
print('这是一个中文标题')
else:
print('这不是一个中文标题')