python判断中文文章一行是否为标题

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('这不是一个中文标题')

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值