Snabbstart: Använda python-REST API för att anropa tjänsten Textanalys kognitivtQuickstart: Using the Python REST API to call the Text Analytics Cognitive Service

Använd den här snabb starten för att börja analysera språk med Textanalys REST API och python.Use this quickstart to begin analyzing language with the Text Analytics REST API and Python. Den här artikeln visar hur du kan identifiera språk, analysera sentiment, extrahera nyckel fraseroch identifiera länkade entiteter.This article shows you how to detect language, analyze sentiment, extract key phrases, and identify linked entities.

Se API-definitionerna för teknisk dokumentation för API:erna.Refer to the API definitions for technical documentation for the APIs.

FörutsättningarPrerequisites

  • Python 3.xPython 3.x

  • Biblioteket python-begärandenThe Python requests library

    Du kan installera biblioteket med det här kommandot:You can install the library with this command:

    pip install --upgrade requests
    

En nyckel och slut punkt för en Textanalys resurs.A key and endpoint for a Text Analytics resource. Azure-Cognitive Services representeras av Azure-resurser som du prenumererar på.Azure Cognitive Services are represented by Azure resources that you subscribe to. Skapa en resurs för Textanalys med hjälp av Azure Portal eller Azure CLI på den lokala datorn.Create a resource for Text Analytics using the Azure portal or Azure CLI on your local machine. Du kan också:You can also:

Skapa ett nytt Python-programCreate a new Python application

Skapa ett nytt python-program i din favorit redigerare eller IDE.Create a new Python application in your favorite editor or IDE. Lägg till följande importer i filen.Add the following imports to your file.

import requests
# pprint is used to format the JSON response
from pprint import pprint

Skapa variabler för resursens Azure-slut punkt och prenumerations nyckel.Create variables for your resource's Azure endpoint and subscription key. Hämta de här värdena från miljövariablerna TEXT_ANALYTICS_SUBSCRIPTION_KEY och TEXT_ANALYTICS_ENDPOINT.Obtain these values from the environment variables TEXT_ANALYTICS_SUBSCRIPTION_KEY and TEXT_ANALYTICS_ENDPOINT. Om du har skapat dessa miljövariabler när du började redigera programmet måste du stänga och öppna den redigerare, IDE eller Shell som du använder för att få åtkomst till variablerna.If you created these environment variables after you began editing the application, you will need to close and reopen the editor, IDE, or shell you are using to access the variables.

import os

key_var_name = 'TEXT_ANALYTICS_SUBSCRIPTION_KEY'
if not key_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(key_var_name))
subscription_key = os.environ[key_var_name]

endpoint_var_name = 'TEXT_ANALYTICS_ENDPOINT'
if not endpoint_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(endpoint_var_name))
endpoint = os.environ[endpoint_var_name]

I följande avsnitt beskrivs hur du anropar var och en av API-funktionerna.The following sections describe how to call each of the API's features.

Identifiera språkDetect languages

Lägg /text/analytics/v2.1/languages till i textanalys bas slut punkten för att skapa URL: en för språk identifiering.Append /text/analytics/v2.1/languages to the Text Analytics base endpoint to form the language detection URL. Exempel: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v2.1/languagesFor example: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v2.1/languages

language_api_url = endpoint + "/text/analytics/v2.1/languages"

Nytto lasten till API: et består av en documentslista över, som är tupler id som innehåller text ett och ett-attribut.The payload to the API consists of a list of documents, which are tuples containing an id and a text attribute. Attributet lagrar texten som ska analyseras id och kan vara vilket värde som helst. textThe text attribute stores the text to be analyzed, and the id can be any value.

documents = {"documents": [
    {"id": "1", "text": "This is a document written in English."},
    {"id": "2", "text": "Este es un document escrito en Español."},
    {"id": "3", "text": "这是一个用中文写的文件"}
]}

