一起因
起因其实很简单,就是有时候进行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文件外)
文章介绍了在SCI写作中如何利用Python的text2vec包进行语料分析,该包可将文本转化为向量,实现文本相似度计算。通过示例代码展示了如何使用SentenceModel进行操作,并提供了修改模型搜索目录以适应国内网络环境的方法。同时,强调了这种方法与专业写作辅助平台的差距,并期待更多深度学习或机器学习的应用来提升科研写作效率。
933

被折叠的 条评论
为什么被折叠?



