Пример #1
0
def get_label(alt_url='https://api.rosette.com/rest/v1/', sentence = "Default Sentence"):

    api = API(user_key=API_KEYS.ROSETTE_API_KEY, service_url=alt_url)
    params = DocumentParameters()
    label_set = set()
    final_response_list = []
    params["content"] = sentence
    params["language"] = "eng"
    try:
        response_dict = json.loads(json.dumps(api.categories(params)))["categories"]
        response_list = []
        for item in response_dict:
            response_list.append([str(item["label"]).replace("_", " "), float(item["confidence"])])

        response_list_sorted = sorted(response_list, key=getKey, reverse=True)

        for item in response_list_sorted:
            label = item[0]
            if not label in label_set:
                final_response_list.append(item)
                label_set.add(label)

        return final_response_list[:5]
    except RosetteException as exception:
        print(exception)
Пример #2
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    url = "Sony Pictures is planning to shoot a good portion of the new "Ghostbusters" in Boston as well."
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)
    params = DocumentParameters()

    # Use a URL to input data instead of a string
    params["contentUri"] = url
    return api.categories(params)
Пример #3
0
def run(alt_url='https://api.rosette.com/rest/v1/', sentence = "Default Sentence"):

    api = API(user_key=API_KEYS.ROSETTE_API_KEY, service_url=alt_url)
    params = DocumentParameters()

    params["content"] = sentence
    try:
        return api.categories(params)
    except RosetteException as exception:
        print(exception)
Пример #4
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    categories_url_data = "http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/"
    url = categories_url_data
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)
    params = DocumentParameters()

    # Use a URL to input data instead of a string
    params["contentUri"] = url
    return api.categories(params)
Пример #5
0
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
    """ Run the example """
    categories_url_data = "https://onlocationvacations.com/2018/02/06/downton-abbey-exhibition-extended-april-2-nyc/"
    url = categories_url_data
    # Create an API instance
    api = API(user_key=key, service_url=alt_url)

    # Set selected API options
    # For more information on the functionality of these
    # and other available options, see Rosette Features & Functions
    # https://developer.rosette.com/features-and-functions#categorization

    # api.set_option('singleLabel', 'true')
    # api.set_option('scoreThreshold',- 0.20)

    params = DocumentParameters()

    # Use a URL to input data instead of a string
    params["contentUri"] = url
    try:
        return api.categories(params)
    except RosetteException as exception:
        print(exception)
Пример #6
0
"""
Example code to call Rosette API to get the category of a document (at a given URL).
"""

import argparse
import pprint

from rosette.api import API, DocumentParameters

parser = argparse.ArgumentParser(description="Get the category of a piece of a document at a URL")
parser.add_argument("--key", required=True, help="Rosette API key")
parser.add_argument("--service_url", nargs="?", help="Optional user service URL")
parser.add_argument("--url", nargs="?", default="http://www.basistech.com/about/", help="Optional URL for data")
args = parser.parse_args()

# Create an API instance
if args.service_url:
    api = API(service_url=args.service_url, user_key=args.key)
else:
    api = API(user_key=args.key)

params = DocumentParameters()

# Use a URL to input data instead of a string
params["contentUri"] = args.url

result = api.categories(params)

