IR:如何根据索引值匹配文档并返回文档?

发布于 2025-01-20 15:36:45 字数 659 浏览 2 评论 0 原文

我有一个熊猫数据框架 df ,它的基于其BM25分数排名的语料库中的前10个文档,并由其 doc_id 索引。

DOC_ID 等级 BM25分数
1234 1 3.3472
5678 2 3.3238

我也有一个列表文档包含所有文档与其 doc_id [['first doc_id','第一个doc文本],['second doc_id','第二doc text],...]

我需要在 df 中使用 doc_id ,并在 df 中使用 doc_ /代码>并打印出文档文本。我知道如何通过执行 df 来获取特定等级的 doc_id 。 /code>,但我不确定如何从那里获取相应的文档文本。

I have a pandas data frame df which has the top 10 documents from a corpus ranked based on their BM25 score, and indexed by their Doc_ID.

Doc_ID Rank BM25 Score
1234 1 3.3472
5678 2 3.3238

I also have a list documents containing all of the documents paired up with their Doc_ID, such that the list is in the following form: [['First Doc_ID', 'First doc text], ['Second Doc_ID', 'Second doc text], ...].

I need to take the Doc_ID for the top 3 ranked documents in df, and match each one with the corresponding Doc_ID in documents and print out the document text. I know how to get the Doc_ID for a particular rank from df by doing df.index[df['Rank'] == 1][0], but I'm unsure how to go from there to get the corresponding document text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-01-27 15:36:45

您可以将列表转换为dataframe和代码>

documents = [[1234, 'First doc text'],
             [5678, 'Second doc text'],
             [5679, 'Third doc text'],
             [5680, 'Fourth doc text']]

(df[df['Rank'].le(3)]
 .merge(pd.DataFrame(documents,
                     columns=['Doc_ID', 'Text']),
        on='Doc_ID')
)

输出:

   Doc_ID  Rank  BM25 Score             Text
0    1234     1      3.3472   First doc text
1    5678     2      3.3238  Second doc text
2    5679     3      3.2000   Third doc text

二手输入:

   Doc_ID  Rank  BM25 Score
0    1234     1      3.3472
1    5678     2      3.3238
2    5679     3      3.2000
3    5680     4      3.1000

或者,如果您想要使用python的列表:

top3 = set(df.loc[df['Rank'].le(3), 'Doc_ID'])
out = [text for ID, text in documents if ID in top3]

output: ['first doc text','第二doc text','第三doc text'] >

You can convert your list to DataFrame and merge:

documents = [[1234, 'First doc text'],
             [5678, 'Second doc text'],
             [5679, 'Third doc text'],
             [5680, 'Fourth doc text']]

(df[df['Rank'].le(3)]
 .merge(pd.DataFrame(documents,
                     columns=['Doc_ID', 'Text']),
        on='Doc_ID')
)

output:

   Doc_ID  Rank  BM25 Score             Text
0    1234     1      3.3472   First doc text
1    5678     2      3.3238  Second doc text
2    5679     3      3.2000   Third doc text

used input:

   Doc_ID  Rank  BM25 Score
0    1234     1      3.3472
1    5678     2      3.3238
2    5679     3      3.2000
3    5680     4      3.1000

Alternatively, if you want a list using python:

top3 = set(df.loc[df['Rank'].le(3), 'Doc_ID'])
out = [text for ID, text in documents if ID in top3]

output: ['First doc text', 'Second doc text', 'Third doc text']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文