Example #1
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create default file to read from
    f = tempfile.NamedTemporaryFile(suffix=".html")
    sentiment_file_data = "<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>"
    message = sentiment_file_data
    f.write(message)
    f.seek(0)

    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

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

    # Use an HTML file to load data instead of a string
    params.load_document_file(f.name)
    try:
        result = api.sentiment(params)
    except RosetteException as e:
        print(e)
    finally:
        # Clean up the file
        f.close()

    return result
Example #2
0
def test_debug():
    # Doesn't really matter what it returns for this test, so just making sure it catches all of them
    endpoints = ["categories", "entities", "entities/linked", "language", "matched-name", "morphology-complete",
                 "sentiment", "translated-name", "relationships"]
    expected_status_filename = response_file_dir + "eng-sentence-entities.status"
    expected_output_filename = response_file_dir + "eng-sentence-entities.json"
    for rest_endpoint in endpoints:
        httpretty.register_uri(httpretty.POST, "https://api.rosette.com/rest/v1/" + rest_endpoint,
                               status=get_file_content(expected_status_filename),
                               body=get_file_content(expected_output_filename),
                               content_type="application/json")

    with open(expected_output_filename, "r") as expected_file:
        expected_result = json.loads(expected_file.read())

    # need to mock /info call too because the api will call it implicitly
    with open(response_file_dir + "info.json", "r") as info_file:
        body = info_file.read()
        httpretty.register_uri(httpretty.GET, "https://api.rosette.com/rest/v1/info",
                               body=body, status=200, content_type="application/json")
        httpretty.register_uri(httpretty.POST, "https://api.rosette.com/rest/v1/info",
                               body=body, status=200, content_type="application/json")

    api = API("0123456789", debug=True)

    content = "He also acknowledged the ongoing U.S. conflicts in Iraq and Afghanistan, noting that he is the \"commander in chief of a country that is responsible for ending a war and working in another theater to confront a ruthless adversary that directly threatens the American people\" and U.S. allies."

    params = DocumentParameters()
    params.__setitem__("content", content)
    api.entities(params)

    # Check that the most recent querystring had debug=true
    assert httpretty.last_request().querystring == {'debug': ['true']}
Example #3
0
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
    """ Run the example """
    # Create default file to read from
    temp_file = tempfile.NamedTemporaryFile(suffix=".html")
    sentiment_file_data = "<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>"
    message = sentiment_file_data
    temp_file.write(
        message if isinstance(message, bytes) else message.encode())
    temp_file.seek(0)

    # 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#sentiment-analysis

    # api.set_option('modelType','dnn') #Valid for English only

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

    # Use an HTML file to load data instead of a string
    params.load_document_file(temp_file.name)
    try:
        result = api.sentiment(params)
    except RosetteException as exception:
        print(exception)
    finally:
        # Clean up the file
        temp_file.close()

    return result
Example #4
0
    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 "
Example #5
0
    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)
Example #6
0
def get_vector(folder, file):
    f = open(folder + file, "r")
    embeddings_data = f.read()

    api = API(user_key="", service_url="http://localhost:8181/rest/v1/")
    params = DocumentParameters()
    params["content"] = embeddings_data

    try:
        vector = api.text_embedding(params)
        save_vector(file, vector, "doc", "")
    except RosetteException as exception:
        print(exception)
    except:
        print('unkown error in get vector')

    try:
        sentences = api.sentences(params)
        sentences = sentences.get('sentences')
        for x in sentences:
            params["content"] = x
            sentence_vector = api.text_embedding(params)
            save_vector(file, sentence_vector, "sen", x)
    except RosetteException as exception:
        print(exception)
    except:
        print('unkown sentence error')
    def analyse_rosette(self, tweet):
        params = DocumentParameters()
        params["content"] = tweet
        params["genre"] = "social-media"

        extracted_entities = []
        try:
            entities = self.rosette.entities(params)
            for entity in entities['entities']:
                doc = {
                    "entity": entity['mention'].lower(),
                    "type": entity["type"],
                }
                if "confidence" in entity:
                    doc["certainty"] = entity["confidence"]
                    extracted_entities.append(doc)

                elif "linkingConfidence" in entity:
                    doc["certainty"] = entity["linkingConfidence"]
                    extracted_entities.append(doc)

            return extracted_entities
        except RosetteException as exception:
            warns = ['meaningful', 'Language']
            # if ['meaningful', 'Language'] not in exception.message:
            if not any(warn in exception.message for warn in warns):
                raise RosetteException(
                    status=exception.status,
                    message=exception.message,
                    response_message=exception.response_message)

            else:
                return []
