예제 #1
0
def dominant_entity_lookup(subscription_key):
    """DominantEntityLookup.

    This will look up a single entity (Satya Nadella) and print out a short description about them.
    """
    client = EntitySearchAPI(
        endpoint="https://api.cognitive.microsoft.com",
        credentials=CognitiveServicesCredentials(subscription_key))

    try:
        entity_data = client.entities.search(query="satya nadella")

        if entity_data.entities.value:
            # find the entity that represents the dominant one

            main_entities = [
                entity for entity in entity_data.entities.value
                if entity.entity_presentation_info.entity_scenario ==
                "DominantEntity"
            ]

            if main_entities:
                print(
                    'Searched for "Satya Nadella" and found a dominant entity with this description:'
                )
                print(main_entities[0].description)
            else:
                print("Couldn't find main entity Satya Nadella!")

        else:
            print("Didn't see any data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
예제 #2
0
def facts():

    subscription_key = '600ab55597bb4bf08c1878e935ead82c'
    from azure.cognitiveservices.search.entitysearch import EntitySearchAPI
    from azure.cognitiveservices.search.entitysearch.models import Place, ErrorResponseException
    from msrest.authentication import CognitiveServicesCredentials

    client = EntitySearchAPI(CognitiveServicesCredentials(subscription_key))
    save_speech('answer')
    speech = speech2text()
    try:

        entity_data = client.entities.search(query=speech)
        if entity_data.entities.value:

            main_entities = [
                entity for entity in entity_data.entities.value
                if entity.entity_presentation_info.entity_scenario ==
                "DominantEntity"
            ]

            if main_entities:
                main_string = main_entities[0].description
                # sent_token = main_string.split('.')
                #
                # for sente in sent_token:
                #     modular_speech(sente)
                #     if keyboard.is_pressed('d'):
                #         break
                modular_speech(main_string)

    except AttributeError:
        save_speech('unknownError')
예제 #3
0
파일: video.py 프로젝트: tarsbase/MSNewsAR
def getVideoFromAzure(title):
    entity_client = EntitySearchAPI(
        CognitiveServicesCredentials(subscription_key_entity))
    video_client = VideoSearchAPI(
        CognitiveServicesCredentials(subscription_key_video))

    try:
        entity_data = entity_client.entities.search(query=title)

        if entity_data.entities is not None:
            main_entities = [
                entity for entity in entity_data.entities.value
                if entity.entity_presentation_info.entity_scenario ==
                "DominantEntity"
            ]
            if len(main_entities) != 0:
                video_result = video_client.videos.search(
                    query=' '.join(str(ent) for ent in main_entities),
                    safe_search=SafeSearch.strict)
                if video_result is not None and len(video_result.value) != 0:
                    soup = BeautifulSoup(video_result.value[0].embed_html,
                                         'html.parser')
                    return soup.find("iframe")["src"]
        else:
            video_result = video_client.videos.search(
                query=title[:45], safe_search=SafeSearch.strict)
            if video_result is not None and len(video_result.value) != 0:
                soup = BeautifulSoup(video_result.value[0].embed_html,
                                     'html.parser')
                return soup.find("iframe")["src"]

    except Exception as err:
        print("Encountered exception. {}".format(err))

    return None
예제 #4
0
def dominant_entity_lookup(subscription_key):
    """DominantEntityLookup.

    This will look up a single entity (tom cruise) and print out a short description about them.
    """
    client = EntitySearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        entity_data = client.entities.search(query="tom cruise")

        if entity_data.entities.value:
            # find the entity that represents the dominant one

            main_entities = [entity for entity in entity_data.entities.value
                             if entity.entity_presentation_info.entity_scenario == "DominantEntity"]

            if main_entities:
                print('Searched for "Tom Cruise" and found a dominant entity with this description:')
                print(main_entities[0].description)
            else:
                print("Couldn't find main entity tom cruise!")
        
        else:
            print("Didn't see any data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
예제 #5
0
def error(subscription_key):
    """Error.

    This triggers a bad request and shows how to read the error response.
    """

    client = EntitySearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        entity_data = client.entities.search(query="tom cruise",
                                             market="no-ty")
    except ErrorResponseException as err:
        # The status code of the error should be a good indication of what occurred. However, if you'd like more details, you can dig into the response.
        # Please note that depending on the type of error, the response schema might be different, so you aren't guaranteed a specific error response schema.

        print("Exception occurred, status code {} with reason {}.\n".format(
            err.response.status_code, err))

        # if you'd like more descriptive information (if available)
        if err.error.errors:
            print("This is the errors I have:")
            for error in err.error.errors:
                print(
                    "Parameter \"{}\" has an invalid value \"{}\". SubCode is \"{}\". Detailed message is \"{}\""
                    .format(error.parameter, error.value, error.sub_code,
                            error.message))
        else:
            print("There was no details on the error.")
예제 #6
0
def handling_disambiguation(subscription_key):
    """HandlingDisambiguation.

    "This will handle disambiguation results for an ambiguous query (William Gates)".
    """
    client = EntitySearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        entity_data = client.entities.search(query="william gates")

        if entity_data.entities.value:
            # find the entity that represents the dominant one

            main_entities = [
                entity for entity in entity_data.entities.value
                if entity.entity_presentation_info.entity_scenario ==
                "DominantEntity"
            ]

            disambig_entities = [
                entity for entity in entity_data.entities.value
                if entity.entity_presentation_info.entity_scenario ==
                "DisambiguationItem"
            ]

            if main_entities:
                main_entity = main_entities[0]
                type_hint = main_entity.entity_presentation_info.entity_type_display_hint

                print(
                    'Searched for "William Gates" and found a dominant entity {}with this description:'
                    .format('"with type hint "{}" '.
                            format(type_hint) if type_hint else ''))
                print(main_entity.description)
            else:
                print(
                    "Couldn't find a reliable dominant entity for William Gates!"
                )

            if disambig_entities:
                print(
                    "\nThis query is pretty ambiguous and can be referring to multiple things. Did you mean one of these:"
                )
                suggestions = []
                for disambig_entity in disambig_entities:
                    suggestions.append("{} the {}".format(
                        disambig_entity.name, disambig_entity.
                        entity_presentation_info.entity_type_display_hint))
                print(", or ".join(suggestions))
            else:
                print(
                    "We didn't find any disambiguation items for William Gates, so we must be certain what you're talking about!"
                )

        else:
            print("Didn't see any data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
예제 #7
0
def multiple_restaurant_lookup(subscription_key):
    """MultipleRestaurantLookup.

    This will look up a list of restaurants (seattle restaurants) and present their names and phone numbers.
    """

    client = EntitySearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        restaurants = client.entities.search(query="seattle restaurants")

        if restaurants.places.value:

            # get all the list items that relate to this query
            list_items = [
                entity for entity in restaurants.places.value if
                entity.entity_presentation_info.entity_scenario == "ListItem"
            ]

            if list_items:

                suggestions = []
                for place in list_items:
                    # Pythonic approach : EAFP "Easier to ask for forgiveness than permission"
                    # see https://docs.python.org/3/glossary.html
                    try:
                        suggestions.append("{} ({})".format(
                            place.name, place.telephone))
                    except AttributeError:
                        print(
                            "Unexpectedly found something that isn\'t a place named '{}'",
                            place.name)

                print("Ok, we found these places: ")
                print(", ".join(suggestions))

            else:
                print(
                    "Couldn't find any relevant results for \"seattle restaurants\""
                )

        else:
            print("Didn't see any data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
예제 #8
0
def restaurant_lookup(subscription_key):
    """RestaurantLookup.

    This will look up a single restaurant (john howie bellevue) and print out its phone number.
    """
    client = EntitySearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        entity_data = client.entities.search(query="john howie bellevue")

        if entity_data.places.value:

            restaurant = entity_data.places.value[0]

            # Some local entities will be places, others won't be. Depending on what class contains the data you want, you can check
            # using isinstance one of the class, or try to get the attribute and handle the exception (EAFP principle).
            # The recommended Python way is usually EAFP (see https://docs.python.org/3/glossary.html)
            # In this case, the item being returned is technically a Restaurant, but the Place schema has the data we want (telephone)

            # Pythonic approach : EAFP "Easier to ask for forgiveness than permission"
            try:
                telephone = restaurant.telephone
                print(
                    'Searched for "John Howie Bellevue" and found a restaurant with this phone number:'
                )
                print(telephone)
            except AttributeError:
                print("Couldn't find a place!")

            # More cross language approach
            if isinstance(restaurant, Place):
                print(
                    'Searched for "John Howie Bellevue" and found a restaurant with this phone number:'
                )
                print(restaurant.telephone)
            else:
                print("Couldn't find a place!")

        else:
            print("Didn't see any data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
    def test_search(self):
        query = 'seahawks'
        market = 'en-us'

        credentials = CognitiveServicesCredentials(
            self.settings.CS_SUBSCRIPTION_KEY)
        entity_search_api = EntitySearchAPI(credentials)
        response = entity_search_api.entities.search(query=query,
                                                     market=market)

        assert response is not None
        assert response._type is not None

        assert response.query_context is not None
        assert response.query_context.original_query == query

        assert response.entities is not None
        assert response.entities.value is not None
        assert len(response.entities.value) == 1
        assert response.entities.value[0].contractual_rules is not None
  python -m pip install azure-cognitiveservices-search-entitysearch
'''

# Add your Cognitive Services subscription key and endpoint to your environment variables.
subscription_key = os.environ['COGNITIVE_SERVICES_SUBSCRIPTION_KEY']
endpoint = os.environ['COGNITIVE_SERVICES_ENDPOINT']

# Search queries
person_query = 'Allan Turing'
restaurant_query = 'Shiro\'s Sushi Seattle'

'''
AUTHENTICATE
Create an Entity Search client.
'''
entity_search_client = EntitySearchAPI(CognitiveServicesCredentials(subscription_key), endpoint + '/bing/v7.0/')

'''
Bing Entity Search
Takes an entity (a famous person) and prints a short description about them.
'''
# Search for the dominant entity
try:
  entity_data = entity_search_client.entities.search(query=person_query)

  if entity_data.entities.value:
    # Find the entity that represents the dominant one
    main_entities = [entity for entity in entity_data.entities.value
                      if entity.entity_presentation_info.entity_scenario == 'DominantEntity']

    if main_entities: