SCI写作之text2vec包

一起因

起因其实很简单,就是有时候进行SCI写作的时候没有办法找到合适的语句进行模仿,那么寻找一个合适的可以用于语料分析的模块必然是一件十分重要的事情啦(读万卷书形成自己的风格也是一种方式)。因此,在笔者的寻寻觅觅之下,找到了一个还不错的语料分析包,下面也分享给大家。PS:这里只是提供一个思路,但是目前看起来,与专业的写作辅助平台还是有不小的差距。也希望有越来越多的大佬能够分享深度学习或者机器学习的方法来帮助SCI小白进行科研写作。

二详细介绍

首先,介绍一个python包:text2vec

官方文档的介绍: text2vec文本向量表征工具,把文本转化为向量矩阵,是文本进行计算机处理的第一步。text2vec实现了Word2Vec、RankBM25、BERT、Sentence-BERT、CoSENT等多种文本表征、文本相似度计算模型,并在文本语义匹配(相似度计算)任务上比较了各模型的效果。

那么,接下来,笔者就用最简单的例子来向读者示例如何使用这一包进行语料分析。2.1 运行代码

import sys
from text2vec import SentenceModel, cos_sim, semantic_search
embedder = SentenceModel()
# Corpus with example sentences
corpus = [
    '花呗更改绑定银行卡',
    '我什么时候开通了花呗',
    'A man is eating food.',
    'A man is eating a piece of bread.',
    'The girl is carrying a baby.',
    'A man is riding a horse.',
    'A woman is playing violin.',
    'Two men pushed carts through the woods.',
    'A man is riding a white horse on an enclosed ground.',
    'A monkey is playing drums.',
    'A cheetah is running behind its prey.'
]
corpus_embeddings = embedder.encode(corpus)

# Query sentences:
queries = [
    '如何更换花呗绑定银行卡',
    'A man is eating pasta.',
    'Someone in a gorilla costume is playing a set of drums.',
    'A cheetah chases prey on across a field.']

for query in queries:
    query_embedding = embedder.encode(query)
    hits = semantic_search(query_embedding, corpus_embeddings, top_k=5)
    print("\n\n======================\n\n")
    print("Query:", query)
    print("\nTop 5 most similar sentences in corpus:")
    hits = hits[0]  # Get the hits for the first query
    for hit in hits:
        print(corpus[hit['corpus_id']], "(Score: {:.4f})".format(hit['score']))


需要注意的是,embedder = SentenceModel()这句代码需要提前下载语料库。然而,由于国内网络的限制,我们其实是没有办法下载下来的,因此,这里教给大家一个小tip。
Tips: 我们可以在安装text2vec的安装目录下(如笔者安装的目录是F:\Postdoc_analysis\Custom_program),找到sentence_model.py文件,将其中的模型搜索目录修改,即将shibing624/修改为指定的目录F:\\Postdoc_analysis\\pretrain_model。具体的修改方式如下

原代码

class SentenceModel:
    def __init__(
            self,
            model_name_or_path: str = "shibing624/",
            encoder_type: Union[str, EncoderType] = "MEAN",
            max_seq_length: int = 128,
            device: Optional[str] = None,
    ):
修改后的代码


class SentenceModel:
    def __init__(
            self,
            model_name_or_path: str = "F:\\Postdoc_analysis\\pretrain_model",
            encoder_type: Union[str, EncoderType] = "MEAN",
            max_seq_length: int = 128,
            device: Optional[str] = None,
    ):
2.2 下载预训练模型

然后呢,在指定目录下载好作者已经训练好的模型文件。如本模块的地址就在:https://huggingface.co/shibing624/text2vec-base-chinese

需要注意的是,在该目录下的所有文件都需要下载(除了那个.gitattributes和README.md文件外)

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值