Menu iconMenu iconNatural Language Processing with Python
Natural Language Processing with Python

Chapter 6: Syntax and Parsing

6.6 Practical Exercises of Chapter 6: Syntax and Parsing

  1. POS Tagging Exercise: Use NLTK's pos_tag function to perform POS tagging on the following sentences:
    • "The quick brown fox jumps over the lazy dog."
    • "Python is a high-level programming language."

    Note the POS tags for each word in the sentence and try to understand what they mean.

    Example:

    import nltk
    nltk.download('averaged_perceptron_tagger')

    sentences = ["The quick brown fox jumps over the lazy dog.",
                 "Python is a high-level programming language."]

    for sentence in sentences:
        text = nltk.word_tokenize(sentence)
        print(nltk.pos_tag(text))
  2. NER Exercise: Use Spacy's NER capabilities to perform named entity recognition on the following sentences:
    • "Apple Inc. is planning to open a new factory in India."
    • "Barack Obama was the President of the United States."

    Identify the named entities in each sentence and their types.

    Example:

    import spacy

    nlp = spacy.load("en_core_web_sm")

    sentences = ["Apple Inc. is planning to open a new factory in India.",
                 "Barack Obama was the President of the United States."]

    for sentence in sentences:
        doc = nlp(sentence)
        for ent in doc.ents:
            print(ent.text, ent.label_)
  3. Dependency Parsing Exercise: Use Spacy's nlp function and the displacy module to perform dependency parsing and visualize the dependency tree for the following sentence:
    • "The cat sat on the mat."

    Example:

    import spacy
    from spacy import displacy

    nlp = spacy.load("en_core_web_sm")

    doc = nlp("The cat sat on the mat.")
    displacy.serve(doc, style="dep")
  4. Constituency Parsing Exercise: Use NLTK's RegexpParser to create a grammar that can parse the following sentence:
    • "My dog also likes eating sausage."

    Try different grammars and see how they affect the constituency tree.

    Example:

    import nltk
    nltk.download('punkt')

    sentence = "My dog also likes eating sausage."
    tokens = nltk.word_tokenize(sentence)

    # Define your grammar using regular expressions
    grammar = ('''
        NP: {<DT>?<JJ>*<NN>} # NP
        VBD: {<VBD><RB>?<VBG><NN>} # VBD
    ''')

    chunkParser = nltk.RegexpParser(grammar)
    tagged = nltk.pos_tag(tokens)
    tree = chunkParser.parse(tagged)

    for subtree in tree.subtrees():
        print(subtree)

Remember to replace the grammar in the Constituency Parsing Exercise with the one that works best for your sentence.

Remember, the exercises provided are designed to aid you in comprehending the concepts more thoroughly. It is recommended that you attempt to complete each exercise without referring to the solutions. Only consult the solutions if you are completely stuck or after you have finished the exercises to verify your answers.

It is important to note that by completing the exercises on your own, you are actively engaging with the material and reinforcing your understanding of the concepts. Therefore, it is advisable to attempt the exercises without assistance in order to maximize the potential benefits of this learning opportunity.

