-
Notifications
You must be signed in to change notification settings - Fork 386
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
Changes from 5 commits
74af29e
4fee720
ee3469a
ef672c2
934077d
97cdb7f
8289e57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`: | ||
```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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I think you can just write |
||
|
||
To compute the tf-idf representation of a document, do something like this: | ||
|
||
```python | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, the document returned by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed - can you do that? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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"?