Använd begär ande biblioteket för att skicka dokumenten till API: et.Use the Requests library to send the documents to the API. Lägg till din prenumerations nyckel Ocp-Apim-Subscription-Key i rubriken och skicka begäran med. requests.post()Add your subscription key to the Ocp-Apim-Subscription-Key header, and send the request with requests.post().

headers = {"Ocp-Apim-Subscription-Key": subscription_key}
response = requests.post(language_api_url, headers=headers, json=documents)
languages = response.json()
pprint(languages)

OutputOutput

{
"documents":[
    {
        "detectedLanguages":[
        {
            "iso6391Name":"en",
            "name":"English",
            "score":1.0
        }
        ],
        "id":"1"
    },
    {
        "detectedLanguages":[
        {
            "iso6391Name":"es",
            "name":"Spanish",
            "score":1.0
        }
        ],
        "id":"2"
    },
    {
        "detectedLanguages":[
        {
            "iso6391Name":"zh_chs",
            "name":"Chinese_Simplified",
            "score":1.0
        }
        ],
        "id":"3"
    }
],
"errors":[]
}

Analysera sentimentAnalyze sentiment

Om du vill identifiera sentiment (som sträcker sig mellan positivt eller negativt) i en uppsättning dokument /text/analytics/v2.1/sentiment lägger du till textanalys bas slut punkten för att skapa URL: en för språk identifiering.To detect the sentiment (which ranges between positive or negative) of a set of documents, append /text/analytics/v2.1/sentiment to the Text Analytics base endpoint to form the language detection URL. Exempel: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v2.1/sentimentFor example: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v2.1/sentiment

sentiment_url = endpoint + "/text/analytics/v2.1/sentiment"

Som med språk identifierings exemplet skapar du en ord lista med documents en nyckel som består av en lista med dokument.As with the language detection example, create a dictionary with a documents key that consists of a list of documents. Varje dokument är en tuppel som består av id, text som ska analyseras och textens language.Each document is a tuple consisting of the id, the text to be analyzed and the language of the text.

documents = {"documents": [
    {"id": "1", "language": "en",
        "text": "I had a wonderful experience! The rooms were wonderful and the staff was helpful."},
    {"id": "2", "language": "en",
        "text": "I had a terrible time at the hotel. The staff was rude and the food was awful."},
    {"id": "3", "language": "es",
        "text": "Los caminos que llevan hasta Monte Rainier son espectaculares y hermosos."},
    {"id": "4", "language": "es",
     "text": "La carretera estaba atascada. Había mucho tráfico el día de ayer."}
]}

Använd begär ande biblioteket för att skicka dokumenten till API: et.Use the Requests library to send the documents to the API. Lägg till din prenumerations nyckel Ocp-Apim-Subscription-Key i rubriken och skicka begäran med. requests.post()Add your subscription key to the Ocp-Apim-Subscription-Key header, and send the request with requests.post().

headers = {"Ocp-Apim-Subscription-Key": subscription_key}
response = requests.post(sentiment_url, headers=headers, json=documents)
sentiments = response.json()
pprint(sentiments)

OutputOutput

Sentiment-poängen för ett dokument är mellan 0,0 och 1,0, med en högre poäng som visar en mer positiv sentiment.The sentiment score for a document is between 0.0 and 1.0, with a higher score indicating a more positive sentiment.

{
  "documents":[
    {
      "id":"1",
      "score":0.9708490371704102
    },
    {
      "id":"2",
      "score":0.0019068121910095215
    },
    {
      "id":"3",
      "score":0.7456425428390503
    },
    {
      "id":"4",
      "score":0.334433376789093
    }
  ],
  "errors":[]
}

Extrahera nyckelfraserExtract key phrases

Extrahera nyckel fraserna från en uppsättning dokument genom att lägga /text/analytics/v2.1/keyPhrases till i textanalys bas slut punkten för att skapa URL: en för språk identifiering.To extract the key phrases from a set of documents, append /text/analytics/v2.1/keyPhrases to the Text Analytics base endpoint to form the language detection URL. Exempel: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v2.1/keyPhrasesFor example: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v2.1/keyPhrases

