转述挖掘 (Paraphrase Mining)

转述挖掘 (Paraphrase mining) 是指在一个大型句子语料库中寻找转述(即具有相同/相似含义的文本)的任务。在语义文本相似度 (Semantic Textual Similarity)中,我们看到了一个简化版的在句子列表中寻找转述的方法。那里介绍的方法使用暴力破解的方式来对所有句子对进行评分和排序。

然而,由于这种方法的时间复杂度是二次方的,它无法扩展到大型(10,000句及以上)的句子集合。对于更大的集合,可以使用 paraphrase_mining() 函数。

from sentence_transformers import SentenceTransformer
from sentence_transformers.util import paraphrase_mining

model = SentenceTransformer("all-MiniLM-L6-v2")

# Single list of sentences - Possible tens of thousands of sentences
sentences = [
    "The cat sits outside",
    "A man is playing guitar",
    "I love pasta",
    "The new movie is awesome",
    "The cat plays in the garden",
    "A woman watches TV",
    "The new movie is so great",
    "Do you like pizza?",
]

paraphrases = paraphrase_mining(model, sentences)

for paraphrase in paraphrases[0:10]:
    score, i, j = paraphrase
    print("{} \t\t {} \t\t Score: {:.4f}".format(sentences[i], sentences[j], score))

paraphrase_mining() 函数接受以下参数:

sentence_transformers.util.paraphrase_mining(model: SentenceTransformer, sentences: list[str], show_progress_bar: bool = False, batch_size: int = 32, query_chunk_size: int = 5000, corpus_chunk_size: int = 100000, max_pairs: int = 500000, top_k: int = 100, score_function: Callable[[Tensor, Tensor], Tensor] = <function cos_sim>, truncate_dim: int | None = None, prompt_name: str | None = None, prompt: str | None = None) list[list[float | int]][源代码]

给定一个句子/文本列表,此函数执行转述挖掘。它将所有句子与所有其他句子进行比较,并返回一个包含余弦相似度得分最高句子对的列表。

参数:
  • model (SentenceTransformer) – 用于嵌入计算的 SentenceTransformer 模型

  • sentences (List[str]) – 一个字符串列表(文本或句子)

  • show_progress_bar (bool, 可选) – 是否绘制进度条。默认为 False。

  • batch_size (int, 可选) – 模型同时编码的文本数量。默认为 32。

  • query_chunk_size (int, 可选) – 同时为 #query_chunk_size 个查询搜索最相似的句子对。减小该值可降低内存占用(会增加运行时间)。默认为 5000。

  • corpus_chunk_size (int, 可选) – 一个句子同时与 #corpus_chunk_size 个其他句子进行比较。减小该值可降低内存占用(会增加运行时间)。默认为 100000。

  • max_pairs (int, 可选) – 返回的最大文本对数量。默认为 500000。

  • top_k (int, 可选) – 对于每个句子,我们最多检索 top_k 个其他句子。默认为 100。

  • score_function (Callable[[Tensor, Tensor], Tensor], 可选) – 用于计算分数的函数。默认使用余弦相似度。默认为 cos_sim。

  • truncate_dim (int, 可选) – 将句子嵌入截断到的维度。如果为 None,则使用模型的维度。默认为 None。

  • prompt_name (Optional[str], 可选) –

    编码句子时使用的预定义提示的名称。它必须与模型 prompts 字典中的一个键匹配,该字典可以在模型初始化时设置或从模型配置中加载。

    如果提供了 prompt,则此参数将被忽略。默认为 None。

  • prompt (Optional[str], 可选) –

    一个原始的提示字符串,在编码时直接加在输入句子的前面。

    例如,prompt=”query: “ 会将句子“What is the capital of France?”转换为:“query: What is the capital of France?”。使用此参数可以完全覆盖提示逻辑并提供您自己的前缀。此参数的优先级高于 prompt_name。默认为 None。

返回:

返回一个格式为 [分数, id1, id2] 的三元组列表

返回类型:

List[List[Union[float, int]]]

为了优化内存和计算时间,转述挖掘是分块进行的,具体由 query_chunk_sizecorpus_chunk_size 指定。具体来说,每次只会比较 query_chunk_size * corpus_chunk_size 个句子对,而不是 len(sentences) * len(sentences) 个。这在时间和内存上都更高效。此外,paraphrase_mining() 对每个句子的每个块只考虑 top_k 个最高分。您可以试验这个值,以在效率和性能之间进行权衡。

例如,在此脚本中,对于每个句子,您将只得到一个最相关的句子。

paraphrases = paraphrase_mining(model, sentences, corpus_chunk_size=len(sentences), top_k=1)

最后一个关键参数是 max_pairs,它决定了函数返回的最大转述对数量。通常,返回的句子对会更少,因为列表会清除重复项,例如,如果它包含 (A, B) 和 (B, A),则只返回其中一个。

注意

如果 B 是 A 最相似的句子,A 不一定是 B 最相似的句子。因此,返回的列表可能会包含像 (A, B) 和 (B, C) 这样的条目。