信息检索与数据挖掘——倒排索引2

信息检索实验报告

实验题目

Ranked retrieval model

实验内容
  • 在 Homework 1.1的基础上实现最基本的 Ranked retrieval model;
  • Use SMART notation: lnc.ltc;
  • 在Dictionary和posting list中存储每个term的DF;
实验过程
  • tf and df存储

添加其它需要的数据结构来分别记录DF:

postings = defaultdict(dict)
document_frequency = defaultdict(int)

遍历tweets时记录tf

unique_terms = set(line)
for term in unique_terms:
    postings[term][tweetid] = line.count(term)

记录tf完成后的输出文件如下:
在这里插入图片描述
记录df

for term in postings:
    document_frequency[term] = len(postings[term])

记录df完成后的输出文件如下:

在这里插入图片描述
注:这里的len是取term出现在文档的个数

  • 查询实现

  • 查询relevant_tweetids

利用已有数据结构实现查询的功能,对于输入的一串语句,进行相同的token处理,而后为了加快检索速率,避免去遍历所有tweet计算每一个F(q,d),可以先对于查询检索提取出相关的tweetid,得到relevant_tweetids列表,然后对相关tweetid计算score,降序输出topk个相关tweets。

  unique_query = set(query)
  relevant_tweetids = Union([set(postings[term].keys()) for term in unique_query])
  • 计算score

然后对query中的每一个键值对(term,id),计算score

    for term in unique_query:
        wtq=query.count(term)/len(query)

        if (term in postings) and (id in postings[term].keys()):
            wtd = (1 + math.log(postings[term][id]) * math.log((document_numbers + 1) / document_frequency[term]))
            similarity = wtq*wtd
  • 排序

调用sorted函数对score进行排序

        scores = sorted([(id, Getscore(query, id) 
                          for id in relevant_tweetids],
                         key=lambda x: x[1],
                         reverse=True)
  • 输出

输出前k个值

        for (id, score) in scores3:
            if i<=10:
                result.append(id)
                print(str(score) + ": " + id)
                i = i + 1
            else:
                break
        print
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值