Skip to content

Commit

Permalink
Merge pull request #100 from david-rohrschneider/autocomplete-api
Browse files Browse the repository at this point in the history
Feature: autocomplete api
  • Loading branch information
danielnsilva authored Dec 24, 2024
2 parents e4aee4c + a2a9fce commit 04fb41a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
24 changes: 24 additions & 0 deletions semanticscholar/AsyncSemanticScholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from semanticscholar.PaginatedResults import PaginatedResults
from semanticscholar.Paper import Paper
from semanticscholar.Reference import Reference
from semanticscholar.Autocomplete import Autocomplete

logger = logging.getLogger('semanticscholar')

Expand Down Expand Up @@ -796,3 +797,26 @@ async def get_recommended_papers_from_lists(
papers = [Paper(item) for item in data['recommendedPapers']]

return papers

async def get_autocomplete(self, query: str) -> list[Autocomplete]:
"""Get autocomplete suggestions for a query.
:calls: `GET /graph/v1/paper/autocomplete?query={query} \
<https://api.semanticscholar.org/api-docs/graph#tag/\
Paper-Data/operation/get_graph_get_paper_autocomplete>`_
:param str query: query to get autocomplete suggestions for.
:returns: list of autocomplete suggestions.
:rtype: :class:`List` of :class:`semanticscholar.Autocomplete.Autocomplete`
"""
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f"{base_url}/paper/autocomplete"

parameters = f"query={query}"

data = await self._requester.get_data_async(url, parameters, self.auth_header)

if not data or "matches" not in data:
return []

return [Autocomplete(suggestion) for suggestion in data["matches"]]
34 changes: 34 additions & 0 deletions semanticscholar/Autocomplete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from semanticscholar.SemanticScholarObject import SemanticScholarObject


class Autocomplete(SemanticScholarObject):
def __init__(self, data: dict) -> None:
super().__init__()
self._id = None
self._title = None
self._authors_year = None
self._init_attributes(data)

def _init_attributes(self, data: dict) -> None:
self._data = data

if "id" in data:
self._id = data["id"]

if "title" in data:
self._title = data["title"]

if "authorsYear" in data:
self._authors_year = data["authorsYear"]

@property
def id(self) -> str:
return self._id

@property
def title(self) -> str:
return self._title

@property
def authors_year(self) -> str:
return self._authors_year
20 changes: 20 additions & 0 deletions semanticscholar/SemanticScholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from semanticscholar.AsyncSemanticScholar import AsyncSemanticScholar
from semanticscholar.Author import Author
from semanticscholar.Paper import Paper
from semanticscholar.Autocomplete import Autocomplete


class SemanticScholar():
Expand Down Expand Up @@ -586,3 +587,22 @@ def get_recommended_papers_from_lists(
)

return papers

def get_autocomplete(self, query: str) -> list[Autocomplete]:
"""Get autocomplete suggestions for a query.
:calls: `GET /graph/v1/paper/autocomplete?query={query} \
<https://api.semanticscholar.org/api-docs/graph#tag/\
Paper-Data/operation/get_graph_get_paper_autocomplete>`_
:param str query: query to get autocomplete suggestions for.
:returns: list of autocomplete suggestions.
:rtype: :class:`List` of :class:`semanticscholar.Autocomplete.Autocomplete`
"""

loop = asyncio.get_event_loop()
results = loop.run_until_complete(
self._AsyncSemanticScholar.get_autocomplete(query=query)
)

return results

0 comments on commit 04fb41a

Please sign in to comment.