keyphrase_url = endpoint + "/text/analytics/v2.1/keyphrases"

Den här samlingen av dokument är samma som används för analys exemplet sentiment.This collection of documents is the same used for the sentiment analysis example.

documents = {"documents": [
    {"id": "1", "language": "en",
        "text": "I had a wonderful experience! The rooms were wonderful and the staff was helpful."},
    {"id": "2", "language": "en",
        "text": "I had a terrible time at the hotel. The staff was rude and the food was awful."},
    {"id": "3", "language": "es",
        "text": "Los caminos que llevan hasta Monte Rainier son espectaculares y hermosos."},
    {"id": "4", "language": "es",
     "text": "La carretera estaba atascada. Había mucho tráfico el día de ayer."}
]}

Använd begär ande biblioteket för att skicka dokumenten till API: et.Use the Requests library to send the documents to the API. Lägg till din prenumerations nyckel Ocp-Apim-Subscription-Key i rubriken och skicka begäran med. requests.post()Add your subscription key to the Ocp-Apim-Subscription-Key header, and send the request with requests.post().

headers = {"Ocp-Apim-Subscription-Key": subscription_key}
response = requests.post(keyphrase_url, headers=headers, json=documents)
key_phrases = response.json()
pprint(key_phrases)

OutputOutput

{
  "documents":[
    {
      "keyPhrases":[
        "wonderful experience",
        "staff",
        "rooms"
      ],
      "id":"1"
    },
    {
      "keyPhrases":[
        "food",
        "terrible time",
        "hotel",
        "staff"
      ],
      "id":"2"
    },
    {
      "keyPhrases":[
        "Monte Rainier",
        "caminos"
      ],
      "id":"3"
    },
    {
      "keyPhrases":[
        "carretera",
        "tráfico",
        "día"
      ],
      "id":"4"
    }
  ],
  "errors":[]
}

Identifiera entiteterIdentify Entities

Identifiera välkända entiteter (personer, platser och saker) i text dokument genom att lägga /text/analytics/v2.1/entities till i textanalys bas slut punkten för att bilda URL: en för språk identifiering.To identify well-known entities (people, places, and things) in text documents, append /text/analytics/v2.1/entities to the Text Analytics base endpoint to form the language detection URL. Exempel: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v2.1/entitiesFor example: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v2.1/entities

entities_url = endpoint + "/text/analytics/v2.1/entities"

Skapa en samling dokument, som i de föregående exemplen.Create a collection of documents, like in the previous examples.

documents = {"documents": [
    {"id": "1", "text": "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800."}
]}

Använd begär ande biblioteket för att skicka dokumenten till API: et.Use the Requests library to send the documents to the API. Lägg till din prenumerations nyckel Ocp-Apim-Subscription-Key i rubriken och skicka begäran med. requests.post()Add your subscription key to the Ocp-Apim-Subscription-Key header, and send the request with requests.post().

headers = {"Ocp-Apim-Subscription-Key": subscription_key}
response = requests.post(entities_url, headers=headers, json=documents)
entities = response.json()
pprint(entities)

OutputOutput

