def run(key, altUrl='https://api.rosette.com/rest/v1/'): # Create an API instance api = API(user_key=key, service_url=altUrl) params = NameMatchingParameters() params["name1"] = {"text": "Michael Jackson", "language": "eng", "entityType": "PERSON"} params["name2"] = {"text": "迈克尔·杰克逊", "entityType": "PERSON"} return api.matched_name(params)
def test_just_text(): 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") 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." result = api.entities(content) # Check that it work for entities assert result == expected_result # Check that it throws the correct error for matched-name try: api.matched_name(content) assert False except RosetteException as e: assert e.status == "incompatible" # Check that it throws the correct error for translated-name try: api.translated_name(content) assert False except RosetteException as e: assert e.status == "incompatible"
def wikipedia_summary(query, key=user_key, alt_url=ros_url): # Create an API instance api = API(user_key=key, service_url=alt_url) params = NameMatchingParameters() params['name1'] = query max = ['error', 0] for x in wikipedia.search(query): params["name2"] = x val = json.loads(json.dumps(api.matched_name(params), indent=2, ensure_ascii=False))['result']['score'] > max[1] if val > max[1]: max = [x, val] if max[0] != 'error': return wikipedia.summary(max[0]) else: raise Exception("No wiki articles found")
def proper_key_words(key_word, key=user_key, alt_url=ros_url): # Create an API instance api = API(user_key=key, service_url=alt_url) params = NameMatchingParameters() params['name1'] = key_word max = ['error', 0] for x in wikipedia.search(key_word): params["name2"] = x val = json.loads(json.dumps(api.matched_name(params), indent=2, ensure_ascii=False))['result']['score'] > max[1] if val > max[1]: max = [x, val] params = DocumentParameters() params['content'] = wikipedia.summary(max[0]) json_obj = json.loads(json.dumps(api.entities(params), indent=2, ensure_ascii=False, sort_keys=True)) return parse_with_queue(json_obj, key_word)
# -*- coding: utf-8 -*- """ Example code to call Rosette API to get match score (similarity) of two names. """ import argparse import pprint from rosette.api import API, NameMatchingParameters parser = argparse.ArgumentParser(description="Get the similarity score of two names") parser.add_argument("--key", required=True, help="Rosette API key") parser.add_argument("--service_url", nargs="?", help="Optional user service URL") 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 = NameMatchingParameters() params["name1"] = {"text": "Michael Jackson", "language": "eng", "entityType": "PERSON"} params["name2"] = {"text": "迈克尔·杰克逊", "entityType": "PERSON"} result = api.matched_name(params) pprint.pprint(result)