信息检索与数据挖掘 | 【实验】排名检索模型

news2024/9/21 14:38:36

文章目录

  • 📚实验内容
  • 📚相关概念
  • 📚实验步骤
    • 🐇分词预处理
    • 🐇构建倒排索引表
    • 🐇计算query和各个文档的相似度
    • 🐇queries预处理及检索函数
      • 🔥对输入的文本进行词法分析和标准化处理
      • 🔥检索函数
    • 🐇调试结果

📚实验内容

  1. 在Experiment1的基础上实现最基本的Ranked retrieval model
    • Input:a query (like Ron Weasley birthday)
    • Output: Return the top K (e.g., K = 100) relevant tweets.
  2. Use SMART notation: lnc.ltn
    • Document: logarithmic tf (l as first character), no idf and cosine normalization
    • Query: logarithmic tf (l in leftmost column), idf (t in second column), no normalization
  3. 改进Inverted index
    • 在Dictionary中存储每个term的DF
    • 在posting list中存储term在每个doc中的TF with pairs (docID, tf)

📚相关概念

  • 信息检索与数据挖掘 | (五)文档评分、词项权重计算及向量空间模型
  • 词项频率(term frequencey):t在文档中的出现次数。
  • 文档集频率(collection frequency):词项在文档集中出现的次数。
  • 文档频率(document frequency):出现t的所有文档的数目。
  • 逆文档频率
    在这里插入图片描述
  • t f − i d f t , d tf-idf_{t,d} tfidft,d计算
    在这里插入图片描述
    在这里插入图片描述
  • 相似度计算
    在这里插入图片描述
  • 查询权重机制
    在这里插入图片描述

📚实验步骤

🐇分词预处理

  1. 将输入的推特文档转换为小写,这里统一处理,使得后续查询不区分大小写。

  2. 根据特定标记在推特文档中查找并确定关键部分信息的位置索引,并提取出推特文档中的tweetid和tweet内容。

  3. 对提取出的文本内容进行分词处理,并将单词转换为其单数形式。

  4. 对分词后的词列表进行词形还原,主要针对动词的还原操作。同时,筛去[“text”, “tweetid”]

  5. 将筛选出的有效词添加到最终结果列表中,并返回。

    #分词预处理
    def tokenize_tweet(document):
        # 统一处理使查询不区分大小写
        document = document.lower()
        # 根据特定标记在推特文档中查找并确定关键部分信息的位置索引
        # 这里的减1减3是对引号逗号切入与否的调整
        a = document.index("tweetid") - 1
        b = document.index("errorcode") - 1
        c = document.index("text") - 1
        d = document.index("timestr") - 3
        # 将推特文档中的tweetid和text内容主要信息提取出来
        document = document[a:b] + document[c:d]
        # 分词处理,并将单词转换为其单数形式
        terms = TextBlob(document).words.singularize()
        # 将分词后的词列表进行词形还原,并筛选出不属于无用词的有效词
        result = []
        for word in terms:
            # 将当前词转换为Word对象
            expected_str = Word(word)
            # 动词的还原操作
            expected_str = expected_str.lemmatize("v")
            if expected_str not in uselessTerm:
                # 筛去["text", "tweetid"],添加到result中
                result.append(expected_str)
        return result
    

