bmp 0.2__cp37-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bmp/__init__.py +1 -0
- bmp/_bmp.pyd +0 -0
- bmp/pyterrier.py +79 -0
- bmp-0.2.dist-info/METADATA +71 -0
- bmp-0.2.dist-info/RECORD +7 -0
- bmp-0.2.dist-info/WHEEL +4 -0
- bmp-0.2.dist-info/entry_points.txt +2 -0
bmp/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from bmp._bmp import ciff2bmp, search, Searcher, InvertedIndexer, Indexer
|
bmp/_bmp.pyd
ADDED
|
Binary file
|
bmp/pyterrier.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import pyterrier as pt
|
|
4
|
+
import pyterrier_alpha as pta
|
|
5
|
+
from bmp import Indexer, Searcher
|
|
6
|
+
|
|
7
|
+
class BmpIndex(pta.Artifact, pt.Indexer):
|
|
8
|
+
ARTIFACT_TYPE = 'sparse_index'
|
|
9
|
+
ARTIFACT_FORMAT = 'bmp'
|
|
10
|
+
def __init__(self, path: str, *, verbose: bool = True):
|
|
11
|
+
super().__init__(path)
|
|
12
|
+
self.verbose = verbose
|
|
13
|
+
self._searcher = None
|
|
14
|
+
def built(self) -> bool:
|
|
15
|
+
return Path(self.path).exists()
|
|
16
|
+
def indexer(self, bsize=32, compress_range=False, scale_float=100.):
|
|
17
|
+
return BmpIndexer(self, bsize=bsize, compress_range=compress_range, scale_float=scale_float)
|
|
18
|
+
def index(self, inp):
|
|
19
|
+
return self.indexer().index(inp)
|
|
20
|
+
def retriever(self):
|
|
21
|
+
return BmpRetriever(self)
|
|
22
|
+
def transform(self, inp):
|
|
23
|
+
return self.retriever()(inp)
|
|
24
|
+
def load_into_memory(self):
|
|
25
|
+
if self._searcher is None:
|
|
26
|
+
self._searcher = Searcher(str(self.path/'index.bmp'))
|
|
27
|
+
return self._searcher
|
|
28
|
+
def __enter__(self):
|
|
29
|
+
return self
|
|
30
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
31
|
+
self.close()
|
|
32
|
+
def close(self):
|
|
33
|
+
self._searcher = None
|
|
34
|
+
|
|
35
|
+
class BmpIndexer(pt.Indexer):
|
|
36
|
+
def __init__(self, bmp_index, bsize=32, compress_range=False, scale_float=100.):
|
|
37
|
+
self.bmp_index = bmp_index
|
|
38
|
+
self.bsize = bsize
|
|
39
|
+
self.compress_range = compress_range
|
|
40
|
+
self.scale_float = scale_float
|
|
41
|
+
def index(self, inp):
|
|
42
|
+
assert not self.bmp_index.built()
|
|
43
|
+
with pta.ArtifactBuilder(self.bmp_index) as builder:
|
|
44
|
+
indexer = Indexer(str(self.bmp_index.path/'index.bmp'), bsize=self.bsize, compress_range=self.compress_range)
|
|
45
|
+
count = 0
|
|
46
|
+
for doc in inp:
|
|
47
|
+
vector = doc['toks']
|
|
48
|
+
if len(vector) > 0 and isinstance(next(iter(vector.values())), float):
|
|
49
|
+
vector = {k: int(v * self.scale_float) for k, v in vector.items()}
|
|
50
|
+
indexer.add_document(doc['docno'], vector)
|
|
51
|
+
count += 1
|
|
52
|
+
indexer.finish()
|
|
53
|
+
builder.metadata['bsize'] = self.bsize
|
|
54
|
+
builder.metadata['compress_range'] = self.compress_range
|
|
55
|
+
builder.metadata['scale_float'] = self.scale_float
|
|
56
|
+
builder.metadata['num_docs'] = count
|
|
57
|
+
return self
|
|
58
|
+
|
|
59
|
+
class BmpRetriever(pt.Indexer):
|
|
60
|
+
def __init__(self, bmp_index, *, num_results=1000, alpha=1.0, beta=1.0):
|
|
61
|
+
self.bmp_index = bmp_index
|
|
62
|
+
self.num_results = num_results
|
|
63
|
+
self.alpha = alpha
|
|
64
|
+
self.beta = beta
|
|
65
|
+
def transform(self, inp):
|
|
66
|
+
pta.validate.query_frame(inp, extra_columns=['query_toks'])
|
|
67
|
+
searcher = self.bmp_index.load_into_memory()
|
|
68
|
+
res = pta.DataFrameBuilder(['docno', 'score', 'rank'])
|
|
69
|
+
for toks in inp['query_toks']:
|
|
70
|
+
docnos, scores = searcher.search(toks, k=self.num_results, alpha=self.alpha, beta=self.beta)
|
|
71
|
+
res.extend({
|
|
72
|
+
'docno': docnos,
|
|
73
|
+
'score': scores,
|
|
74
|
+
'rank': np.arange(len(scores))
|
|
75
|
+
})
|
|
76
|
+
return res.to_df(inp)
|
|
77
|
+
def fuse_rank_cutoff(self, k):
|
|
78
|
+
if self.num_results > k:
|
|
79
|
+
return BmpRetriever(self.bmp_index, num_results=k, alpha=self.apha, beta=self.beta)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: bmp
|
|
3
|
+
Version: 0.2
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
7
|
+
Requires-Dist: python-terrier; extra == 'pyterrier'
|
|
8
|
+
Requires-Dist: pyterrier-alpha; extra == 'pyterrier'
|
|
9
|
+
Provides-Extra: pyterrier
|
|
10
|
+
Requires-Python: >=3.7
|
|
11
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<img width="100px" src="../img/logo.jpg" />
|
|
16
|
+
<img width="100px" src="../img/plus.png" />
|
|
17
|
+
<img width="100px" src="../img/python.png" />
|
|
18
|
+
<h1 align="center">BMP</h1>
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
From PyPi:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
pip install bmp
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
From Source:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
pip install maturin
|
|
33
|
+
maturin build -r
|
|
34
|
+
pip install target/wheels/*.whl
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### Index from CIFF
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from bmp import ciff2bmp
|
|
43
|
+
ciff2bmp(ciff_file="/path/to/ciff", output="/path/to/index", bsize=32, compress_range=False)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Index with Python
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from bmp import Indexer
|
|
50
|
+
indexer = Indexer('/path/to/index', bsize=32, compress_range=False)
|
|
51
|
+
indexer.add_document('doc1', {'a': 1, 'b': 5, 'c': 8}) # docid, vector
|
|
52
|
+
indexer.add_document('doc2', {'a': 2, 'c': 1, 'd': 8, 'f': 2})
|
|
53
|
+
...
|
|
54
|
+
indexer.finish()
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Search
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from bmp import search, Searcher
|
|
61
|
+
|
|
62
|
+
# batch operation
|
|
63
|
+
results = search(index="/path/to/index", queries="/path/to/queries", k=10, alpha=1.0, beta=1.0)
|
|
64
|
+
# -> str (TREC run file)
|
|
65
|
+
|
|
66
|
+
# query-by-query operation
|
|
67
|
+
searcher = Searcher("/path/to/index") # loads index into memory once
|
|
68
|
+
searcher.search({'tok1': 5.3, 'tok2': 1.1}, k=10, alpha=1.0, beta=1.0)
|
|
69
|
+
# -> Tuple[List[str], List[float]] (doc IDs, scores) for this query
|
|
70
|
+
```
|
|
71
|
+
|
bmp-0.2.dist-info/RECORD
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
bmp-0.2.dist-info/METADATA,sha256=XCTAvc7nosH_ZAG5CPKw5onCbUydOMXASPYFPqOVHk0,1739
|
|
2
|
+
bmp-0.2.dist-info/WHEEL,sha256=5EEVb2qtUBAftJIn0bFfpziowjNUtiDBo-e9pkgBCo8,96
|
|
3
|
+
bmp-0.2.dist-info/entry_points.txt,sha256=7T9_5X3HnKORahbEc7zjcrSMKd4gluc-v7sXmgx7BMs,61
|
|
4
|
+
bmp/pyterrier.py,sha256=JRRSGp9eXcnd0lvU3injJ3nUZENThdf4CpPJzWEiiR0,3317
|
|
5
|
+
bmp/__init__.py,sha256=MlXKBU0lt2uGHecHHOW-ndvEOqjBEFBfqsrzAK-uNM8,75
|
|
6
|
+
bmp/_bmp.pyd,sha256=MVSrwOrPVGw63d2wYuUplFnW2CfSKRa1_hf1XuHWpeA,1471488
|
|
7
|
+
bmp-0.2.dist-info/RECORD,,
|
bmp-0.2.dist-info/WHEEL
ADDED