Skip to main content

PyTerrier components for doc2query

Project description

PyTerrier_doc2query

New: Check out our interactive demo on 🤗 HuggingFace Spaces

New: Improve effectiveness and efficiency using Doc2Query−−

This is the PyTerrier plugin for the docTTTTTquery [Nogueira20] and Doc2Query−− [Gospodinov23] approaches for document expansion by query prediction.

Installation

This repostory can be installed using Pip.

pip install --upgrade git+https://github.com/terrierteam/pyterrier_doc2query.git

What does it do?

A Doc2Query object has a transform() function, which takes the text of each document, and suggests questions for that text.

sample_doc = "The presence of communication amid scientific minds was equally important to the success of the Manhattan Project as scientific intellect was. The only cloud hanging over the impressive achievement of the atomic researchers and engineers is what their success truly meant; hundreds of thousands of innocent lives obliterated"

import pyterrier_doc2query
doc2query = pyterrier_doc2query.Doc2Query()
doc2query([{"docno" : "d1", "text" : sample_doc}])

The resulting dataframe will have an additional "querygen" column, which contains the generated queries, such as:

docno querygen
"d1" 'what was the importance of the manhattan project to the united states atom project? what influenced the success of the united states why was the manhattan project a success? why was it important'

As a PyTerrier transformer, there are lots of ways to introduce Doc2query into a PyTerrier retrieval process.

By default, the plugin loads macavaney/doc2query-t5-base-msmarco, which is a a version of the checkpoint released by the original authors, converted to pytorch format. You can load another T5 model by passing another huggingface model name (or path to model on the file system) by passing it as the first argument:

doc2query = pyterrier_doc2query.Doc2Query('some/other/model')

Using Doc2Query for Indexing

Then, indexing is as easy as instantiating the Doc2Query object and a PyTerrier indexer:

import pyterrier as pt
dataset = pt.get_dataset("irds:vaswani")
import pyterrier_doc2query
doc2query = pyterrier_doc2query.Doc2Query(append=True) # append generated queries to the orignal document text
indexer = doc2query >> pt.IterDictIndexer(index_loc)
indexer.index(dataset.get_corpus_iter())

Doc2Query−−: When Less is More

The performance of Doc2Query can be significantly improved by removing queries that are not relevant to the documents that generated them. This involves first scoring the generated queries (using QueryScorer) and then filtering out the least relevant ones (using QueryFilter).

from pyterrier_doc2query import Doc2Query, QueryScorer, QueryFilter
from pyterrier_dr import ElectraScorer

doc2query = Doc2Query(append=False, num_samples=5)
scorer = ElectraScorer()
indexer = pt.IterDictIndexer('./index')
pipeline = doc2query >> QueryScorer(scorer) >> QueryFilter(t=3.21484375) >> indexer # t=3.21484375 is the 70th percentile for generated queries on MS MARCO

pipeline.index(dataset.get_corpus_iter())

We've also released pre-computed filter scores for various models on HuggingFace datasets:

Using Doc2Query for Retrieval

Doc2query can also be used at retrieval time (i.e. on retrieved documents) rather than at indexing time.

import pyterrier_doc2query
doc2query = pyterrier_doc2query.Doc2Query()

dataset = pt.get_dataset("irds:vaswani")
bm25 = pt.terrier.Retriever.from_dataset("vaswani", "terrier_stemmed", wmodel="BM25")
bm25 >> pt.get_text(dataset) >> doc2query >> pt.text.scorer(body_attr="querygen", wmodel="BM25")

Examples

Check out out the notebooks, even on Colab:

Implementation Details

We use a PyTerrier transformer to rewrite documents by doc2query.

References

Credits

  • Craig Macdonald, University of Glasgow
  • Sean MacAvaney, University of Glasgow
  • Mitko Gospodinov, University of Glasgow

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

pyterrier_doc2query-0.2.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyterrier_doc2query-0.2.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file pyterrier_doc2query-0.2.0.tar.gz.

File metadata

  • Download URL: pyterrier_doc2query-0.2.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pyterrier_doc2query-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3da992d5d4f797f615c274cef3d03715d8e43e824e83866ef9afb36aa721e0cc
MD5 0a5b2301d19b9ae80b3f33b30535fca2
BLAKE2b-256 2fbe3e0797e4295c152c3a98eb841d9a76a4565e6c499344e4f2687d8e83c4a0

See more details on using hashes here.

File details

Details for the file pyterrier_doc2query-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pyterrier_doc2query-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53b6425f8b0cc3b90455aa78e211e8ed9c70a02af1887ce69cff13024055fdf9
MD5 53cfedd7f874e6f22da48c4b0b6e049e
BLAKE2b-256 43670bc870659e24071886c074740ed923bc3195f68ce3a05548f816946eacd5

See more details on using hashes here.

Supported by

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