semanticscholar Python Client

repository·master·Indexed 19 days ago

https://github.com/danielnsilva/semanticscholar

An unofficial Python client library for interacting with Semantic Scholar APIs, including the Academic Graph, Recommendations, and Datasets APIs. It provides typed responses, asynchronous support via AsyncSemanticScholar, and streamlined navigation for paginated results. Key features include paper and author retrieval, keyword searching with bulk retrieval options, and automatic retry mechanisms for rate limiting.

Tokens
5.8K
Snippets
31
Records
34
Agent score
66%

What's inside semanticscholar

  1. Main features of semanticscholar

    master

    The semanticscholar library provides several key capabilities for interacting with Semantic Scholar:

    • Simplified access: Easier interface for Semantic Scholar APIs.
    • API Support: Access to Academic Graph, Recommendations, and Datasets APIs.
    • Typed responses: Responses are returned as typed objects.
    • Pagination: Streamlined navigation of paginated responses.
    • Asynchronous support: Support for asynchronous requests.
  2. Iterate through results using PaginatedResults

    master

    When performing queries that return multiple results, the semanticscholar library returns a PaginatedResults object. This object allows you to iterate through all pages of results seamlessly. You can treat the PaginatedResults instance as an iterable to access individual items across all pages without manually managing page offsets or tokens.

    # Example pattern for iterating through paginated results
    for result in paginated_results:
        print(result)
  3. Access typed responses and raw JSON data

    master

    The library provides typed response objects for easy data extraction. You can access specific fields as attributes (e.g., paper.title).

    If you need the original API response in its dictionary format, use the raw_data attribute. To see all available fields in the response object, use the keys() method.

    paper = sch.get_paper('10.1093/mind/lix.236.433')
    
    # Access typed attributes
    print(paper.title)
    
    # Access original JSON as a dictionary
    print(paper.raw_data)
    
    # List all available fields
    print(paper.keys())
  4. Understand Datasets API concepts

    master

    The Datasets API allows you to access large snapshots of Semantic Scholar data. It is built around three core concepts:

    • Releases: Snapshots of the data at specific points in time (e.g., '2023-12-01'). You can also use 'latest' to refer to the most recent release.
    • Datasets: Specific collections within a release, such as 'papers', 'authors', or 'publications'.
    • Incremental Updates: Diffs between releases that show only the changes, allowing for efficient data synchronization.

    Note: While this library can retrieve diffs, it does not support the actual process of applying those diffs to update a local dataset. For that, you should refer to official documentation regarding tools like Spark.

  5. Quickstart with semanticscholar

    master

    To use the library, import the SemanticScholar class, instantiate the client, and use its methods to fetch data. The client provides typed responses and streamlined navigation for paginated results.

    # First, import the client from semanticscholar module
    from semanticscholar import SemanticScholar
    
    # You'll need an instance of the client to request data from the API
    sch = SemanticScholar()
    
    # Get a paper by its ID
    paper = sch.get_paper('10.1093/mind/lix.236.433')
    
    # Print the paper title
    print(paper.title)
  6. Quickstart with the SemanticScholar client

    master

    To use the library, import the SemanticScholar class, instantiate a client, and use its methods to fetch data. The client provides typed responses and supports various Semantic Scholar APIs including Academic Graph, Recommendations, and Datasets.

    # First, import the client from semanticscholar module
    from semanticscholar import SemanticScholar
    
    # You'll need an instance of the client to request data from the API
    sch = SemanticScholar()
    
    # Get a paper by its ID
    paper = sch.get_paper('10.1093/mind/lix.236.433')
    
    # Print the paper title
    print(paper.title)
  7. Handle paginated search results

    master

    Methods like search_paper and search_author return paginated results (default 100 items per page).

    • Automatic Iteration: Iterating over the result object (e.g., [item for item in results]) automatically handles pagination and retrieves all available items.
    • Access Current Page: Use the .items property to access only the items in the current batch without triggering extra API calls.
    • Manual Pagination: Use .next_page() to fetch the next batch of results and append them to the current list.
    # Iterate through all results automatically
    results = sch.search_paper('Computing Machinery and Intelligence')
    all_results = [item for item in results]
    
    # Access only the first page
    first_page = sch.search_paper('Computing Machinery and Intelligence').items
    
    # Fetch the next page manually
    results = sch.search_paper('Computing Machinery and Intelligence')
    results.next_page()
    first_two_pages = results.items
  8. Install the development version of semanticscholar

    master

    If you need the latest features from the development branch, you can install directly from GitHub using one of two methods:

    Method 1: Manual Clone Clone the repository locally and install the current directory.

    Method 2: VCS Support Install directly via a Git URL using pip.

    # Method 1: Manual Clone
    git clone git@github.com:danielnsilva/semanticscholar.git
    cd semanticscholar
    pip install .
    
    # Method 2: VCS Support
    pip install git+https://github.com/danielnsilva/semanticscholar@master
  9. Basic usage of SemanticScholar

    master

    To use the library, initialize the SemanticScholar class and call its methods (such as get_paper) to retrieve data. The returned objects allow you to access attributes directly.

    from semanticscholar import SemanticScholar
    sch = SemanticScholar()
    paper = sch.get_paper('10.1093/mind/lix.236.433')
    print(paper.title)