信息检索:找到能解决问题的文档
Information retrieval fundamentals
按明确相关性,对比字面匹配与显式同义归一。
information-retrieval正确文档用了另一个词
读者想找更换自行车刹车线的记录,查询写成“bicycle brake cable”。虚构集合中的一条相关记录却写“cycle stopping wire replacement”,没有一个查询词。另一条“car brake cable repair”匹配两个词,车辆却不对。匹配词语和满足信息需求,是两种检查。
信息检索(information retrieval)把需求与集合中有用的材料连接起来。实际系统为文档分配身份,分析内容,建立可检索表示,生成候选,再为读者排序。查询只是需求的简短表达,相关性判断应针对需求本身。IIR教材的评估章节明确区分了两者。IIR第8.1节
小集合可以先用完整扫描作为非AI基线。下面有九条原创文本碎片,包括空记录和近重复记录;预先规定0、4、5、7号与维修需求相关。这些是自行设定的标签,不是用户研究结果,也不是从检索分数倒推的判断。
每一阶段负责什么
第一个排序器统计文档包含多少个不同查询词。Q是集合,重复查询词不会增加权重。零交集文档不返回,同分时按文档编号升序。
这个分数既不是概率,也不判断真假。分析器将文本转小写,只保留连续ASCII字母串;不做词干还原,也不理解中文。文档和查询必须使用同一规则。改变规则就改变表示,可能需要重建索引。
令R为预先规定的相关文档集合,L_k为最多返回k条的前缀。本例即使返回不足k条,precision@k的分母仍为k,空槽没有命中。R为空时召回率未定义。
倒数排名(reciprocal rank)奖励尽早找到第一条相关文档。r*是它在整个返回列表中的排名,从一开始,不一定限于上述截断位置:
两阶段系统可能只对候选池C重排。不在C里的文档,不论怎么排序都找不回来,因此有一个阶段级上限:
返回整个候选池时取等号;如果只展示更短前缀,召回还可能更低。怀疑重排器之前,先测候选覆盖。
帮助一种需求,却伤害另一种的同义词表
用Python 3.12运行独立程序,只需标准库。文档在DOCUMENTS中的位置就是编号。除了字面排序,还比较文件顺序和明确设定的归一映射:cycle→bicycle、stopping→brake、wire→cable、replacement→repair。这是词汇规则,不是学出来的语义模型。
import json,math,re
from collections import Counter,defaultdict
DOCUMENTS=(
'repair bicycle brake cable',
'bicycle brake adjustment guide',
'bicycle paint blue frame',
'car brake cable repair',
'cycle stopping wire replacement',
'repair bicycle brake cable cable cable',
'',
'brake cable bicycle repair',
'cell cycle stopping research',
)
def tokens(text):
if not isinstance(text,str) or len(text)>2000:raise ValueError('text of at most2000 characters required')
return re.findall(r'[a-z]+',text.lower())
def corpus(documents):
if not isinstance(documents,(list,tuple)) or not 1<=len(documents)<=100:raise ValueError('one to100 documents required')
return [tokens(d) for d in documents]
ALIASES={'cycle':'bicycle','stopping':'brake','wire':'cable','replacement':'repair'}
def rank(query,documents=DOCUMENTS,expand=False):
docs=corpus(documents);q=tokens(query)
def normalize(ts):return {ALIASES.get(t,t) if expand else t for t in ts}
q=normalize(q);scored=[(i,len(q&normalize(ts))) for i,ts in enumerate(docs)]
return [dict(doc=i,score=s) for i,s in sorted(scored,key=lambda x:(-x[1],x[0])) if s>0]
def evaluate(ranking,relevant,universe,k):
if type(k) is not int or not 1<=k<=100:raise ValueError('cutoff1..100 required')
if len(ranking)!=len(set(ranking)) or not set(ranking)<=set(universe) or not set(relevant)<=set(universe):raise ValueError('unique ranked IDs and relevance within the declared universe required')
top=ranking[:k];hits=sum(i in relevant for i in top)
return dict(precision_at_k=hits/k,recall_at_k=hits/len(relevant) if relevant else None,returned=len(top),relevant_total=len(relevant),hits=hits,reciprocal_rank=next((1/(i+1) for i,d in enumerate(ranking) if d in relevant),0.))
def demo():
universe=list(range(len(DOCUMENTS)));relevant={0,4,5,7};query='bicycle brake cable';results={}
ranks={'file_order':universe,'lexical':[v['doc'] for v in rank(query)],'normalized':[v['doc'] for v in rank(query,expand=True)]}
for name,order in ranks.items():results[name]=dict(ranking=order,cutoffs={str(k):evaluate(order,relevant,universe,k) for k in [1,2,3,4,6]})
ambiguous={}
for name,expand in [('literal',False),('normalized',True)]:
order=[v['doc'] for v in rank('cycle',expand=expand)];ambiguous[name]=dict(ranking=order,metrics=evaluate(order,{8},universe,2))
candidates=set(ranks['lexical'][:3]);ceiling=len(candidates&relevant)/len(relevant)
return dict(documents=list(DOCUMENTS),need='Find bicycle brake cable repair/replacement records.',query=query,relevant=sorted(relevant),results=results,ambiguous_cycle_need='Find the cell-cycle record, not bicycle records.',ambiguous_cycle=ambiguous,lexical_top3_recall_ceiling=ceiling,unknown_query=rank('quasar'),empty_query=rank(''),scope='Authored fragments and relevance labels; no human study or learned semantic retriever.')
if __name__=='__main__':print(json.dumps(demo(),indent=2,allow_nan=False))
截断为四条时,文件顺序的精确率和召回率都是0.25。字面匹配前四条为0、5、7、1,两项指标均为0.75。归一后前四条变为0、4、5、7,在这个构造需求上达到1。三种方法的RR都为1,因为第一条均相关;只看RR就看不出完整性差别。
现在需求改为细胞周期记录,即8号,查询为“cycle”。字面匹配返回4、8,前两个槽位里仍有相关记录。同一归一规则却把自行车记录引入并排在它前面,前两条召回从1降到0。在一个领域有用的同义规则,换领域可能混淆词义。
字面前三候选为0、5、7,召回上限为0.75。排列这些编号找不到4号。例子刻意让近重复记录占据多个位置;指标统计相关文档编号,不统计不同事实或多样性。实际评估可能两者都需要。
T个已分析词元、N篇文档,扫描需O(T),随后排序需O(N log N);简单实现保留分析后的文档。本例足够小,可以逐个检查标签。规模更大时,倒排索引减少查询时扫描,BM25则提供更细的词项评分。
评价需求,不只评价方便的替代量
NIST的TREC概述描述了文档、问题、提交排名及汇集后的相关性判断。这说明检索器与用于评判它的证据应当分开。历史概述并不是所有现行赛道的规范。TREC
应用中应分开开发查询与最终评测,纳入没有相关文档、语言歧义、拼写错误和词汇变化的查询。未判断文档要单独记录,缺标签不证明不相关。除了实际展示位置上的质量,也比较延迟、索引大小、时效性和更新成本;受限材料在返回前需应用权限过滤。
精确商品编号可能只需维护好的查询表,自由文本则可先用词项排序。扩大匹配可能改善候选召回,也可能增加误报。按观察到的失败选择干预:同义词漏检改词汇处理,短语需求改位置匹配,顺序不好改评分,文档不存在则更新数据。
哪一部分出了问题?
- 文档包含全部查询词,就一定满足读者需求吗?
答案:不一定。汽车维修记录与自行车维修查询高度重叠,却涉及错误车辆。相关性针对明确的信息需求。
- 截断为四条,只返回一篇相关文档;全集有两篇相关文档。本例约定下两项指标是多少?
答案:P@4为1/4,前四条召回为1/2。若把精确率分母设为唯一返回的文档,就采用了另一个指标,并掩盖空槽。
- 对R=0、4、5、7,只重排0、5、7能全部召回吗?
答案:不能。任何排列都只有四个相关编号中的三个,上限是3/4;候选生成必须加入4号。
- cycle→bicycle改善维修搜索后,应对所有查询启用吗?
答案:不应。它会伤害明确的细胞周期需求。要跨领域评估词汇规则,或采用依赖上下文的扩展,并保留未用于筛选的对照数据。
可复现比较需要标签
教材解释检索表示与基于相关性的评估,TREC展示共享评估基础设施。九条文本、标签和数值结果均为原创。练习准确说明这些排序器在该固定数据上做了什么,不是公共语料上的实测提升,也不保证用户满意。
先判断,再看解释
候选集合漏掉四篇相关文档中的一篇,只重排这一集合能恢复全部召回吗?
学习目标
- 分开信息需求、查询、候选集合、排名和相关性判断。
- 运行原创小语料并解释一个失败案例。
先修与延伸
查看知识关联图 →参考文献
- An Introduction to Information Retrieval — online draft, April 1, 2009
Christopher D. Manning; Prabhakar Raghavan; Hinrich Schütze · 2009 - Text REtrieval Conference (TREC) Overview
National Institute of Standards and Technology
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-20 · Single AI author. Viewed physical PDF43/48/78/133/134/158/159/193; read PDF38/44/49/155/189/192 text. Inverted/positional indexes, Boolean merge, variable bytes, lexical vectors and evaluation; not a whole-book review. Read opening through the described document/question, ranked-run pooling and judgment workflow; historical page, not a claim about every current track. 4 paired equations and four explained exercises. Need-relative relevance, cutoff denominators, synonym collision, empty-label handling and candidate recall ceiling independently checked.
- 核需求相关性、截断分母、同义碰撞、空标签及候选召回上限。
- 每题四张计算SVG,10数值/几何与30响应式组合通过;实看10张最终图,完整页面验收另记。
核对的来源位置:
- An Introduction to Information Retrieval — online draft, April 1, 2009 · Viewed physical PDF43/48/78/133/134/158/159/193; read PDF38/44/49/155/189/192 text. Inverted/positional indexes, Boolean merge, variable bytes, lexical vectors and evaluation; not a whole-book review.
- Text REtrieval Conference (TREC) Overview · Read opening through the described document/question, ranked-run pooling and judgment workflow; historical page, not a claim about every current track.
相关主题