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
improve variable and function naming
  • Loading branch information
nsndimt committed Jul 27, 2020
commit ef672c271bb2c27c1ebb952a628bea594ffbe93f
20 changes: 10 additions & 10 deletions pyserini/index/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ 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_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,
def get_position_list(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.

Expand All @@ -277,17 +277,17 @@ def get_document_postings(self, docid: str) -> Optional[Tuple[Dict[str, int], st
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:
java_term_position_map = self.object.getPositionList(self.reader, JString(docid))
if java_term_position_map is None:
return None
doc_posting_dict = {}
term_position_map = {}
term_pos = []
for term in doc_posting_map.keySet().toArray():
doc_posting_dict[term] = doc_posting_map.get(JString(term.encode('utf-8'))).toArray()
for p in doc_posting_dict[term]:
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 doc_posting_dict, ' '.join([t for t, p in term_pos])
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
Expand Down