def error(subscription_key):
    """Error.

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

    client = EntitySearchClient(
        endpoint="https://api.cognitive.microsoft.com",
        credentials=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.")
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 = EntitySearchClient(
        endpoint=ENDPOINT,
        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))
def handling_disambiguation(subscription_key):
    """HandlingDisambiguation.

    "This will handle disambiguation results for an ambiguous query (William Gates)".
    """
    client = EntitySearchClient(
        endpoint="https://api.cognitive.microsoft.com",
        credentials=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))
def restaurant_lookup(subscription_key):
    """RestaurantLookup.

    This will look up a single restaurant (john howie bellevue) and print out its phone number.
    """
    client = EntitySearchClient(
        endpoint="https://api.cognitive.microsoft.com",
        credentials=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 multiple_restaurant_lookup(subscription_key):
    """MultipleRestaurantLookup.

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

    client = EntitySearchClient(
        endpoint="https://api.cognitive.microsoft.com",
        credentials=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))
Exemplo n.º 6
0
    def test_search(self):
        raise unittest.SkipTest("Skipping test_search")
        query = 'seahawks'
        market = 'en-us'

        credentials = CognitiveServicesCredentials(
            self.settings.CS_SUBSCRIPTION_KEY)
        entity_search_api = EntitySearchClient(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
Exemplo n.º 7
0
from utils.config import get_config
from azure.cognitiveservices.search.entitysearch import EntitySearchClient
from msrest.authentication import CognitiveServicesCredentials
from utils.format_tools import clean_url
from lib.model import Company
from utils.exceptions import NotFoundException

subscription_key = get_config('bing.subscription_key')
endpoint = get_config('bing.endpoint')
credentials = CognitiveServicesCredentials(subscription_key)
client = EntitySearchClient(endpoint=endpoint, credentials=credentials)


def __bing_find(name: str) -> Company:
    entity_data = client.entities.search(query=name)
    if entity_data.entities is not None:
        entity = entity_data.entities.value[0]
        company = Company({
            'name': entity.name,
            'domain': clean_url(entity.url),
            'long_description': entity.description
        })
        return company
    else:
        raise NotFoundException(name)
Exemplo n.º 8
0
Install the Entity Search SDK from a command prompt or IDE terminal:
  python -m pip install azure-cognitiveservices-search-entitysearch
'''

subscription_key = 'PASTE_YOUR_BING_SEARCH_SUBSCRIPTION_KEY_HERE'
endpoint = 'PASTE_YOUR_BING_SEARCH_ENDPOINT_HERE'

# Search queries
person_query = 'Allan Turing'
restaurant_query = 'Shiro\'s Sushi Seattle'
'''
AUTHENTICATE
Create an Entity Search client.
'''
entity_search_client = EntitySearchClient(
    endpoint, CognitiveServicesCredentials(subscription_key))
'''
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'
        ]
Exemplo n.º 9
0
key = "8d4febee8bf849a2b9a97797b422223e"
endpoint = "https://azuretext1.cognitiveservices.azure.com/"

from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsApiKeyCredential
from azure.cognitiveservices.search.entitysearch import EntitySearchClient
from azure.cognitiveservices.search.entitysearch.models import Place, ErrorResponseException
from msrest.authentication import CognitiveServicesCredentials

import docx2txt

#setup for entity search
subscription_key = "d8c84aab3fdb4dbcaa7b702f37fe4578"
endpoint_search = "https://bingysearch.cognitiveservices.azure.com"
client_bing = EntitySearchClient(endpoint=endpoint_search, credentials=CognitiveServicesCredentials(subscription_key))

my_text = docx2txt.process("050B.docx")

def authenticate_client():
    ta_credential = TextAnalyticsApiKeyCredential(key)
    text_analytics_client = TextAnalyticsClient(
            endpoint=endpoint, credential=ta_credential)
    return text_analytics_client

client = authenticate_client()


def sentiment_analysis_example(client):
    document = [my_text]
    response = client.analyze_sentiment(inputs=document)[0]
    print("Document Sentiment: {}".format(response.sentiment))
    print("Overall scores: positive={0:.3f}; neutral={1:.3f}; negative={2:.3f} \n".format(