added RetrieveSentences.py (#73)

This commit is contained in:
MeowFei
2017-10-28 15:05:52 -04:00
committed by rosequ
parent 491f0b32d5
commit 00ef2d0fde
2 changed files with 115 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
## Retrieve Sentences
#### 1. Clone [Anserini](https://github.com/castorini/Anserini.git) and [Castor](https://github.com/castorini/Castor.git)
```bash
git clone https://github.com/castorini/Anserini.git
git clone https://github.com/castorini/Castor.git
```
Your directory structure should look like
```
├── Anserini
└── Castor
```
#### 2. Compile Anserini
```bash
cd Anserini
mvn package
```
This creates `anserini-0.0.1-SNAPSHOT.jar` at `Anserini/target`
#### 3. Download Dependencies
- Download the index from [here](https://drive.google.com/open?id=0B2u_nClt6NbzcllGZTVtX2Q4bkk)
- Download the Google word2vec file from [here](https://drive.google.com/drive/folders/0B2u_nClt6NbzNWJkWExmaklYNTA?usp=sharing)
#### 4. Run the following command
```bash
python ./anserini_dependency/RetrieveSentences.py
```
Possible parameters are:
| option | input format | default | description |
|----------------|--------------|---------|-------------|
| `-index` | string | N/A | Path of the Lucene index |
| `-embeddings` | string | "" | Path of the word2vec index |
| `-topics` | string | "" | topics file |
| `-query` | string | "" | a single query |
| `-hits` | [1, inf) | 100 | max number of hits to return |
| `-scorer` | string | Idf | passage scores (Idf or Wmd) |
| `-k` | [1, inf) | 1 | top-k passages to be retrieved |
Note: Either a query or a topic must be passed in as an argument; they can't be both empty.
+69
View File
@@ -0,0 +1,69 @@
import argparse
import jnius_config
jnius_config.set_classpath("../Anserini/target/anserini-0.0.1-SNAPSHOT.jar")
from jnius import autoclass
class CallRetrieveSentences:
"""Python class built to call RetrieveSentences
Attributes
----------
rs : io.anserini.qa.RetrieveSentences
The RetrieveSentences object
args : io.anserini.qa.RetrieveSentences$Args
The arguments for constructing RetrieveSentences object as well as calling getRankedPassages
"""
def __init__(self, args):
"""
Constructor for the CallRetrieveSentences class.
Parameters
----------
args : argparse.Namespace
Arguments needed for constructing an instance of RetrieveSentences class
"""
RetrieveSentences = autoclass("io.anserini.qa.RetrieveSentences")
Args = autoclass("io.anserini.qa.RetrieveSentences$Args")
String = autoclass("java.lang.String")
self.args = Args()
index = String(args.index)
self.args.index = index
embeddings = String(args.embeddings)
self.args.embeddings = embeddings
topics = String(args.topics)
self.args.topics = topics
query = String(args.query)
self.args.query = query
self.args.hits = int(args.hits)
scorer = String(args.scorer)
self.args.scorer = scorer
self.args.k = int(args.k)
self.rs = RetrieveSentences(self.args)
def getRankedPassages(self):
"""
Call RetrieveSentneces.getRankedPassages
"""
self.rs.getRankedPassages(self.args)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Retrieve Sentences')
parser.add_argument("-index", help="Lucene index", required=True)
parser.add_argument("-embeddings", help="Path of the word2vec index", default="")
parser.add_argument("-topics", help="topics file", default="")
parser.add_argument("-query", help="a single query", default="")
parser.add_argument("-hits", help="max number of hits to return", default=100)
parser.add_argument("-scorer", help="passage scores", default="Idf")
parser.add_argument("-k", help="top-k passages to be retrieved", default=1)
args_raw = parser.parse_args()
rs = CallRetrieveSentences(args_raw)
rs.getRankedPassages()