如何去除文本中的停用词及特殊符号

业务背景

` 在RAG方案中,知识库中的文本会很大的影响到召回的准确率,因此我们在切分文本时去除停用词及特殊符号来提升召回的准确率。

技术细节

  • 准备停用词文件及特殊符号文件
    特殊符号文件
    在这里插入图片描述
    停用词文件
    在这里插入图片描述
    读取停用词及特殊符号
punctuations = load_txt_data("docs/punctuations.txt")
stopwords = load_txt_data("docs/query_stopwords.txt")
exclusions = stopwords + punctuations

def load_txt_data(file_path) -> list:
    current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    with open(os.path.join(current_dir, file_path), encoding="utf-8") as file:
        text_data = [line.strip() for line in file.readlines()]
    return text_data
  • 使用jieba去除停用词
def clean_pages_es(query) -> str:
    punctuations = load_txt_data("docs/punctuations.txt")
    stopwords = load_txt_data("docs/query_stopwords.txt")
    exclusions = stopwords + punctuations
    # 去除空格、制表符、换页符、换行符等
    text = re.sub(r'\s+', '', query)
    # 分词
    tokens = jieba.lcut(text)
    # 去除停用词
    tokens = [word for word in tokens if word not in exclusions]
    # 重新组合文本
    cleaned_text = ''.join(tokens)
    query = cleaned_text
    return query
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值