Example #8
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"
    # params["modelType"] = "dnn"
    try:
        response_dict = json.loads(json.dumps(
            api.sentiment(params)))["document"]
        response_list = []
        if str(response_dict["label"]) == "pos":
            response_list.append(
                ["POSITIVE", float(response_dict["confidence"])])
        elif str(response_dict["label"]) == "neg":
            response_list.append(
                ["NEGATIVE", float(response_dict["confidence"])])
        else:
            response_list.append(
                ["NEUTRAL", float(response_dict["confidence"])])
        # ["sentiment"]
        # print response_dict
        return response_list[0]
    except RosetteException as exception:
        print(exception)
Example #9
0
def request(content, endpoint, api, language=None, uri=False, **kwargs):
    """Request Rosette API results for the given content and endpoint.

    This method gets the requested results from the Rosette API as JSON.  If
    api's output parameter has been set to "rosette" then the JSON will consist
    of an A(nnotated) D(ata) M(odel) or ADM.  An ADM is a Python dict
    representing document content, annotations of the document content,
    and document metadata.
    
    content:  path or URI of a document for the Rosette API to process
    endpoint: a Rosette API endpoint string (e.g., 'entities')
              (see https://developer.rosette.com/features-and-functions)
    api:      a rosette.api.API instance
              (e.g., API(user_key=<key>, service_url=<url>))
    language: an optional ISO 639-2 T language code
              (the Rosette API will automatically detect the language of the
              content by default)
    uri:      specify that the content is to be treated as a URI and the
              the document content is to be extracted from the URI
    kwargs:   additional keyword arguments
              (e.g., if endpoint is 'morphology' you can specify facet='lemmas';
              see https://developer.rosette.com/features-and-functions for
              complete documentation)
    """
    parameters = DocumentParameters()
    if uri:
        parameters['contentUri'] = content
    else:
        parameters['content'] = content
    parameters['language'] = language
    try:
        adm = methodcaller(endpoint, parameters, **kwargs)(api)
        return adm
    except RosetteException as e:
        print(f'[{e.status}]: {e.message}', file=sys.stderr)
Example #10
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)
    embeddings_data = "Cambridge, Massachusetts"
    params = DocumentParameters()
    params["content"] = embeddings_data
    return api.text_embedding(params)
Example #11
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)
def runRosette(text, key, alt_url='https://api.rosette.com/rest/v1/'):
    """ Run the example """
    # Create default file to read from
    temp_file = tempfile.NamedTemporaryFile(suffix=".html")
    sentiment_file_data = "<html><head><title></title></head><body><p>" + text + "</p></body></html>"
    message = sentiment_file_data
    temp_file.write(
        message if isinstance(message, bytes) else message.encode())
    temp_file.seek(0)

    # Create an API instance
    api = API(user_key=key, service_url=alt_url)

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

    # Use an HTML file to load data instead of a string
    #params.load_document_file(temp_file.name)
    try:
        result = api.sentiment(params)
        #print (result)
    except RosetteException as exception:
        print(exception)
    finally:
        # Clean up the file
        temp_file.close()

    return result
Example #13
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    tokens_data = "北京大学生物系主任办公室内部会议"
    params = DocumentParameters()
    params["content"] = tokens_data
    return api.tokens(params)
Example #14
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    morphology_compound_components_data = "Rechtsschutzversicherungsgesellschaften"
    params = DocumentParameters()
    params["content"] = morphology_compound_components_data
    return api.morphology(params, MorphologyOutput.COMPOUND_COMPONENTS)
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    morphology_han_readings_data = "北京大学生物系主任办公室内部会议"
    params = DocumentParameters()
    params["content"] = morphology_han_readings_data
    return api.morphology(params, MorphologyOutput.HAN_READINGS)
Example #16
0
def vectorize_text(text, key, url='https://api.rosette.com/rest/v1/'):
    """
    Return the vector representation of the input text (as a list of floats).
    """
    api = API(user_key=key, service_url=url)
    params = DocumentParameters()
    params["content"] = text
    return api.text_embedding(params)["embedding"]
Example #17
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)
    entities_text_data = "Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS"
    params = DocumentParameters()
    params["content"] = entities_text_data
    params["genre"] = "social-media"
    return api.entities(params)
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    morphology_lemmas_data = "The fact is that the geese just went back to get a rest and I'm not banking on their return soon"
    params = DocumentParameters()
    params["content"] = morphology_lemmas_data
    return api.morphology(params, MorphologyOutput.LEMMAS)
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    morphology_complete_data = "The quick brown fox jumped over the lazy dog. Yes he did."
    params = DocumentParameters()
    params["content"] = morphology_complete_data
    return api.morphology(params)
