如何在python中使用gensim BM25排名

时间:2016-12-05 01:54:43

标签: python ranking gensim

我发现gensim具有BM25排名功能。但是,我找不到教程如何使用它。

就我而言,我有一个查询。从搜索引擎中检索到的一些文档。如何使用gensim BM 25排名来比较查询和文档以找到最相似的一个?

我是gensim的新手。感谢。

查询:

"experimental studies of creep buckling ."

文件1:

" the 7 x 7 in . hypersonic wind tunnel at rae farnborough, part 1, design, instrumentation and flow visualization techniques . this is the first of three parts of the calibration report on the r.a.e. some details of the design and lay-out of the plant are given, together with the calculated performance figures, and the major components of the facility are briefly described . the instrumentation provided for the wind-tunnel is described in some detail, including the optical and other methods of flow visualization used in the tunnel . later parts will describe the calibration of the flow in the working-section, including temperature measurements . a discussion of the heater performance will also be included as well as the results of tests to determine starting and running pressure ratios, blockage effects, model starting loads, and humidity of the air flow ."

文件2:

" the 7 in. x 7 in. hypersonic wind tunnel at r.a.e. farnborough part ii. heater performance . tests on the storage heater, which is cylindrical in form and mounted horizontally, show that its performance is adequate for operation at m=6.8 and probably adequate for flows at m=8.2 with the existing nozzles . in its present state, the maximum design temperature of 680 degrees centigrade for operation at m=9 cannot be realised in the tunnel because of heat loss to the outlet attachments of the heater and quick-acting valve which form, in effect, a large heat sink . because of this heat loss there is rather poor response of stagnation temperature in the working section at the start of a run . it is hoped to cure this by preheating the heater outlet cone and the quick-acting valve . at pressures greater than about 100 p.s.i.g. free convection through the fibrous thermal insulation surrounding the heated core causes the top of the heater shell to become somewhat hotter than the bottom, which results in /hogging/ distortion of the shell . this free convection cools the heater core and a vertical temperature gradient is set up across it after only a few minutes at high pressure . modifications to be incorporated in the heater to improve its performance are described ."

文件3:

" supersonic flow at the surface of a circular cone at angle of attack . formulas for the inviscid flow properties on the surface of a cone at angle of attack are derived for use in conjunction with the m.i.t. cone tables . these formulas are based upon an entropy distribution on the cone surface which is uniform and equal to that of the shocked fluid in the windward meridian plane . they predict values for the flow variables which may differ significantly from the corresponding values obtained directly from the cone tables . the differences in the magnitudes of the flow variables computed by the two methods tend to increase with increasing free-stream mach number, cone angle and angle of attack ."

文件4:

" theory of aircraft structural models subjected to aerodynamic heating and external loads . the problem of investigating the simultaneous effects of transient aerodynamic heating and external loads on aircraft structures for the purpose of determining the ability of the structure to withstand flight to supersonic speeds is studied . by dimensional analyses it is shown that .. constructed of the same materials as the aircraft will be thermally similar to the aircraft with respect to the flow of heat through the structure will be similar to those of the aircraft when the structural model is constructed at the same temperature as the aircraft . external loads will be similar to those of the aircraft . subjected to heating and cooling that correctly simulate the aerodynamic heating of the aircraft, except with respect to angular velocities and angular accelerations, without requiring determination of the heat flux at each point on the surface and its variation with time . acting on the aerodynamically heated structural model to those acting on the aircraft is determined for the case of zero angular velocity and zero angular acceleration, so that the structural model may be subjected to the external loads required for simultaneous simulation of stresses and deformations due to external loads ."

3 个答案:

答案 0 :(得分:2)

由于@mkerrig的答案现在已过期(2020年),因此假设您有gensim 3.8.3个文档列表,这是将BM25与docs一起使用的一种方法。该代码返回最匹配的10个文档的索引。

from gensim import corpora
from gensim.summarization import bm25

texts = [doc.split() for doc in docs] # you can do preprocessing as removing stopwords
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
bm25_obj = bm25.BM25(corpus)
query_doc = dictionary.doc2bow(query.split())
scores = bm25_obj.get_scores(query_doc)
best_docs = sorted(range(len(scores)), key=lambda i: scores[i])[-10:]

请注意,您不再需要average_idf参数。

答案 1 :(得分:0)

我承认上述答案是正确的。但是,我继续为落入此处的其他社区成员添加我的2位。 :)

以下4个链接非常有用,可以全面涵盖该问题。

  1. https://github.com/nhirakawa/BM25 BM25排名函数的Python实现。极其易于使用,我也将其用于我的项目。很棒!我认为这是可以解决您问题的系统。

  2. https://sajalsharma.com/portfolio/cross_language_information_retrieval 演示了在系统中使用Okapi BM25的非常详细和分步的步骤,该系统可用于为当前系统设计任务绘制参考。

  3. http://lixinzhang.github.io/implementation-of-okapi-bm25-on-python.html仅适用于Okapi BM25的代码。

  4. https://github.com/thunlp/EntityDuetNeuralRanking实体-Duet神经排序模型。非常适合研究和学术工作。

和平!

---添加:https://github.com/airalcorn2/RankNet RankNet和LambdaRank

答案 2 :(得分:0)

@fonfonx 给出的答案会起作用。但这不是使用 BM25 的自然方式。 BM25 构造函数需要 List[List[str]]。这意味着它负责获得一个标记化的语料库。

我觉得有一个更好的例子:

from gensim.summarization.bm25 import BM25
corpus = ["The little fox ran home",
          "dogs are the best ",
          "Yet another doc ",
          "I see a little fox with another small fox",
          "last doc without animals"]

def simple_tok(sent:str):
    return sent.split()

tok_corpus = [simple_tok(s) for s in corpus]
bm25 = BM25(tok_corpus)
query = "a little fox".split()
scores = bm25.get_scores(query)

best_docs = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)[:3]
for i, b in enumerate(best_docs):
    print(f"rank {i+1}: {corpus[b]}")

输出:

>> rank 1: I see a little fox with another small fox
>> rank 2: The little fox ran home
>> rank 3: dogs are the best