Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get position list function #223

Merged
merged 7 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/usage-indexreader.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ print(doc_vector)
```

The result is a dictionary where the keys are the analyzed terms and the values are the term frequencies.

If you want to know the positions of each term in the document, you can use `get_term_positions`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"of each term" -> "of every term"?

```python
term_positions, indexed_doc = index_reader.get_term_positions('FBIS4-67701')
print(term_positions)
print(indexed_doc)
```
The result is a tuple. The first member is a dictionary where the keys are the analyzed terms and the values are the positions each term occur in the document. The second member is a string containing the recovered document content using the position information.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I think you can just write Tuple[Dict[str, int], str] - a Python programmer should be able to interpret type signatures, and it's more concise.


To compute the tf-idf representation of a document, do something like this:

```python
Expand Down
29 changes: 29 additions & 0 deletions pyserini/index/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,35 @@ def get_document_vector(self, docid: str) -> Optional[Dict[str, int]]:
doc_vector_dict[term] = doc_vector_map.get(JString(term.encode('utf-8')))
return doc_vector_dict

def get_term_positions(self, docid: str) -> Optional[Tuple[Dict[str, int], str]]:
"""Return the term position mapping of the document with ``docid`` and the recovered document using the list. Note that
the term in the document is stemmed and stop words may be removed according to your index settings. Also,
requesting the document vector of a ``docid`` that does not exist in the index will return ``None`` (as opposed
to an empty dictionary); this forces the caller to handle ``None`` explicitly and guards against silent errors.

Parameters
----------
docid : str
Collection ``docid``.

Returns
-------
Optional[Tuple[Dict[str, int], str]]
A tuple contains a dictionary with analyzed terms as keys and corresponding posting list as values, and a
string representing the recovered document
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want to return "string representing the recovered document"? What's the use case? Users can easily do this also if they want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the document returned by doc.contents() is the document before it is processed by Lucene. The reconstructed document will provide a view of the document after Lucene's processing.
Yes. Users can easily do this if they want, and not every user needs this. I think we can move this piece of code to usage-indexreader.md as an example of how to use this function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed - can you do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"""
java_term_position_map = self.object.getTermPositions(self.reader, JString(docid))
if java_term_position_map is None:
return None
term_position_map = {}
term_pos = []
for term in java_term_position_map.keySet().toArray():
term_position_map[term] = java_term_position_map.get(JString(term.encode('utf-8'))).toArray()
for p in term_position_map[term]:
term_pos.append((term, p))
term_pos = sorted(term_pos, key=lambda x: x[1])
return term_position_map, ' '.join([t for t, p in term_pos])

def doc(self, docid: str) -> Optional[Document]:
"""Return the :class:`Document` corresponding to ``docid``. Returns ``None`` if the ``docid`` does not exist
in the index.
Expand Down