OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

case 3 output is not as per expected output and it is failing for Hands on POS Tagging - NLP using python

  • Thread starter Thread starter MdAA
  • Start date Start date
M

MdAA

Guest
Define a function called 'tagPOS' which takes three parameters.

The first parameter, 'textcontent', is a string, the second parameter, 'taggedtextcontent', is also as string,

and the third parameter 'defined_tags', is a dictionary. The function definition code stub is given in the editor.

perform the following specified task.


  1. Tag the part of speech for the given 'textcontent' words, store the result into the variable 'nltk_pos_tags'(hint: use pos_tag)


  2. Tag the part of speech for the given 'taggedtextcontent' words using the Tagging_Text_method. Store the result into the variable 'tagged_pos_tag'.


  3. Tag the part of speech for the given 'textcontent' words and use 'defined_tags' as a model in the Lookup Tagger method. Store the result into the variable 'unigram_pos_tag'.

Return 'nltk_pos_tags', 'tagged_pos_tag','unigram_pos_tag' variables from the function.

Input Format for custom Testing:

Inpurt from stdin will be process as follows and passed to the function.

The first line contains a string 'textcontent'. Text content is used to tag part of speech.

The second line conatins a sting 'taggedtextcontent'. The tagged text content is used to tag part of speech, which contains the tag itself.

Sample Case 1:

Sample Input 1:

STDIN Function Parameters

----- -------------------

Python is awesome. → textcontent = 'Python is awesome.'

Python/NNP is/VBZ awesome/DT ./. → taggedtextcontent = 'Python/NNP is/VBZ awesome/DT ./.'

Sample Expected Output for given input 1:

[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'JJ'), ('.', '.')]

[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'DT'), ('.', '.')]

[('Python', None), ('is', 'VERB'), ('awesome', 'ADJ'), ('.', '.')]

Explanation:

The first line displays POS tagged words using pos_tag for the given 'textcontent'

The second line dsiplays POS tagged words using the Tagging Text mehtod for the given 'taggedtextcontent'.

The third line displays POS tagged words using the Lookup Tagger method for the given 'textcontent'.

Code:
import nltk
from nltk import pos_tag, word_tokenize
from nltk.tag import UnigramTagger

def tagPOS(textcontent, taggedtextcontent, defined_tags):
    # Step 1: NLTK POS Tagging
    nltk_pos_tags = pos_tag(word_tokenize(textcontent))
    
    # Step 2: Tagging Text Method
    tagged_pos_tag = []
    tagged_words = taggedtextcontent.split()
    for tagged_word in tagged_words:
        word, tag = tagged_word.rsplit('/', 1)
        tagged_pos_tag.append((word, tag))
    
    # Step 3: Lookup Tagger Method
    # Create a UnigramTagger based on defined_tags
    unigram_tagger = UnigramTagger(model=defined_tags)
    words = word_tokenize(textcontent)
    unigram_pos_tags = unigram_tagger.tag(words)
    
    return nltk_pos_tags, tagged_pos_tag, unigram_pos_tags


Case 1: 

Python is awesome.
Python/NNP is/VBZ awesome/DT ./.


My Output:
[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'JJ'), ('.', '.')]
[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'DT'), ('.', '.')]
[('Python', None), ('is', 'VERB'), ('awesome', 'ADJ'), ('.', '.')]

Expected Output:
[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'JJ'), ('.', '.')]
[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'DT'), ('.', '.')]
[('Python', None), ('is', 'VERB'), ('awesome', 'ADJ'), ('.', '.')]


Case 2:
If eating three-egg omelets causes weight-gain, budgie eggs are a good substitute.
If/IN eating/VBG three/CD -/: egg/NN omelets/NNS causes/NNS weight/NN -/: gain/NN ,/, budgie/NNP eggs/NNS are/VBP a/DT good/JJ substitute/NN ./.
 