6.6 Practical Exercises of Chapter 6: Syntax and Parsing

  1. POS Tagging Exercise: Use NLTK's pos_tag function to perform POS tagging on the following sentences:
    • "The quick brown fox jumps over the lazy dog."
    • "Python is a high-level programming language."

    Note the POS tags for each word in the sentence and try to understand what they mean.

    Example:

    import nltk
    nltk.download('averaged_perceptron_tagger')

    sentences = ["The quick brown fox jumps over the lazy dog.",
                 "Python is a high-level programming language."]

    for sentence in sentences:
        text = nltk.word_tokenize(sentence)
        print(nltk.pos_tag(text))
  2. NER Exercise: Use Spacy's NER capabilities to perform named entity recognition on the following sentences:
    • "Apple Inc. is planning to open a new factory in India."
    • "Barack Obama was the President of the United States."

    Identify the named entities in each sentence and their types.

    Example:

    import spacy

    nlp = spacy.load("en_core_web_sm")

    sentences = ["Apple Inc. is planning to open a new factory in India.",
                 "Barack Obama was the President of the United States."]

    for sentence in sentences:
        doc = nlp(sentence)
        for ent in doc.ents:
            print(ent.text, ent.label_)
  3. Dependency Parsing Exercise: Use Spacy's nlp function and the displacy module to perform dependency parsing and visualize the dependency tree for the following sentence:
    • "The cat sat on the mat."

    Example:

    import spacy
    from spacy import displacy

    nlp = spacy.load("en_core_web_sm")

    doc = nlp("The cat sat on the mat.")
    displacy.serve(doc, style="dep")
  4. Constituency Parsing Exercise: Use NLTK's RegexpParser to create a grammar that can parse the following sentence:
    • "My dog also likes eating sausage."

    Try different grammars and see how they affect the constituency tree.

    Example:

    import nltk
    nltk.download('punkt')

    sentence = "My dog also likes eating sausage."
    tokens = nltk.word_tokenize(sentence)

    # Define your grammar using regular expressions
    grammar = ('''
        NP: {<DT>?<JJ>*<NN>} # NP
        VBD: {<VBD><RB>?<VBG><NN>} # VBD
    ''')

    chunkParser = nltk.RegexpParser(grammar)
    tagged = nltk.pos_tag(tokens)
    tree = chunkParser.parse(tagged)

    for subtree in tree.subtrees():
        print(subtree)

Remember to replace the grammar in the Constituency Parsing Exercise with the one that works best for your sentence.

Remember, the exercises provided are designed to aid you in comprehending the concepts more thoroughly. It is recommended that you attempt to complete each exercise without referring to the solutions. Only consult the solutions if you are completely stuck or after you have finished the exercises to verify your answers.

It is important to note that by completing the exercises on your own, you are actively engaging with the material and reinforcing your understanding of the concepts. Therefore, it is advisable to attempt the exercises without assistance in order to maximize the potential benefits of this learning opportunity.

6.6 Practical Exercises of Chapter 6: Syntax and Parsing

  1. POS Tagging Exercise: Use NLTK's pos_tag function to perform POS tagging on the following sentences:
    • "The quick brown fox jumps over the lazy dog."
    • "Python is a high-level programming language."

    Note the POS tags for each word in the sentence and try to understand what they mean.

    Example:

    import nltk
    nltk.download('averaged_perceptron_tagger')

    sentences = ["The quick brown fox jumps over the lazy dog.",
                 "Python is a high-level programming language."]

    for sentence in sentences:
        text = nltk.word_tokenize(sentence)
        print(nltk.pos_tag(text))
  2. NER Exercise: Use Spacy's NER capabilities to perform named entity recognition on the following sentences:
    • "Apple Inc. is planning to open a new factory in India."
    • "Barack Obama was the President of the United States."

    Identify the named entities in each sentence and their types.

    Example:

    import spacy

    nlp = spacy.load("en_core_web_sm")

    sentences = ["Apple Inc. is planning to open a new factory in India.",
                 "Barack Obama was the President of the United States."]

    for sentence in sentences:
        doc = nlp(sentence)
        for ent in doc.ents:
            print(ent.text, ent.label_)
  3. Dependency Parsing Exercise: Use Spacy's nlp function and the displacy module to perform dependency parsing and visualize the dependency tree for the following sentence:
    • "The cat sat on the mat."

    Example:

    import spacy
    from spacy import displacy

    nlp = spacy.load("en_core_web_sm")

    doc = nlp("The cat sat on the mat.")
    displacy.serve(doc, style="dep")
  4. Constituency Parsing Exercise: Use NLTK's RegexpParser to create a grammar that can parse the following sentence:
    • "My dog also likes eating sausage."

    Try different grammars and see how they affect the constituency tree.

    Example:

    import nltk
    nltk.download('punkt')

    sentence = "My dog also likes eating sausage."
    tokens = nltk.word_tokenize(sentence)

    # Define your grammar using regular expressions
    grammar = ('''
        NP: {<DT>?<JJ>*<NN>} # NP
        VBD: {<VBD><RB>?<VBG><NN>} # VBD
    ''')

    chunkParser = nltk.RegexpParser(grammar)
    tagged = nltk.pos_tag(tokens)
    tree = chunkParser.parse(tagged)

    for subtree in tree.subtrees():
        print(subtree)