pprint.pprint(result)
Пример #7
0
class MonitorBasisAPI():
    API_KEY = "cfdb0d7990f5c1d2546c61a94cf0a445"
    DO_NOT_CALL = False

    def __init__(self):
        self.rosette_handle = API(user_key=self.API_KEY)
        # altUrl='https://api.rosette.com/rest/v1/'
        # OPTION: service_url=altUrl

        result = self.rosette_handle.ping()
        print("/ping: ", result)

    def temp_demo(self):
        pass

    # NOT TESTED
    def get_basis_sentiment(self, txt_str):
        params = DocumentParameters()
        params["language"] = "eng"

        # Use an HTML file to load data instead of a string
        params.load_document_string(txt_str)
        try:
            result = self.rosette_handle.sentiment(params)
        except RosetteException as e:
            print(e)
        finally:
            print " DONE "

    # NOT TESTED
    def get_basis_categories(self, txt_str):
        # if self.DO_NOT_CALL == True:
        #     return "POS"

        params = DocumentParameters()
        params["language"] = "eng"

        # Use an HTML file to load data instead of a string
        params.load_document_string(txt_str)

        typed_relations_resp = self.rosette_handle.categories(text=txt_str)

        print " >>> CALLED IBM - typed_relations_resp: " + str(
            typed_relations_resp)
        return json.dumps(typed_relations_resp)

    def get_basis_service_by_tag(self, txt_str, basis_str):
        '''
            To Be Determined:
            - syntax_dependencies
            - morphology
            - name_translation
            - name_similarity
        '''
        print "~~~~~~~~~~~~~~~~~~~~~~~~~~~ going to get_basis_service_by_tag: " + str(
            basis_str)

        # if ibm_str == "authors":
        #     return self.get_basis_service(txt_str, self.rosette_handle.authors)
        # elif ibm_str == "concepts":
        #     return self.get_basis_service(txt_str, self.rosette_handle.concepts)
        # elif ibm_str == "dates":
        #     return self.get_basis_service(txt_str, self.rosette_handle.dates)
        # elif ibm_str == "emotion":
        #     return self.get_basis_service(txt_str, self.rosette_handle.emotion)
        # elif ibm_str == "feeds":
        #     return self.get_basis_service(txt_str, self.rosette_handle.feeds)
        # elif ibm_str == "keywords":
        #     return self.get_basis_service(txt_str, self.rosette_handle.keywords)
        # elif ibm_str == "microformats":
        #     return self.get_basis_service(txt_str, self.rosette_handle.microformats)
        # elif ibm_str == "publication_date":
        #     return self.get_basis_service(txt_str, self.rosette_handle.publication_date)
        #

        # CURRENT LIST:  "entities", "language", "relationships", "sentences", "sentiment", "syntax_dependencies", "tokens"

        if basis_str == "entities":
            return self.get_basis_service(
                txt_str, self.rosette_handle.entities
            )  # also could use  params["genre"] = "social-media"
        elif basis_str == "language":
            return self.get_basis_service(txt_str,
                                          self.rosette_handle.language)  #!
        elif basis_str == "relationships":
            return self.get_basis_service(
                txt_str, self.rosette_handle.relationships)  #!
        elif basis_str == "sentences":
            return self.get_basis_service(txt_str,
                                          self.rosette_handle.sentences)  #!
        elif basis_str == "sentiment":
            return self.get_basis_service(txt_str,
                                          self.rosette_handle.sentiment)
        elif basis_str == "syntax_dependencies":
            return self.get_basis_service(
                txt_str, self.rosette_handle.syntax_dependencies)
        elif basis_str == "tokens":
            return self.get_basis_service(txt_str,
                                          self.rosette_handle.tokens)  #!

        # elif ibm_str == "title":
        #     return self.get_basis_service(txt_str, self.rosette_handle.title)
        # elif ibm_str == "typed_relations":
        #     return self.get_basis_service(txt_str, self.rosette_handle.typed_relations)

    def get_basis_service(self, txt_str, basis_func):
        if self.DO_NOT_CALL == True:
            return "POS"
        callStart = datetime.datetime.now()

        params = DocumentParameters()
        params["language"] = "eng"

        # Use an HTML file to load data instead of a string
        params.load_document_string(txt_str)

        # TODO: determine when to use
        # params["content"]  vs load_document_string

        resp = {"return_data": basis_func(params)}

        # print " >>> CALLED IBM - resp: " + str(resp)

        resp_time = (datetime.datetime.now() - callStart).total_seconds()
        resp['resp_time'] = resp_time
        return resp


# mba = MonitorBasisAPI()