代码抽取功能优化(一)

前言

  • 本文基于上篇博客代码抽取功能开发记录进行了一些优化

    经过测试发现,之前的代码抽取,对html语言处理存在疏漏,因此,加入了html解析器。
    先将文本经过html解析器解析后,在输入到代码抽取函数中

  • code

from html.parser import HTMLParser


class TextHTMLParser(HTMLParser):
    def __init__(self, ignore_pre):
        HTMLParser.__init__(self)
        self.texts = []
        self.pre_tag_count = 0
        self.ignore_pre = ignore_pre

    def handle_starttag(self, tag, attrs):
        if tag=='pre':
            self.pre_tag_count += 1

    def handle_endtag(self, tag):
        if tag=='pre':
            self.pre_tag_count -= 1

    def handle_data(self, data):
        if self.pre_tag_count>0 and self.ignore_pre:
            return

        self.texts.append(data)

def filter_html(text):
    try:
        parser = TextHTMLParser(False)
        parser.feed(text)
        return ' '.join(parser.texts)
    except:
        print('parse text as html failed, text:', text)
        return text

def filter_html_and_ignore_pre(text):
    try:
        parser = TextHTMLParser(True)
        parser.feed(text)
        return ' '.join(parser.texts)
    except:
        print('parse text as html failed, text:', text)
        return text

对于之前的代码抽取功能的其他优化:

之前的抽取结果中,会保留下来一些换行符,优化后全部替换为空字符串。

  • code
import re
'''计算字符串中 中文字符 数量'''
def getnum_of_cn(inputdata):
    chi=re.findall(r'[\u4E00-\u9FFF]',inputdata)#汉字的Unicode编码范围
    return len(chi)

'''计算字符串中 字母 与 空格 数量之和'''
def getnum_of_en(inputdata):
    char = re.findall(r'[a-zA-Z]',inputdata)#以列表类型返回全部能匹配的子串
    blank = re.findall(r' ',inputdata)
    #other = len(inputdata) - len(char) - len(blank)
    return len(char) + len(blank)

def getnum_of_en_noblank(inputdata):
    char = re.findall(r'[a-zA-Z]',inputdata)#以列表类型返回全部能匹配的子串
    #other = len(inputdata) - len(char) - len(blank)
    return len(char)

special_words = ['I', 'My', 'have', 'was', 'What', "I'm", '我的', '我', '转载于', 'https://', 'when', "i'd", 'help?', 'thanks', "let's", 'How', 'Here']


def extract_code(question):
    codedata = filter_html_and_ignore_pre(question)
    codedata_split = codedata.split('\n')
    re_line = []
    for line in codedata_split:
        cn_num = getnum_of_cn(line)
        en_num = getnum_of_en(line)
        if cn_num > en_num:
            if line.startswith('<p>') and line.endswith('</p>'):
                re_line.append(line + '\n')
            elif '<!--' in line and '-->' in line:
                re_line.append(line + '\n')
            elif r'""""""' in line:
                re_line.append(line + '\n')
            elif '#' in line:
                re_line.append(line + '\n')
            else:
                re_line.append('')

        elif line == '```':
            re_line.append('')

        elif line == '"':
            re_line.append('')
        elif line == '```"':
            re_line.append('')

        else:
            if cn_num > 20:
                if line.startswith('<p>') and line.endswith('</p>') and cn_num <= en_num:
                    re_line.append(line + '\n')
                elif 'tr' in line:
                    re_line.append(line + '\n')
                else:
                    re_line.append('')
            else:
                if '图片说明' in line:
                    re_line.append('')
                elif 'error' in line.lower():
                    re_line.append('')
                elif line.endswith('png)') or line.startswith('(https') or line.startswith('!['):
                    re_line.append('')
                elif cn_num > 0 and en_num > 0 and en_num < 30 and not line.startswith('#') and not line.startswith('/*') \
                        and not line.startswith('<p>') and r'//' not in line and r'#' not in line \
                        and 'print' not in line and 'printf' not in line and '-->' not in line \
                        and '<!--' not in line and 'send' not in line:
                    re_line.append('')
                else:
                    re_line.append(line + '\n')
    result = ''.join(re_line)
    re_cn_num = getnum_of_cn(result)
    re_en_num = getnum_of_en_noblank(result)
    if re_en_num == 0 and re_cn_num == 0:
        result = ''
    return  result

存在的问题

1、c、c++函数在经过html解析器后,如:#include<stdio.h> 。<>中的字符没有被保留
2、若提问者以英文提问,问题描述不能被正确去除

下周计划

解决上述两个存在的问题

总结

想要用AI做好一个产品,光靠模型是远远不够的,要达到一个很高的准确率,往往会夹杂许多规则。

注:如果大家有更好的解决方案,随时欢迎大家私信,感谢

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ToTensor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值