My Output:
[('If', 'IN'), ('eating', 'JJ'), ('three-egg', 'JJ'), ('omelets', 'NNS'), ('causes', 'VBZ'), ('weight-gain', 'NN'), (',', ','), ('budgie', 'NN'), ('eggs', 'NNS'), ('are', 'VBP'), ('a', 'DT'), ('good', 'JJ'), ('substitute', 'NN'), ('.', '.')]
[('If', 'IN'), ('eating', 'VBG'), ('three', 'CD'), ('-', ':'), ('egg', 'NN'), ('omelets', 'NNS'), ('causes', 'NNS'), ('weight', 'NN'), ('-', ':'), ('gain', 'NN'), (',', ','), ('budgie', 'NNP'), ('eggs', 'NNS'), ('are', 'VBP'), ('a', 'DT'), ('good', 'JJ'), ('substitute', 'NN'), ('.', '.')]
[('If', 'ADP'), ('eating', 'VERB'), ('three-egg', None), ('omelets', None), ('causes', 'VERB'), ('weight-gain', None), (',', '.'), ('budgie', None), ('eggs', 'NOUN'), ('are', 'VERB'), ('a', 'DET'), ('good', 'ADJ'), ('substitute', 'NOUN'), ('.', '.')]


Expected Output:
[('If', 'IN'), ('eating', 'JJ'), ('three-egg', 'JJ'), ('omelets', 'NNS'), ('causes', 'VBZ'), ('weight-gain', 'NN'), (',', ','), ('budgie', 'NN'), ('eggs', 'NNS'), ('are', 'VBP'), ('a', 'DT'), ('good', 'JJ'), ('substitute', 'NN'), ('.', '.')]
[('If', 'IN'), ('eating', 'VBG'), ('three', 'CD'), ('-', ':'), ('egg', 'NN'), ('omelets', 'NNS'), ('causes', 'NNS'), ('weight', 'NN'), ('-', ':'), ('gain', 'NN'), (',', ','), ('budgie', 'NNP'), ('eggs', 'NNS'), ('are', 'VBP'), ('a', 'DT'), ('good', 'JJ'), ('substitute', 'NN'), ('.', '.')]
[('If', 'ADP'), ('eating', 'VERB'), ('three-egg', None), ('omelets', None), ('causes', 'VERB'), ('weight-gain', None), (',', '.'), ('budgie', None), ('eggs', 'NOUN'), ('are', 'VERB'), ('a', 'DET'), ('good', 'ADJ'), ('substitute', 'NOUN'), ('.', '.')]


Case 3:

