AWS AI Services Programming Series - Part4 (Comprehend)

   Go back to the Task List

  « 3: Detect Entities in the Text    5: Syntax Analysis in the Text »

4: Detect Key Phrases in the Text

In this step, you use Amazon Comprehend to detect key phrases in the text. The key phrases are the talking points in the text. The Keyphrase Extraction API returns the key phrases and a confidence score to support that this is a key phrase.

  1. In AWS Cloud9 console, update dojocomprehend.py file to detect the phrases in the text. Update the code as shown below and save the file.

    Comprehend

    import boto3
    
    client = boto3.client('comprehend')
    
    mytext = "Luxembourg , officially the Grand Duchy of Luxembourg, is a landlocked country in Western Europe. It is bordered by Belgium to the west and north, Germany to the east, and France to the south. Its capital, Luxembourg City, is one of the four official capitals of the European Union (together with Brussels, Frankfurt, and Strasbourg) and the seat of the Court of Justice of the European Union, the highest judicial authority in the EU. Its culture, people, and languages are highly intertwined with its neighbours, making it essentially a mixture of French and German cultures, as evident by the nation's three official languages: French, German, and the national language of Luxembourgish. The repeated invasions by Germany, especially in World War II, resulted in the country's strong will for mediation between France and Germany and, among other things, led to the foundation of the European Union."
    
    response = client.detect_dominant_language(Text= mytext)
    
    thelangcode = response["Languages"][0]["LanguageCode"]
    
    response = client.detect_key_phrases(Text=mytext, LanguageCode= thelangcode)
    
    for e in response["KeyPhrases"]:
        print (e["Text"] + " - confidence level - " + str(e["Score"]))
    

    `

  2. In the code above, you use detect_key_phrases method to detect and print the key phrases in the text. Execute python dojocomprehend.py command to run the dojocomprehend.py code to print the phrases and their confidence level score.

    Comprehend

  3. Great so far. In the next step, you update the code to perform syntax analysis.