beyond_codes

Assignment of beyond codes

https://github.com/kkewat/beyond_codes

Science Score: 31.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (6.8%) to scientific vocabulary
Last synced: 10 months ago · JSON representation ·

Repository

Assignment of beyond codes

Basic Info
  • Host: GitHub
  • Owner: kkewat
  • Language: Python
  • Default Branch: main
  • Size: 8.79 KB
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created about 2 years ago · Last pushed about 2 years ago
Metadata Files
Readme Citation

README.md

Beyond_Codes

Assignment of beyond codes

Program for identifying the citation from API Response

This program retrieves the data from the paginated API and the checks it’s citation and returns the valid citation based on the response text and source information.

Features:

  1. Fetch the data from the pages of the API above.
  2. Identifies citations based on similarity between response text and source context using a threshold.
  3. List down the sources from which the response was formed. Returns an empty array if the response did not come from any source. The shortlisted sources will be called citations
  4. Return the citations for all objects coming from the API.
  5. Prints identified citations in JSON format and a user-friendly table.

Requirements:-

• Python 3.10 or above

• requests library (pip install requests)

Usage :-

• install the required libraries

• save the code as name.py(eg. Citation.py)

• run the file in cmd as python name.py

Owner

  • Name: kunal kewat
  • Login: kkewat
  • Kind: user

Citation (citation.py)

import requests
import json

def get_data(url):
    data = []
    while url:
        try:
            response = requests.get(url)
            response.raise_for_status()  # Raise an error for bad status codes
            response_data = response.json()
        except requests.exceptions.RequestException as e:
            print(f"Request error: {e}",e)
            break
        except requests.exceptions.JSONDecodeError:
            print("Error: Invalid JSON response from API.")
            break
        except Exception as e:
            print(f"An unexpected error occurred: {e}",e)
            break

        page_data = response_data.get("data", {}).get("data", [])
        data.extend(page_data)
        url = response_data.get("data", {}).get("next_page_url")
    #print(data)
    return data

from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

def identify_citations(response, sources):
    citations = []
    response_lower = response.lower()
    for source in sources:
        context = source["context"]
        link = source.get("link", "")
        if isinstance(context, str) and similar(context.lower(), response_lower) > 0.2 and link:
            citations.append({"id": source["id"], "link": link})
        elif isinstance(context, list):
            for ctx in context:
                if similar(ctx.lower(), response_lower) > 0.2 and link:
                    citations.append({"id": source["id"], "link": link})
                    break
    return citations

def main():
  url = "https://devapi.beyondchats.com/api/get_message_with_sources"
  all_data = get_data(url)

  citations = []
  for item in all_data:
    response = item["response"]
    sources = item["source"]
    citation = identify_citations(response, sources)
    citations.extend(citation)

  print("Citations:")
  print(citations)
  for citation in citations:
    #print(citation)
    print(f"\tID: {citation['id']}, Link: {citation.get('link', '')}")

if __name__ == "__main__":
  main()

GitHub Events

Total
Last Year