Skip to content

Commit

Permalink
normalize how the extra requirement decorators are done
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmacavaney committed Aug 3, 2024
1 parent c820681 commit 7d857e4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pyterrier/anserini/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyterrier.anserini.java import is_installed, set_version, required, J
from pyterrier.anserini.java import is_installed, set_version, pyserini_required, J
from pyterrier.anserini.retriever import AnseriniBatchRetrieve

__all__ = ['is_installed', 'set_version', 'required', 'AnseriniBatchRetrieve', 'J']
__all__ = ['is_installed', 'set_version', 'pyserini_required', 'AnseriniBatchRetrieve', 'J']
7 changes: 2 additions & 5 deletions pyterrier/anserini/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,15 @@ def set_version(version: str):


@pt.utils.pre_invocation_decorator
def required(fn: Callable):
def pyserini_required(fn: Callable):
"""
Requires pyserini to be installed (raises error if not installed). If the JVM has not yet been started, it runs
pt.java.init(), too, similar to pt.java.required().
Requires pyserini to be installed (raises error if not installed).
Can be used as a function/class @decorator. When used as a class decorator, it
is applied to all methods defined by the class.
"""
if not is_installed():
raise RuntimeError('pyserini required to use pyterrier.anserini. `pip install pyserini` and try again.')
if not pt.java.started():
pt.java.init()


J = pt.java.JavaClasses(
Expand Down
5 changes: 3 additions & 2 deletions pyterrier/anserini/retriever.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from warnings import warn
import pandas as pd
import pyterrier as pt
from pyterrier.anserini.java import required
from pyterrier.anserini.java import pyserini_required


@required
@pyserini_required
@pt.java.required
class AnseriniBatchRetrieve(pt.Transformer):
"""
Allows retrieval from an Anserini index. To use this class, you must first enable anserini using `pt.anserini.enable()`.
Expand Down
4 changes: 2 additions & 2 deletions pyterrier/terrier/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ def __init__(self, *args, **kwargs):
kwargs["qemodel"] = "KL"
super().__init__(*args, **kwargs)

@prf_required
@pt.java.required
class RM3(QueryExpansion):
'''
Expand All @@ -376,7 +377,6 @@ class RM3(QueryExpansion):
)
'''
@prf_required
def __init__(self, *args, fb_terms=10, fb_docs=3, fb_lambda=0.6, **kwargs):
"""
Args:
Expand Down Expand Up @@ -408,6 +408,7 @@ def transform(self, queries_and_docs):
self.qe.fbDocs = self.fb_docs
return super().transform(queries_and_docs)

@prf_required
@pt.java.required
class AxiomaticQE(QueryExpansion):
'''
Expand All @@ -422,7 +423,6 @@ class AxiomaticQE(QueryExpansion):
- fb_terms(int): number of feedback terms. Defaults to 10
- fb_docs(int): number of feedback documents. Defaults to 3
'''
@prf_required
def __init__(self, *args, fb_terms=10, fb_docs=3, **kwargs):
"""
Args:
Expand Down

0 comments on commit 7d857e4

Please sign in to comment.