Remember to replace the grammar in the Constituency Parsing Exercise with the one that works best for your sentence.

Remember, the exercises provided are designed to aid you in comprehending the concepts more thoroughly. It is recommended that you attempt to complete each exercise without referring to the solutions. Only consult the solutions if you are completely stuck or after you have finished the exercises to verify your answers.

It is important to note that by completing the exercises on your own, you are actively engaging with the material and reinforcing your understanding of the concepts. Therefore, it is advisable to attempt the exercises without assistance in order to maximize the potential benefits of this learning opportunity.

6.6 Practical Exercises of Chapter 6: Syntax and Parsing

  1. POS Tagging Exercise: Use NLTK's pos_tag function to perform POS tagging on the following sentences:
    • "The quick brown fox jumps over the lazy dog."
    • "Python is a high-level programming language."

    Note the POS tags for each word in the sentence and try to understand what they mean.

    Example:

    import nltk
    nltk.download('averaged_perceptron_tagger')

    sentences = ["The quick brown fox jumps over the lazy dog.",
                 "Python is a high-level programming language."]

    for sentence in sentences:
        text = nltk.word_tokenize(sentence)
        print(nltk.pos_tag(text))
  2. NER Exercise: Use Spacy's NER capabilities to perform named entity recognition on the following sentences:
    • "Apple Inc. is planning to open a new factory in India."
    • "Barack Obama was the President of the United States."

    Identify the named entities in each sentence and their types.

    Example:

    import spacy

    nlp = spacy.load("en_core_web_sm")

    sentences = ["Apple Inc. is planning to open a new factory in India.",
                 "Barack Obama was the President of the United States."]

    for sentence in sentences:
        doc = nlp(sentence)
        for ent in doc.ents:
            print(ent.text, ent.label_)
  3. Dependency Parsing Exercise: Use Spacy's nlp function and the displacy module to perform dependency parsing and visualize the dependency tree for the following sentence:
    • "The cat sat on the mat."

    Example:

    import spacy
    from spacy import displacy

    nlp = spacy.load("en_core_web_sm")

    doc = nlp("The cat sat on the mat.")
    displacy.serve(doc, style="dep")
  4. Constituency Parsing Exercise: Use NLTK's RegexpParser to create a grammar that can parse the following sentence:
    • "My dog also likes eating sausage."

    Try different grammars and see how they affect the constituency tree.

    Example:

    import nltk
    nltk.download('punkt')

    sentence = "My dog also likes eating sausage."
    tokens = nltk.word_tokenize(sentence)

    # Define your grammar using regular expressions
    grammar = ('''
        NP: {<DT>?<JJ>*<NN>} # NP
        VBD: {<VBD><RB>?<VBG><NN>} # VBD
    ''')

    chunkParser = nltk.RegexpParser(grammar)
    tagged = nltk.pos_tag(tokens)
    tree = chunkParser.parse(tagged)

    for subtree in tree.subtrees():
        print(subtree)

Remember to replace the grammar in the Constituency Parsing Exercise with the one that works best for your sentence.

Remember, the exercises provided are designed to aid you in comprehending the concepts more thoroughly. It is recommended that you attempt to complete each exercise without referring to the solutions. Only consult the solutions if you are completely stuck or after you have finished the exercises to verify your answers.

It is important to note that by completing the exercises on your own, you are actively engaging with the material and reinforcing your understanding of the concepts. Therefore, it is advisable to attempt the exercises without assistance in order to maximize the potential benefits of this learning opportunity.