My Output:
[('When', 'WRB'), ('money', 'NN'), ('was', 'VBD'), ('tight', 'RB'), (',', ','), ('he', 'PRP'), ("'d", 'MD'), ('get', 'VB'), ('his', 'PRP$'), ('lunch', 'NN'), ('money', 'NN'), ('from', 'IN'), ('the', 'DT'), ('local', 'JJ'), ('wishing', 'NN'), ('well', 'RB'), ('.', '.')]
[('When', 'WRB'), ('money', 'NN'), ('was', 'VBD'), ('tight', 'JJ'), (',', ','), ('he', 'PRP'), ("'", 'POS'), ('d', '-None-'), ('get', 'VB'), ('his', 'PRP$'), ('lunch', 'NN'), ('money', 'NN'), ('from', 'IN'), ('the', 'DT'), ('local', 'JJ'), ('wishing', 'VBG'), ('well', 'RB'), ('.', '.')]
[('When', 'ADV'), ('money', 'NOUN'), ('was', 'VERB'), ('tight', 'ADV'), (',', '.'), ('he', 'PRON'), ("'d", None), ('get', 'VERB'), ('his', 'DET'), ('lunch', 'NOUN'), ('money', 'NOUN'), ('from', 'ADP'), ('the', 'DET'), ('local', 'ADJ'), ('wishing', 'VERB'), ('well', 'ADV'), ('.', '.')]
<p>Define a function called 'tagPOS' which takes three parameters.</p>
<p>The first parameter, 'textcontent', is a string, the second parameter, 'taggedtextcontent', is also as string,</p>
<p>and the third parameter 'defined_tags', is a dictionary. The function definition code stub is given in the editor.</p>
<p>perform the following specified task.</p>
<ol>
<li><p>Tag the part of speech for the given 'textcontent' words, store the result into the variable 'nltk_pos_tags'(hint: use pos_tag)</p>
</li>
<li><p>Tag the part of speech for the given 'taggedtextcontent' words using the Tagging_Text_method. Store the result into the variable 'tagged_pos_tag'.</p>
</li>
<li><p>Tag the part of speech for the given 'textcontent' words and use 'defined_tags' as a model in the Lookup Tagger method. Store the result into the variable 'unigram_pos_tag'.</p>
</li>
</ol>
<p>Return 'nltk_pos_tags', 'tagged_pos_tag','unigram_pos_tag' variables from the function.</p>
<p>Input Format for custom Testing:</p>
<p>Inpurt from stdin will be process as follows and passed to the function.</p>
<p>The first line contains a string 'textcontent'. Text content is used to tag part of speech.</p>
<p>The second line conatins a sting 'taggedtextcontent'. The tagged text content is used to tag part of speech, which contains the tag itself.</p>
<p>Sample Case 1:</p>
<p>Sample Input 1:</p>
<p>STDIN Function Parameters</p>
<p>----- -------------------</p>
<p>Python is awesome. → textcontent = 'Python is awesome.'</p>
<p>Python/NNP is/VBZ awesome/DT ./. → taggedtextcontent = 'Python/NNP is/VBZ awesome/DT ./.'</p>
<p>Sample Expected Output for given input 1:</p>
<p>[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'JJ'), ('.', '.')]</p>
<p>[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'DT'), ('.', '.')]</p>
<p>[('Python', None), ('is', 'VERB'), ('awesome', 'ADJ'), ('.', '.')]</p>
<p>Explanation:</p>
<p>The first line displays POS tagged words using pos_tag for the given 'textcontent'</p>
<p>The second line dsiplays POS tagged words using the Tagging Text mehtod for the given 'taggedtextcontent'.</p>
<p>The third line displays POS tagged words using the Lookup Tagger method for the given 'textcontent'.</p>
<pre><code>import nltk
from nltk import pos_tag, word_tokenize
from nltk.tag import UnigramTagger

def tagPOS(textcontent, taggedtextcontent, defined_tags):
# Step 1: NLTK POS Tagging
nltk_pos_tags = pos_tag(word_tokenize(textcontent))

# Step 2: Tagging Text Method
tagged_pos_tag = []
tagged_words = taggedtextcontent.split()
for tagged_word in tagged_words:
word, tag = tagged_word.rsplit('/', 1)
tagged_pos_tag.append((word, tag))

# Step 3: Lookup Tagger Method
# Create a UnigramTagger based on defined_tags
unigram_tagger = UnigramTagger(model=defined_tags)
words = word_tokenize(textcontent)
unigram_pos_tags = unigram_tagger.tag(words)

return nltk_pos_tags, tagged_pos_tag, unigram_pos_tags


Case 1:

Python is awesome.
Python/NNP is/VBZ awesome/DT ./.


My Output:
[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'JJ'), ('.', '.')]
[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'DT'), ('.', '.')]
[('Python', None), ('is', 'VERB'), ('awesome', 'ADJ'), ('.', '.')]

Expected Output:
[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'JJ'), ('.', '.')]
[('Python', 'NNP'), ('is', 'VBZ'), ('awesome', 'DT'), ('.', '.')]
[('Python', None), ('is', 'VERB'), ('awesome', 'ADJ'), ('.', '.')]


Case 2:
If eating three-egg omelets causes weight-gain, budgie eggs are a good substitute.
If/IN eating/VBG three/CD -/: egg/NN omelets/NNS causes/NNS weight/NN -/: gain/NN ,/, budgie/NNP eggs/NNS are/VBP a/DT good/JJ substitute/NN ./.

