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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
merge two close related functions
  • Loading branch information
nsndimt committed Jul 25, 2020
commit ee3469a6732f6dfae16e0e765457e3606fa37890
42 changes: 12 additions & 30 deletions pyserini/index/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,11 @@ 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_document_posting(self, docid: str) -> Optional[Dict[str, int]]:
"""Return the posting list of the document with ``docid``. Note that 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.
def get_document_postings(self, docid: str) -> Optional[Tuple[Dict[str, int], str]]:
"""Return the posting list of the document with ``docid`` and the recovered document using the list. Note that
the term in the document is stemmed and stopwords may be removed according to your index setting. 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
----------
Expand All @@ -272,40 +273,21 @@ def get_document_posting(self, docid: str) -> Optional[Dict[str, int]]:

Returns
-------
Optional[Dict[str, List[int]]]
A dictionary with analyzed terms as keys and their posting list as values.
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

"""
doc_posting_map = self.object.getDocumentPostings(self.reader, JString(docid))
if doc_posting_map is None:
return None
doc_posting_dict = {}
term_pos = []
for term in doc_posting_map.keySet().toArray():
doc_posting_dict[term] = doc_posting_map.get(JString(term.encode('utf-8'))).toArray()
return doc_posting_dict

def reorganize_postings(self, postings):
"""Return the recovered document from the posting list returned by get_document_posting. Note that the
term in the document is stemmed and stopwords may be removed according to your index setting

Parameters
----------
postings : [Dict[str, List[int]]]
posting list returned by get_document_posting.

Returns
-------
str
A string contains the recovered document.
"""
term_pos = []
for k, v in postings.items():
for p in v:
term_pos.append((k, p))
for p in doc_posting_dict[term]:
term_pos.append((term, p))
term_pos = sorted(term_pos, key=lambda x: x[1])
for i, (t, p) in enumerate(term_pos):
if i != p:
print(i, t, p)
return ' '.join([t for t, p in term_pos])
return doc_posting_dict, ' '.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
Expand Down