def error(subscription_key): """Error. This triggers a bad request and shows how to read the error response. """ # Breaking the subscription key on purpose client = AutoSuggestSearchAPI( CognitiveServicesCredentials(subscription_key + "1")) try: suggestions = client.auto_suggest(query="Satya Nadella", 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 autosuggest_lookup(subscription_key): """AutoSuggestLookup. This will look up a single query (Xbox) and print out name and url for first web result. """ client = AutoSuggestSearchAPI( CognitiveServicesCredentials(subscription_key)) try: suggestions = client.auto_suggest( query="Satya Nadella") # type: Suggestions if suggestions.suggestion_groups: print("Searched for \"Satya Nadella\" and found suggestions:") suggestion_group = suggestions.suggestion_groups[ 0] # type: SuggestionsSuggestionGroup for suggestion in suggestion_group.search_suggestions: # type: SearchAction print("....................................") print(suggestion.query) print(suggestion.display_text) print(suggestion.url) print(suggestion.search_kind) else: print("Didn't see any suggestion..") except Exception as err: print("Encountered exception. {}".format(err))
def getAutosuggestions(firstTag): subkey= os.getenv('AUTOSUGGESTIONS_KEY') client = AutoSuggestSearchAPI( CognitiveServicesCredentials(subkey)) suggestions = client.auto_suggest( query=firstTag) # type: Suggestions if suggestions.suggestion_groups: suggestion_group = suggestions.suggestion_groups[0] # type: SuggestionsSuggestionGroup for suggestion in suggestion_group.search_suggestions: # type: SearchAction print("suggestion:", suggestion.query) return suggestion.query
from msrest.authentication import CognitiveServicesCredentials import json, os, sys ''' Microsoft Azure Cognitive Services Bing Autosuggest - Get Search Suggestions This script requires the Cognitive Services Bing Autosuggest module: python -m pip install azure-cognitiveservices-search_autosuggest This script runs under Python 3.4 or later. ''' subscription_key = os.environ['AUTOSUGGEST_SUBSCRIPTION_KEY'] if not subscription_key: raise Exception('Please set/export the environment variable: {}'.format( subscription_key)) # Instantiate a Bing Autosuggest client client = AutoSuggestSearchAPI(CognitiveServicesCredentials(subscription_key)) # Returns from the Suggestions class result = client.auto_suggest('sail') # Access all suggestions suggestions = result.suggestion_groups[0] # print results for suggestion in suggestions.search_suggestions: print(suggestion.query) print(suggestion.display_text)
Install the Cognitive Services Bing Autosuggest SDK module: python -m pip install azure-cognitiveservices-search_autosuggest Use Python 3.4+ ''' subscription_key = "PASTE_YOUR_AUTO_SUGGEST_SUBSCRIPTION_KEY_HERE" endpoint = "PASTE_YOUR_AUTO_SUGGEST_ENDPOINT_HERE" ''' AUTHENTICATE Create an Autosuggest client. ''' credentials = CognitiveServicesCredentials(subscription_key) autosuggest_client = AutoSuggestSearchAPI(CognitiveServicesCredentials(subscription_key), endpoint + 'bing/v7.0') ''' AUTOSUGGEST This example uses a query term to search for autocompletion suggestions for the term. ''' # Returns from the Suggestions class result = autosuggest_client.auto_suggest('sail') # Access all suggestions suggestions = result.suggestion_groups[0] # print results for suggestion in suggestions.search_suggestions: print(suggestion.query) print(suggestion.display_text)
#autosuggest from azure.cognitiveservices.search.autosuggest import AutoSuggestSearchAPI from azure.cognitiveservices.search.autosuggest.models import ( Suggestions, SuggestionsSuggestionGroup, SearchAction, ErrorResponseException) from msrest.authentication import CognitiveServicesCredentials subkey = '' client = AutoSuggestSearchAPI(CognitiveServicesCredentials(subkey)) suggestions = client.auto_suggest(query="convolutional") # type: Suggestions if suggestions.suggestion_groups: suggestion_group = suggestions.suggestion_groups[ 0] # type: SuggestionsSuggestionGroup #print(suggestion_group.search_suggestions[1].display_text) for suggestion in suggestion_group.search_suggestions: # type: SearchAction print(suggestion.query) break