{
   "documents" : [
      {
         "id" : "1",
         "entities" : [
            {
               "name" : "Microsoft",
               "matches" : [
                  {
                     "wikipediaScore" : 0.49897989655674446,
                     "entityTypeScore" : 1.0,
                     "text" : "Microsoft",
                     "offset" : 0,
                     "length" : 9
                  }
               ],
               "wikipediaLanguage" : "en",
               "wikipediaId" : "Microsoft",
               "wikipediaUrl" : "https://en.wikipedia.org/wiki/Microsoft",
               "bingId" : "a093e9b9-90f5-a3d5-c4b8-5855e1b01f85",
               "type" : "Organization"
            },
            {
               "name" : "Bill Gates",
               "matches" : [
                  {
                     "wikipediaScore" : 0.58357497243368983,
                     "entityTypeScore" : 0.999847412109375,
                     "text" : "Bill Gates",
                     "offset" : 25,
                     "length" : 10
                  }
               ],
               "wikipediaLanguage" : "en",
               "wikipediaId" : "Bill Gates",
               "wikipediaUrl" : "https://en.wikipedia.org/wiki/Bill_Gates",
               "bingId" : "0d47c987-0042-5576-15e8-97af601614fa",
               "type" : "Person"
            },
            {
               "name" : "Paul Allen",
               "matches" : [
                  {
                     "wikipediaScore" : 0.52977533244176866,
                     "entityTypeScore" : 0.99884098768234253,
                     "text" : "Paul Allen",
                     "offset" : 40,
                     "length" : 10
                  }
               ],
               "wikipediaLanguage" : "en",
               "wikipediaId" : "Paul Allen",
               "wikipediaUrl" : "https://en.wikipedia.org/wiki/Paul_Allen",
               "bingId" : "df2c4376-9923-6a54-893f-2ee5a5badbc7",
               "type" : "Person"
            },
            {
               "name" : "April 4",
               "matches" : [
                  {
                     "wikipediaScore" : 0.37220990924571939,
                     "entityTypeScore" : 0.8,
                     "text" : "April 4",
                     "offset" : 54,
                     "length" : 7
                  }
               ],
               "wikipediaLanguage" : "en",
               "wikipediaId" : "April 4",
               "wikipediaUrl" : "https://en.wikipedia.org/wiki/April_4",
               "bingId" : "52535f87-235e-b513-54fe-c03e4233ac6e",
               "type" : "Other"
            },
            {
               "name" : "April 4, 1975",
               "matches" : [
                  {
                     "entityTypeScore" : 0.8,
                     "text" : "April 4, 1975",
                     "offset" : 54,
                     "length" : 13
                  }
               ],
               "type" : "DateTime",
               "subType" : "Date"
            },
            {
               "name" : "BASIC",
               "matches" : [
                  {
                     "wikipediaScore" : 0.35686239324548041,
                     "entityTypeScore" : 0.8,
                     "text" : "BASIC",
                     "offset" : 89,
                     "length" : 5
                  }
               ],
               "wikipediaLanguage" : "en",
               "wikipediaId" : "BASIC",
               "wikipediaUrl" : "https://en.wikipedia.org/wiki/BASIC",
               "bingId" : "5b16443d-501c-58f3-352e-611bbe75aa6e",
               "type" : "Other"
            },
            {
               "name" : "Altair 8800",
               "matches" : [
                  {
                     "wikipediaScore" : 0.868324676465041,
                     "entityTypeScore" : 0.8,
                     "text" : "Altair 8800",
                     "offset" : 116,
                     "length" : 11
                  }
               ],
               "wikipediaLanguage" : "en",
               "wikipediaId" : "Altair 8800",
               "wikipediaUrl" : "https://en.wikipedia.org/wiki/Altair_8800",
               "bingId" : "7216c654-3779-68a2-c7b7-12ff3dad5606",
               "type" : "Other"
            },
            {
               "name" : "Altair",
               "matches" : [
                  {
                     "entityTypeScore" : 0.52505272626876831,
                     "text" : "Altair",
                     "offset" : 116,
                     "length" : 6
                  }
               ],
               "type" : "Organization"
            },
            {
               "name" : "8800",
               "matches" : [
                  {
                     "entityTypeScore" : 0.8,
                     "text" : "8800",
                     "offset" : 123,
                     "length" : 4
                  }
               ],
               "type" : "Quantity",
               "subType" : "Number"
            }
         ]
      }
   ],
   "errors" : []
}

Nästa stegNext steps

Se ocksåSee also

Översikt över TextanalysText Analytics overview
Vanliga frågor och svar (FAQ)Frequently asked questions (FAQ)