My Output:
[('If', 'IN'), ('eating', 'JJ'), ('three-egg', 'JJ'), ('omelets', 'NNS'), ('causes', 'VBZ'), ('weight-gain', 'NN'), (',', ','), ('budgie', 'NN'), ('eggs', 'NNS'), ('are', 'VBP'), ('a', 'DT'), ('good', 'JJ'), ('substitute', 'NN'), ('.', '.')]
[('If', 'IN'), ('eating', 'VBG'), ('three', 'CD'), ('-', ':'), ('egg', 'NN'), ('omelets', 'NNS'), ('causes', 'NNS'), ('weight', 'NN'), ('-', ':'), ('gain', 'NN'), (',', ','), ('budgie', 'NNP'), ('eggs', 'NNS'), ('are', 'VBP'), ('a', 'DT'), ('good', 'JJ'), ('substitute', 'NN'), ('.', '.')]
[('If', 'ADP'), ('eating', 'VERB'), ('three-egg', None), ('omelets', None), ('causes', 'VERB'), ('weight-gain', None), (',', '.'), ('budgie', None), ('eggs', 'NOUN'), ('are', 'VERB'), ('a', 'DET'), ('good', 'ADJ'), ('substitute', 'NOUN'), ('.', '.')]


Expected Output:
[('If', 'IN'), ('eating', 'JJ'), ('three-egg', 'JJ'), ('omelets', 'NNS'), ('causes', 'VBZ'), ('weight-gain', 'NN'), (',', ','), ('budgie', 'NN'), ('eggs', 'NNS'), ('are', 'VBP'), ('a', 'DT'), ('good', 'JJ'), ('substitute', 'NN'), ('.', '.')]
[('If', 'IN'), ('eating', 'VBG'), ('three', 'CD'), ('-', ':'), ('egg', 'NN'), ('omelets', 'NNS'), ('causes', 'NNS'), ('weight', 'NN'), ('-', ':'), ('gain', 'NN'), (',', ','), ('budgie', 'NNP'), ('eggs', 'NNS'), ('are', 'VBP'), ('a', 'DT'), ('good', 'JJ'), ('substitute', 'NN'), ('.', '.')]
[('If', 'ADP'), ('eating', 'VERB'), ('three-egg', None), ('omelets', None), ('causes', 'VERB'), ('weight-gain', None), (',', '.'), ('budgie', None), ('eggs', 'NOUN'), ('are', 'VERB'), ('a', 'DET'), ('good', 'ADJ'), ('substitute', 'NOUN'), ('.', '.')]


Case 3:

My Output:
[('When', 'WRB'), ('money', 'NN'), ('was', 'VBD'), ('tight', 'RB'), (',', ','), ('he', 'PRP'), ("'d", 'MD'), ('get', 'VB'), ('his', 'PRP$'), ('lunch', 'NN'), ('money', 'NN'), ('from', 'IN'), ('the', 'DT'), ('local', 'JJ'), ('wishing', 'NN'), ('well', 'RB'), ('.', '.')]
[('When', 'WRB'), ('money', 'NN'), ('was', 'VBD'), ('tight', 'JJ'), (',', ','), ('he', 'PRP'), ("'", 'POS'), ('d', '-None-'), ('get', 'VB'), ('his', 'PRP$'), ('lunch', 'NN'), ('money', 'NN'), ('from', 'IN'), ('the', 'DT'), ('local', 'JJ'), ('wishing', 'VBG'), ('well', 'RB'), ('.', '.')]
[('When', 'ADV'), ('money', 'NOUN'), ('was', 'VERB'), ('tight', 'ADV'), (',', '.'), ('he', 'PRON'), ("'d", None), ('get', 'VERB'), ('his', 'DET'), ('lunch', 'NOUN'), ('money', 'NOUN'), ('from', 'ADP'), ('the', 'DET'), ('local', 'ADJ'), ('wishing', 'VERB'), ('well', 'ADV'), ('.', '.')]
</code></pre>
 

Latest posts

D
Replies
0
Views
1
Dhanushka Amarakoon
D
S
Replies
0
Views
1
Shikhar Ambashta
S
Top