Skip to main content

Fast text match algorithm implementation for bm25

Project description

Installation

The easiest way to install this package is through pip, using

pip install fastbm25

fastbm25

The fast bm25 algorithm for text match optimized by reverted index. So the complexity will be no more than O(N(log N)).

(利用倒排索引加速的bm25文本匹配算法,从一堆数据中寻找最相似的文本)

usage

find top k similar sentences from corpus; Note you should tokenize text and use stop words in advance

from fastbm25 import fastbm25

corpus = [
    "How are you !",
    "Hello Jack! Nice to meet you!",
    "I am from China, I like math."
]
tokenized_corpus = [doc.lower().split(" ") for doc in corpus]
model = fastbm25(tokenized_corpus)
query = "where are you from".lower().split()
result = model.top_k_sentence(query,k=1)
print(result)

The result is list of tuple like

[('I am from China, I like math.', 2, -0.06000000000000001)]

For some language like Chinese that doesn't need tokenization. you can use this example

from fastbm25 import fastbm25

corpus = [
    "张三考上了清华",
    "李四考上了北大",
    "我烤上了地瓜.",
    "我们都有光明的未来."
]
model = fastbm25(corpus)
query = "我考上了大学"
result = model.top_k_sentence(query,k=1)
print(result)

[('李四考上了北大', 1, 1.21)]

find document pair similarity between document a and document b

Note that a and b don't need to be included in the reference corpus;

from fastbm25 import fastbm25
corpus = [
    "How are you !",
    "Hello Jack! Nice to meet you!",
    "I am from China, I like math."
]
tokenized_corpus = [doc.lower().split(" ") for doc in corpus]
model = fastbm25(tokenized_corpus)
document_a = "where are you from".lower().split()
document_b = "where are you".lower().split()

result = model.similarity_bm25(document_a,document_b)
print(result)

1.944187075527278

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastbm25-0.0.2.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

fastbm25-0.0.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file fastbm25-0.0.2.tar.gz.

File metadata

  • Download URL: fastbm25-0.0.2.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for fastbm25-0.0.2.tar.gz
Algorithm Hash digest
SHA256 fa196dc4b645b001611e64b94ec413045d2893860a41cec42574bf0fe080bf74
MD5 84dccee50162d9358fce482447c37286
BLAKE2b-256 54072316bd4a224ea3fbc88c3c1d0ce5aff09b71996a619ba35d012e3e50be14

See more details on using hashes here.

File details

Details for the file fastbm25-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: fastbm25-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for fastbm25-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7fe55114085216124cbc4dd0f9c92ea19f5d78694beffb214728df5bc3318c6e
MD5 746c05634ac7d5692771b3268f1abc34
BLAKE2b-256 6eb0ab1b2a84eb9982b3a491b61cd71baa289dc2de3dd18966e3aef5c3fa3e0e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page