🐇构建倒排索引表

  • 存储term在每个doc中的TF with pairs (docID, tf)。
    在这里插入图片描述
  1. 首先明确,在该过程计算文档词项的对应权重,采用lnc规则,即 logarithmic tf (l as first character), no idf and cosine normalization。
  2. 具体流程如下:
    • 读取内容。文件中每行都代表一条推特。将每一行推特文本分解为单词(词条化),并存储在一个列表line中
    • 利用一个全局变量N记录读取的推特文档数量。
    • 从line中提取tweetid,并从line中删除。
    • 创建一个空字典tf用于统计每个词在当前文档中的出现次数。遍历line中的每个词,通过判断词是否已经在tf字典的键中存在来更新词的出现次数。
    • 对tf字典中的每个词项频率进行logarithmic tf的计算,即将出现次数加1并取对数。(对应logarithmic tf (l as first character))
    • 归一化(对应cosine normalization,遍历tf字典的键(即词项),得到归一化因子。最后,代码再次遍历tf字典的键,并将每个词项的频率乘以归一化因子。得到最后的对应tf权重。
    • 将line转换为集合unique_terms并遍历其中的每个词。
      • 如果该词已经在postings字典的键中存在,则更新该词对应的字典项,将tweetid和权重加入其中。
      • 如果该词不存在于postings字典的键中,则创建该键,并将tweetid和权重加入其中。

  • 统计词频频率
    # 统计词项频率,记录每个词在当前文档中的出现次数
    tf = {}
     for word in line:
         if word in tf.keys():
             tf[word] += 1
         else:
             tf[word] = 1
    
  • 1 + l o g ( t f t , d ) 1+log(tf_{t,d}) 1+log(tft,d)
     # logarithmic tf
     for word in tf.keys():
         tf[word] = 1 + math.log(tf[word])
    
  • 1 w 1 2 + w 2 2 + . . . + w m 2 \frac{1}{\sqrt{{w_1}^2+{w_2}^2+...+{w_m}^2}} w12+w22+...+wm2 1
     # 归一化,cosine normalization
     cosine = 0
     for word in tf.keys():
         cosine = cosine + tf[word] * tf[word]
     cosine = 1.0 / math.sqrt(cosine)
     for word in tf.keys():
         tf[word] = tf[word] * cosine
    

🐇计算query和各个文档的相似度

  1. 首先明确,该过程分为两个步骤,首先计算query词项的对应权重,然后求相似度(也即对应词项两个权重相乘并求和)并降序排序。Query权重采用ltn规则,即 logarithmic tf (l in leftmost column), idf (t in second column), no normalization
  2. 具体流程如下:
    • 遍历查询词列表query​,对每个词进行词项频率统计,将结果存储在tf中。
    • 遍历tf字典的键(即查询词),根据每个词在postings中的文档频率(文档出现的次数)计算文档频率df​。若一个词不在postings​中,则将文档频率设置为全局变量 N(表示总的文档数量)。
    • 计算权重tf[word] = (math.log(tf[word]) + 1) * math.log(N / df),对应ltn(logarithmic tf, idf, no normalization)
    • 对于每个查询词,检查它是否postings字典中存在。若存在,则遍历该查询词的倒排索引(文档编号及对应的词项权重),根据每个文档的词项权重和查询词的tf-idf值计算相似度得分
    • 存储得分并进行降序排序,得到一个按照相似度排名的列表,并将其返回作为结果。
    def similarity(query):
        global score_tid
        tf = {}
        # 统计词项频率
        for word in query:
            if word in tf:
                tf[word] += 1
            else:
                tf[word] = 1
        # 统计文档频率
        for word in tf.keys():
            if word in postings:
                df = len(postings[word])
            else:
                df = N
            # 对应ltn,logarithmic tf (l in leftmost column), idf (t in second column), no normalization
            tf[word] = (math.log(tf[word]) + 1) * math.log(N / df)
        # 计算相似度
        for word in query:
            if word in postings:
                for tid in postings[word]:
                    if tid in score_tid.keys():
                        score_tid[tid] += postings[word][tid] * tf[word]
                    else:
                        score_tid[tid] = postings[word][tid] * tf[word]
        # 按照得分(相似度)进行降序排序
        similarity = sorted(score_tid.items(), key=lambda x: x[1], reverse=True)
        return similarity
    

🐇queries预处理及检索函数

🔥对输入的文本进行词法分析和标准化处理

def token(doc):
    # 将输入文本转换为小写字母,以便统一处理。
    doc = doc.lower()
    # 将文本拆分为单个词项,并尝试将词项转换为单数形式
    terms = TextBlob(doc).words.singularize()
    # 将分词后的词列表进行词形还原,返回结果列表result
    result = []
    for word in terms:
        expected_str = Word(word)
        expected_str = expected_str.lemmatize("v")
        result.append(expected_str)
    return result

🔥检索函数

def Union(sets):
    return reduce(set.union, [s for s in sets])

def do_search():
    query = token(input("please input search query >> "))
    result = []
    if query == []:
        sys.exit()
    # set()去除查询词列表中的重复项
    unique_query = set(query)
    # 生成一个包含每个查询词对应的tweet的id集合的列表,并且利用Union()函数将这些集合取并集
    relevant_tweetids = Union([set(postings[term].keys()) for term in unique_query])
    print("一共有" + str(len(relevant_tweetids)) + "条相关tweet!")
    if not relevant_tweetids:
        print("No tweets matched any query terms for")
        print(query)
    else:
        print("the top 100 tweets are:")
        scores = similarity(query)
        i = 1
        for (id, score) in scores:
            if i <= 100:  # 返回前n条查询到的信息
                result.append(id)
                print(str(score) + ": " + id)
                i = i + 1
            else:
                break
        print("finished")

🐇调试结果

在这里插入图片描述


最终代码

import sys
from collections import defaultdict
from textblob import TextBlob
from textblob import Word
import math
from functools import reduce

uselessTerm = ["text", "tweetid"]
# 构建倒排索引表,存储term在每个doc中的TF with pairs (docID, tf)
postings = defaultdict(dict)
# 文档数目N
N = 0
# 最终权值
score_tid = defaultdict(dict)

#分词预处理
def tokenize_tweet(document):
    # 统一处理使查询不区分大小写
    document = document.lower()
    # 根据特定标记在推特文档中查找并确定关键部分信息的位置索引
    # 这里的减1减3是对引号逗号切入与否的调整
    a = document.index("tweetid") - 1
    b = document.index("errorcode") - 1
    c = document.index("text") - 1
    d = document.index("timestr") - 3
    # 将推特文档中的tweetid和text内容主要信息提取出来
    document = document[a:b] + document[c:d]
    # 分词处理,并将单词转换为其单数形式
    terms = TextBlob(document).words.singularize()
    # 将分词后的词列表进行词形还原,并筛选出不属于无用词的有效词
    result = []
    for word in terms:
        # 将当前词转换为Word对象
        expected_str = Word(word)
        # 动词的还原操作
        expected_str = expected_str.lemmatize("v")
        if expected_str not in uselessTerm:
            # 筛去["text", "tweetid"],添加到result中
            result.append(expected_str)
    return result

# 构建倒排索引表,存储term在每个doc中的TF with pairs (docID, tf)
# lnc:logarithmic tf, no idf and cosine normalization
def get_postings():
    global postings, N
    content = open(r"Tweets.txt")
    # 内容读取,每一条推特作为一个元素存储在lines中
    lines = content.readlines()
    for line in lines:
        N += 1
        # 预处理
        line = tokenize_tweet(line)
        # 提取处理后的词列表中的第一个元素,即推特文档的tweetid
        tweetid = line[0]
        # 提取后删除,不作为有效词
        line.pop(0)

        # 统计词项频率,记录每个词在当前文档中的出现次数
        tf = {}
        for word in line:
            if word in tf.keys():
                tf[word] += 1
            else:
                tf[word] = 1
        # logarithmic tf
        for word in tf.keys():
            tf[word] = 1 + math.log(tf[word])
        # 归一化,cosine normalization
        cosine = 0
        for word in tf.keys():
            cosine = cosine + tf[word] * tf[word]
        cosine = 1.0 / math.sqrt(cosine)
        for word in tf.keys():
            tf[word] = tf[word] * cosine

        # 将处理后的词列表转换为集合,获取其中的唯一词
        unique_terms = set(line)
        for key_word in unique_terms:
            if key_word in postings.keys():
                postings[key_word][tweetid] = tf[key_word]
            else:
                postings[key_word][tweetid] = tf[key_word]

# query标准化处理
def token(doc):
    # 将输入文本转换为小写字母,以便统一处理。
    doc = doc.lower()
    # 将文本拆分为单个词项,并尝试将词项转换为单数形式
    terms = TextBlob(doc).words.singularize()
    # 将分词后的词列表进行词形还原,返回结果列表result
    result = []
    for word in terms:
        expected_str = Word(word)
        expected_str = expected_str.lemmatize("v")
        result.append(expected_str)
    return result

# 计算query和各个文档的相似度
def similarity(query):
    global score_tid
    tf = {}
    # 统计词项频率
    for word in query:
        if word in tf:
            tf[word] += 1
        else:
            tf[word] = 1
    # 统计文档频率
    for word in tf.keys():
        if word in postings:
            df = len(postings[word])
        else:
            df = N
        # 对应ltn,logarithmic tf (l in leftmost column), idf (t in second column), no normalization
        tf[word] = (math.log(tf[word]) + 1) * math.log(N / df)
    # 计算相似度
    for word in query:
        if word in postings:
            for tid in postings[word]:
                if tid in score_tid.keys():
                    score_tid[tid] += postings[word][tid] * tf[word]
                else:
                    score_tid[tid] = postings[word][tid] * tf[word]
    # 按照得分(相似度)进行降序排序
    similarity = sorted(score_tid.items(), key=lambda x: x[1], reverse=True)
    return similarity


def Union(sets):
    return reduce(set.union, [s for s in sets])

def do_search():
    query = token(input("please input search query >> "))
    result = []
    if query == []:
        sys.exit()
    # set()去除查询词列表中的重复项
    unique_query = set(query)
    # 生成一个包含每个查询词对应的tweet的id集合的列表,并且利用Union()函数将这些集合取并集
    relevant_tweetids = Union([set(postings[term].keys()) for term in unique_query])
    print("一共有" + str(len(relevant_tweetids)) + "条相关tweet!")
    if not relevant_tweetids:
        print("No tweets matched any query terms for")
        print(query)
    else:
        print("the top 100 tweets are:")
        scores = similarity(query)
        i = 1
        for (id, score) in scores:
            if i <= 100:  # 返回前n条查询到的信息
                result.append(id)
                print(str(score) + ": " + id)
                i = i + 1
            else:
                break
        print("finished")

def main():
    get_postings()
    while True:
        do_search()

if __name__ == "__main__":
    main()

参考博客:信息检索实验2- Ranked retrieval model

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1132519.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

微信小程序设置 wx.showModal 提示框中 确定和取消按钮的颜色

wx官方提供的 showModal 无疑是个非常优秀的选择提示工具 但是 我们还可以让他的颜色更贴近整体的小程序风格 cancelColor 可以改变取消按钮的颜色 confirmColor 则可以控制确定按钮的颜色 参考代码如下 wx.showModal({cancelColor: #0000FF,confirmColor: #45B250,content:…

C++中显示构造和隐式构造

截图来源于C primerplus 第六版。

apple-app-site-association nginx

项目里面配置 applinks: 域名 eg: baidu.com 2. 创建 apple-app-site-association 文件&#xff0c;无json等后缀名 eg&#xff1a; appID 构成 teamId bundleId {"applinks": {"apps": [],"details": [{"appID": "2TEAM6D5.…

C#的DataGridView数据控件(直接访问SQL vs 通过EF实体模型访问SQL)

使用DataGridView控件可以快速地将数据库中的数据显示给用户&#xff0c;并且可以通过DataGridView控件直接对数据进行操作&#xff0c;大大增强了操作数据库的效率。DataGridView控件提供一种强大而灵活的以表格形式显示数据的方式。 既可以通过编程直接访问SQL &#xff0c;也…

C语言进阶第九课 --------动态内存管理

作者前言 &#x1f382; ✨✨✨✨✨✨&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f382; ​&#x1f382; 作者介绍&#xff1a; &#x1f382;&#x1f382; &#x1f382; &#x1f389;&#x1f389;&#x1f389…

我国有多少个港口?

港口是什么&#xff1f; 港口是海洋运输中不可或缺的重要设施之一&#xff0c;是连接陆路和水路运输的重要节点。港口通常是指位于沿海地区的水陆交通枢纽&#xff0c;是船舶停靠、装卸货物、储存物资和维修船只的场所。港口一般由码头、泊位、仓库、货场、客运站等设施组成&a…

上海市道路数据,有63550条数据(shp格式和xlsx格式)

数据地址&#xff1a; 上海市道路https://www.xcitybox.com/datamarketview/#/Productpage?id391 基本信息. 数据名称: 上海市道路数据 数据格式: Shpxlsx 数据时间: 2020年 数据几何类型: 线 数据坐标系: WGS84坐标系 数据来源&#xff1a;网络公开数据 数据字段&am…

Aspera和Aspera_cli软件的安装和使用

Aspera和Aspera_cli软件的安装和使用 Aspera和Aspera_cli软件NCBI数据库和EBI-ENA数据库的下载用户和地址信息1. ASpera 4.X.X1.1 ASpera 4.X.X安装1.2 ASpera 4.X.X密钥文件1.3 ASpera 4.X.X简单使用 2. ASpera 3.X.X2.1 ASpera 3.X.X安装2.2 ASpera 3.X.X密钥文件2.3 ASpera …

有了这个交付系统,微生态与代谢组关联分析更轻松

关联分析有多火自不必多说。组学关联策略也是当下研究生物学问题的热门方式&#xff0c;不仅可以实现单组学的数据挖掘&#xff0c;还可深入研究不同组学数据间的关联和因果关系。代谢组作为最接近生物学表型的一种组学&#xff0c;将其与16s所获得的物种丰度进行关联&#xff…

全球PML_V2陆地蒸散发与总初级生产力数据集

简介 全球PML_V2陆地蒸散发与总初级生产力数据集&#xff0c;包括总初级生产力&#xff08;gross primary product, GPP&#xff09;&#xff0c;植被蒸腾&#xff08;vegetation transpiration, Ec&#xff09;&#xff0c;土壤蒸发&#xff08;soil evaporation, Es&#xf…

新闻稿稿费一般多少钱一篇?建议收藏

新闻稿作为媒体传播的重要载体&#xff0c;一直以来都受到广泛关注。而关于新闻稿稿费的话题&#xff0c;更是众多品牌和企业关注的焦点。今天伯乐网络传媒就来给大家讲一讲。 一、新闻稿稿费标准&#xff1a;了解行情&#xff0c;心中有数 1. 新闻稿稿费的基本构成 新闻稿稿费…

解决Visual studio 未能正确加载...包问题

问题 解决&#xff1a; 菜单: Visual Studio 2019 -> 输入"devenv /resetsettings " 将之前的设置恢复到原始状态。且可以正常使用。理论应该可以使用到其它版本中……

应用案例|基于高精度三维机器视觉引导机器人自动分拣包裹的应用

Part.1 行业背景 近年来&#xff0c;电商高速发展&#xff0c;百万件日订单处理的超大型分拣中心模式日益普及&#xff0c;传统的人工供包模式效率低&#xff0c;难以满足高超大分拣中心对分拣包裹的需求。随着科技的进步&#xff0c;自动供包系统进入大众视野&#xff0c;成为…

达梦:开启sql日志记录

前言 开启sql日志记录&#xff0c;可协助排查定位数据库问题。生产开启会有一定的性能消耗&#xff0c;建议打开 SQL 日志异步刷盘功能 1.配置sqllog.ini文件 sqllog.ini 用于 SQL 日志的配置&#xff0c;当且仅当 INI 参数 SVR_LOG1 时使用。 运行中的数据库实例&#xff0c;可…

Java-运算符

1. 什么是运算符 计算机的最基本的用途之一就是执行数学运算&#xff0c;比如&#xff1a; int a 10; int b 20; a b; a < b; 上述 和< 等就是运算符&#xff0c;即&#xff1a;对操作数进行操作时的符号&#xff0c;不同运算符操作的含义不同。 作为一门计算机语言&a…

1.验证码绕过

1.环境 1.前端验证码 抓包 发到重放器 可重复使用 爆破 总结&#xff0c;前端的验证直接删除验证码即可开始爆破 服务端 3.token 爆破

Jmeter接口自动化测试 —— Jmeter断言之Json断言!

json断言可以让我们很快的定位到响应数据中的某一字段&#xff0c;当然前提是响应数据是json格式的&#xff0c;所以如果响应数据为json格式的话&#xff0c;使用json断言还是相当方便的。 还是以之前的接口举例 Url: https://data.cma.cn/weatherGis/web/weather/weatherFcst…

Java工具类——FastJson的40个常用方法

那些想看却没看的书&#xff0c;在心里摆满一个图书馆… 工具类介绍 阿里巴巴的 FastJSON&#xff0c;也被称为 Alibaba FastJSON 或阿里巴巴 JSON&#xff0c;是一个高性能的 Java JSON 处理库&#xff0c;用于在 Java 应用程序中解析和生成 JSON 数据。FastJSON 以其卓越的性…

中国企业数据安全:DataSecurity Plus的利器

在当今数字时代&#xff0c;数据是企业最宝贵的资产之一。保护企业的数据不仅关乎商业机密&#xff0c;还关系到客户隐私和法律合规性。因此&#xff0c;数据安全已经成为每家企业不可忽视的重要议题。为了满足这一需求&#xff0c;ManageEngine公司推出了一款强大的数据安全解…