Ejemplo n.º 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
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
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)