Example #20
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)
    relationships_text_data = "The Ghostbusters movie was filmed in Boston."
    params = DocumentParameters()
    params["content"] = relationships_text_data
    api.setOption('accuracyMode', 'PRECISION')
    return api.relationships(params)
Example #21
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    sentences_data = "This land is your land. This land is my land\nFrom California to the New York island;\nFrom the red wood forest to the Gulf Stream waters\n\nThis land was made for you and Me.\n\nAs I was walking that ribbon of highway,\nI saw above me that endless skyway:\nI saw below me that golden valley:\nThis land was made for you and me."
    params = DocumentParameters()
    params["content"] = sentences_data

    return api.sentences(params)
Example #22
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    language_data = "Por favor Señorita, says the man."
    params = DocumentParameters()
    params["content"] = language_data
    api.setCustomHeaders("X-RosetteAPI-App", "python-app")
    return api.language(params)
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    entities_linked_text_data = "Last month director Paul Feig announced the movie will have an all-star female cast including Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon."
    params = DocumentParameters()
    params["content"] = entities_linked_text_data
    params["genre"] = "social-media"
    # This syntax is deprecated, call api.entities(params)
    return api.entities(params, True)
Example #24
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)
Example #25
0
 def call_api(self, content):
     params = DocumentParameters()
     params["content"] = content[0:49999]  # truncate to first 50000 chars
     res = {}
     try:
         res = self.client.entities(params)
     except RosetteException as exception:
         print(exception)
         res = {"rosette_exception": str(exception)}
     return json.dumps(res, sort_keys=True, indent=4)
Example #26
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)
Example #27
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create default file to read from
    f = tempfile.NamedTemporaryFile(suffix=".html")
    message = "<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>"
    f.write(message)
    f.seek(0)

    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    params = DocumentParameters()

    # Use an HTML file to load data instead of a string
    params.load_document_file(f.name)
    result = api.sentiment(params)

    # Clean up the file
    f.close()

    return result
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    morphology_han_readings_data = "北京大学生物系主任办公室内部会议"
    params = DocumentParameters()
    params["content"] = morphology_han_readings_data
    try:
        return api.morphology(params, api.morphology_output['HAN_READINGS'])
    except RosetteException as e:
        print(e)
Example #29
0
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
    """ Run the example """
    # Create an API instance
    api = API(user_key=key, service_url=alt_url)
    embeddings_data = "Cambridge, Massachusetts"
    params = DocumentParameters()
    params["content"] = embeddings_data
    try:
        return api.text_embedding(params)
    except RosetteException as exception:
        print(exception)
Example #30
0
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
    """ Run the example """
    syntax_dependencies_data = "Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday."
    params = DocumentParameters()
    params["content"] = syntax_dependencies_data
    # Create an API instance
    api = API(user_key=key, service_url=alt_url)
    try:
        return api.syntax_dependencies(params)
    except RosetteException as exception:
        print(exception)
Example #31
0
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
    """ Run the example """
    # Create an API instance
    api = API(user_key=key, service_url=alt_url)
    relationships_text_data = "FLIR Systems is headquartered in Oregon and produces thermal imaging, night vision, and infrared cameras and sensor systems.  According to the SEC’s order instituting a settled administrative proceeding, FLIR entered into a multi-million dollar contract to provide thermal binoculars to the Saudi government in November 2008.  Timms and Ramahi were the primary sales employees responsible for the contract, and also were involved in negotiations to sell FLIR’s security cameras to the same government officials.  At the time, Timms was the head of FLIR’s Middle East office in Dubai."
    params = DocumentParameters()
    params["content"] = relationships_text_data
    try:
        return api.relationships(params)
    except RosetteException as exception:
        print(exception)
Example #32
0
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
    # Create an API instance
    api = API(user_key=key, service_url=altUrl)

    morphology_parts_of_speech_data = "The fact is that the geese just went back to get a rest and I'm not banking on their return soon"
    params = DocumentParameters()
    params["content"] = morphology_parts_of_speech_data
    try:
        return api.morphology(params, MorphologyOutput.PARTS_OF_SPEECH)
    except RosetteException as e:
        print(e)
Example #33
0
        <p>This is a simple sample HTML file to demonstrate the option of loading data from a file.</p>

    </body>
</html>"""
f.write(message)
f.seek(0)

# Collect arguments
parser = argparse.ArgumentParser(description="Get the sentiment of the text in a local file")
parser.add_argument("--key", required=True, help="Rosette API key")
parser.add_argument("--service_url", nargs="?", help="Optional user service URL")
parser.add_argument("--file", nargs="?", default=f.name, help="Optional input file 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 an HTML file to load data instead of a string
params.load_document_file(args.file)
result = api.sentiment(params)

# Clean up the file
f.close()

pprint.pprint(result)