AWS AI Services Programming Series - Part4 (Comprehend)

   Go back to the Task List

  « 4: Detect Key Phrases in the Text    6: Sentiment Analysis in the Text »

5: Syntax Analysis in the Text

The Amazon Comprehend Syntax analysis enables customers to analyze text using tokenization and Parts of Speech (PoS), and identify word boundaries and labels like nouns and adjectives within the text. Let’s update the code for it.

  1. In AWS Cloud9 console, update dojocomprehend.py file to inspect text for syntax and the part of speech of words. 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_syntax(Text=mytext, LanguageCode= thelangcode)
    
    for e in response["SyntaxTokens"]:
        print (e["Text"] + " - " + str(e["PartOfSpeech"]["Tag"]))	
    

    `

  2. In the code above, you use detect_syntax method to inspect text for syntax and the part of speech of words. Execute python dojocomprehend.py command to run the dojocomprehend.py code.

    Comprehend

  3. Finally, in the next step, you perform sentiment analysis for the text.