Page 237 - 《软件学报》2026年第5期
P. 237

2116                                                       软件学报  2026  年第  37  卷第  5  期


                 方法存在两个问题: ① 文本数据庞大, 逐个计算文本向量并计算相似度会消耗大量时间, 效率比较低; ② 功能实体
                 的短语长度方差很大, 固定窗口长度会显著影响匹配效果.
                                                                                     E F  识别. 方法具体流程如
                    为了解决这两个问题, LLM-Extractor 使用可变滑动窗口结合向量搜索的方法进行
                 算法  1  所示. 在第  5–7  行  LLM-Extractor 首先加载  bge-large-en  模型并使用  faiss [50] 生成所有  E F  的索引, 使用  faiss
                 索引搜索相近词向量, 从而解决上述问题 ①; 随后在第                8  行  LLM-Extractor 计算所有  E F  实体的平均长度, 作为初
                 始窗口长度; 接着在第      9–15  行  LLM-Extractor 使用初始窗口滑动遍历文段, 计算整个文段和所有           E F  实体的向量欧
                 氏距离之和, 选出距离最近的         k 个实体再次计算平均长度作为窗口大小. 这一步的目的是减小窗口大小和目标实
                 体长度差距过大导致的误差, 从而解决上述问题 ②; 最后在第                  17–21  行, LLM-Extractor 使用新的窗口再次滑动遍
                 历文段, 利用   faiss 索引搜索和每个窗口向量欧氏距离最小的             E F  实体, 若该欧氏距离小于预先设定的阈值, 则把该
                 匹配加入返回值集合中. 通过算法          1, LLM-Extractor 完成了文段中的   E F  实体识别, 并记录功能实体匹配的位置.
                 算法  1.  E F  实体识别算法.

                                    E F , 预训练的文本嵌入模型     bge, 配置文本段集合     P;
                 输入: 软件功能实体集合
                 输出: 文本窗口和匹配到的目标实体           e F  的对应关系列表   Matches.
                 1. function EntityIdentification ( E F , bge, P)
                 2.  Let Matches be match list of (p, window,  e F )
                 3.  Let Embeddings be embedding list of  E F
                 4.  Model  ← FlagModel(bge)
                 5.  Embeddings  ← Model.encode( E F )
                 6.  index  ← faiss.IndexFlatL2(dimension) //选择欧氏距离作为搜索方法, 生成  faiss 索引
                 7.  index.add(Embeddings)
                 8.  InitialWindowSize  ← averageLength( E F )
                 9.  for p in  P do
                 10.   Let relevantScores be relevant scores for p to all entities in   E F
                 11.   for window in sliding_window(p, InitialWindowSize, step_size) do
                 12.    D, I  ← index.search(Model.encode(window), len( E F )) //搜索窗口相对于所有实体的距离
                 13.    relevantScores  ← relevantScores+D[0].sorted
                 14.   end for
                 15.   topk_indexs  ← np.argsort(relevantScores)[:k]
                                       ← calculateAverageLength(topk_indexs) //筛选最相近的  k 个实体计算新的窗口大小
                 16.   topk_average_length
                 17.   for window in sliding_window(p, topk_average_length, step_size) do
                            ← index.search(Model.encode(window), top_n) //使用新的窗口大小进行匹配
                 18.    D, I
                 19.      e F , distance  ← getEntityAndDistance(D, I)
                 20.    if distance>threshold do
                 21.     Matches  ← Matches+(p, window,  )
                                                   e F
                 22.    end if
                 23.   end for
                 24.  end for
                 25.  return Matches
                 26. end function

                    获得文档中的功能实体匹配位置后, LLM-Extractor 从这些存在匹配的语句中筛选包含至少一个软件配置项
   232   233   234   235   236   237   238   239   240   241   242