# -*- coding: utf-8 -*-
"""6_Tagging, chunking, NER

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1cfOczmE4Vk_t3N-slzYRYavAEinA-b47

A] Part of speech Tagging and chunking of user defined text.
"""

import nltk
from nltk import tokenize
nltk.download('punkt_tab')
from nltk import tag
from nltk import chunk
nltk.download('averaged_perceptron_tagger_eng')
nltk.download('maxent_ne_chunker_tab')
nltk.download('words')
para="Helloo my name is abc. Today you'll be learning NLTK."
sents=tokenize.sent_tokenize(para)
print("\nsenstence tokenization\n========\n",sents)

# word tokenization
print("""
word
tokenization
=======
""")
for index in range(len(sents)):
  words=tokenize.word_tokenize(sents[index])
  print(words)


"""# POS Tagging

"""

tagged_words=[]
for index in range(len(sents)):
    words_for_sentence = tokenize.word_tokenize(sents[index])
    tagged_words.append(tag.pos_tag(words_for_sentence))
print("\nPOS Tagging\n========\n",tagged_words)

"""# chunking

"""

tree=[]
for index in range(len(sents)):
    tree.append(chunk.ne_chunk(tagged_words[index]))
print("\nchunking\n========\n")
print(tree)



"""##B] Named Entity recognition using user defined text.
**bold text**
"""

###!pip install -U spacy
###!python -m spacy download en_core_web_sm
import spacy
nlp = spacy.load("en_core_web_sm")
text="Apple is looking at buying U.K. startup for $1 billion"
doc=nlp(text)
print(f"{'Token':<10}{'POS Tag':<10}{'Detailed Tag':<15}")
print("-"*35)
for token in doc:
    print(f"{token.text:<10}{token.pos_:<10}{token.tag_:<15}{spacy.explain(token.tag_)}")
proper_nouns=[token.text for token in doc if token.pos_=="PROPN"]
verbs=[token.text for token in doc if token.pos_=="VERB"]
adjectives=[token.text for token in doc if token.pos_=="ADJ"]
print(f"Verbs:{verbs}")
print(f"Proper Nouns: {proper_nouns}")
print(f"Adjective:{adjectives}")

"""#or

#BEST
"""

import spacy
# Load English tokenizer, tagger, parser and NER
nlp = spacy.load("en_core_web_sm")
# Process whole documents
text = ("When Sebastian Thrun started working on self-driving cars at "
"Google in 2007, few people outside of the company took him "
"seriously. “I can tell you very senior CEOs of major American "
"car companies would shake my hand and turn away because I wasn’t "
"worth talking to,” said Thrun, in an interview with Recode earlier "
"this week.")
doc = nlp(text)
# Analyse syntax
print("Noun phrases:", [chunk.text for chunk in doc.noun_chunks])
print("Verbs:", [token.lemma_ for token in doc if token.pos_ == "VERB"])



"""#C] Named Entity recognition with diagram using NLTK corpus – treebank.**bold text**"""

#for IDLE DIGRAM
import nltk

nltk.download('treebank')

from nltk.corpus import treebank_chunk

print(treebank_chunk.tagged_sents()[0])

chunked = treebank_chunk.chunked_sents()[0]
print(chunked)

chunked.draw()

import nltk
nltk.download('treebank')
from nltk.corpus import treebank_chunk
print(treebank_chunk.tagged_sents()[0])
print(treebank_chunk.chunked_sents()[0])
# treebank_chunk.chunked_sents()[0].draw()



"""#NER


"""

from spacy import displacy
NER=spacy.load("en_core_web_sm")
text=("When Sebastian Thrun started working on self-driving cars at "
"Google in 2007, few people outside of the company took him "
"seriously. “I can tell you very senior CEOs of major American "
"car companies would shake my hand and turn away because I wasn’t "
"worth talking to,” said Thrun, in an interview with Recode earlier "
"this week.")
ner=NER(text)
for word in ner.ents:
    print(word.text,word.label_)

displacy.render(ner,style="ent",jupyter=True)

