# -*- coding: utf-8 -*-
"""2_d_tagger_Map Words Dictionaries

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1B5BcRVUJOhdMKl0ATcDtBnKhDV5LF1qT

d. Map Words to Properties Using Python Dictionaries
"""

tdict = {"brand": "Ford","model": "Mustang","year": 1964}
print(tdict)
print(tdict["brand"])
print(len(tdict))
print(type(tdict))

"""e. Study i) DefaultTagger, ii) Regular expression tagger, iii) UnigramTagger

i) DefaultTagger
"""

import nltk
nltk.download('treebank')
from nltk.tag import DefaultTagger
from nltk.corpus import treebank

exptagger = DefaultTagger('NN')
testsentences = treebank.tagged_sents()

#print(exptagger.evaluate(testsentences[1000:]))
print(exptagger.tag_sents([['Hi', ','], ['How', 'are', 'you', '?']]))

import nltk
from nltk.tag import DefaultTagger
exptagger = DefaultTagger('NN')
from nltk.corpus import treebank
testsentences = treebank.tagged_sents() [1000:]
print(exptagger.accuracy (testsentences))


import nltk
from nltk.tag import DefaultTagger
exptagger = DefaultTagger('NN')
print(exptagger.tag_sents([['Hi', ','], ['How', 'are', 'you', '?']]))

"""ii) Regular expression tagger

"""

import nltk
nltk.download('brown')
from nltk.corpus import brown
from nltk.tag import RegexpTagger
test_sent = brown.sents(categories='news')[0]
regexp_tagger = RegexpTagger(
    [(r'^-?[0-9]+(.[0-9]+)?$', 'CD'),
    (r'(The|the|A|a|An|an)$', 'AT'),
    (r'.*able$', 'JJ'),
    (r'.*ness$', 'NN'),
    (r'.*ly$', 'RB'),
    (r'.*s$', 'NNS'),
    (r'.*ing$', 'VBG'),
    (r'.*ed$', 'VBD'),
    (r'.*', 'NN')])
print(regexp_tagger)
print(regexp_tagger.tag(test_sent))

import nltk
nltk.download('brown') # Download the 'brown' corpus if not already present
from nltk.corpus import brown
from nltk.tag import RegexpTagger

# Get the first sentence from the 'news' category of the brown corpus for testing
test_sent = brown.sents(categories='news')[0]

# Define a regular expression tagger with a list of (pattern, tag) pairs
regexp_tagger = RegexpTagger(
    # Tag numbers (integers or decimals) as 'CD' (Cardinal Digit)
    [(r'^-?[0-9]+(.[0-9]+)?$', 'CD'),
    # Tag common articles as 'AT'
    (r'(The|the|A|a|An|an)$', 'AT'),
    # Tag words ending with 'able' as 'JJ' (Adjective)
    (r'.*able$', 'JJ'),
    # Tag words ending with 'ness' as 'NN' (Noun, singular or mass)
    (r'.*ness$', 'NN'),
    # Tag words ending with 'ly' as 'RB' (Adverb)
    (r'.*ly$', 'RB'),
    # Tag words ending with 's' as 'NNS' (Noun, plural)
    (r'.*s$', 'NNS'),
    # Tag words ending with 'ing' as 'VBG' (Verb, gerund or present participle)
    (r'.*ing$', 'VBG'),
    # Tag words ending with 'ed' as 'VBD' (Verb, past tense)
    (r'.*ed$', 'VBD'),
    # Default tag for any other word is 'NN' (Noun, singular or mass)
    (r'.*', 'NN')])

# Print the tagger object (shows its type and size)
print(regexp_tagger)

# Tag the test sentence and print the result
print(regexp_tagger.tag(test_sent))

"""iii) UnigramTagger

"""

from nltk.tag import UnigramTagger

from nltk.corpus import treebank

train_sents = treebank.tagged_sents()[:10]
tagger = UnigramTagger(train_sents)

print(treebank.sents()[0])
print('\n',tagger.tag(treebank.sents()[0]))
tagger.tag(treebank.sents()[0])

tagger = UnigramTagger(model ={'Pierre': 'NN'})
print('\n',tagger.tag(treebank.sents()[0]))

