# -*- coding: utf-8 -*-
"""names_orig_spacy.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/15sQmoJNlMVMLkh9vl8eQk3meZ9V6L6mV
"""

TRAIN_DATA=[('Shri Narendra Modi,Ministry of Personnel, Public Grievances and PensionsDepartment of Atomic EnergyDepartment of Space', {'entities': [(19, 118, 'Label')]}), ('Shri Raj Nath Singh,Ministry of Defence  Contact Facebook Account Twitter Account', {'entities': [(20, 39, 'Label')]}), ('Shri Amit Shah,Ministry of Home Affairs  Contact Facebook Account Twitter Account', {'entities': [(15, 39, 'Label')]}), ('Ministry of Home Affairs  Contact Facebook Account Twitter Account,Shri Amit Shah', {'entities': [(0, 24, 'Label')]}), ('Shri Nitin Jairam Gadkari,Ministry of Road Transport and HighwaysMinistry of Micro, Small and Medium Enterprises  Contact Facebook Account Twitter Account', {'entities': [(26, 112, 'Label')]}), ('Ministry of Road Transport and HighwaysMinistry of Micro, Small and Medium Enterprises  Contact Facebook Account Twitter Account,Shri Nitin Jairam Gadkari', {'entities': [(0, 87, 'Label')]}), ('Shri D.V. Sadananda Gowda,Ministry of Chemicals and Fertilizers  Contact Facebook Account Twitter Account', {'entities': [(26, 63, 'Label')]}), ('Ministry of Chemicals and Fertilizers  Contact Facebook Account Twitter Account,Shri D.V. Sadananda Gowda', {'entities': [(0, 37, 'Label')]}), ('Smt. Nirmala Sitharaman,Ministry of FinanceMinistry of Corporate Affairs  Contact Facebook Account Twitter Account', {'entities': [(24, 72, 'Label')]}), ('Ministry of FinanceMinistry of Corporate Affairs  Contact Facebook Account Twitter Account,Smt. Nirmala Sitharaman', {'entities': [(0, 48, 'Label')]}), ('Shri Ramvilas Paswan,Ministry of Consumer Affairs, Food and Public Distribution  Contact Facebook Account Twitter Account', {'entities': [(21, 80, 'Label')]}), ('Ministry of Consumer Affairs, Food and Public Distribution  Contact Facebook Account Twitter Account,Shri Ramvilas Paswan', {'entities': [(0, 59, 'Label')]}), ('Shri Narendra Singh Tomar,Ministry of Agriculture & Farmers WelfareMinistry of Rural DevelopmentMinistry of Panchayati Raj  Contact Facebook Account Twitter Account', {'entities': [(26, 122, 'Label')]}), ('Ministry of Agriculture & Farmers WelfareMinistry of Rural DevelopmentMinistry of Panchayati Raj  Contact Facebook Account Twitter Account,Shri Narendra Singh Tomar', {'entities': [(0, 96, 'Label')]}), ('Shri Ravi Shankar Prasad,Ministry of Law and JusticeMinistry of CommunicationsMinistry of Electronics and Information Technology  Contact Facebook Account Twitter Account', {'entities': [(25, 128, 'Label')]}), ('Ministry of Law and JusticeMinistry of CommunicationsMinistry of Electronics and Information Technology  Contact Facebook Account Twitter Account,Shri Ravi Shankar Prasad', {'entities': [(0, 103, 'Label')]}), ('Smt. Harsimrat Kaur Badal,Ministry of Food Processing Industries  Contact Facebook Account Twitter Account', {'entities': [(26, 64, 'Label')]}), ('Ministry of Food Processing Industries  Contact Facebook Account Twitter Account,Smt. Harsimrat Kaur Badal', {'entities': [(0, 38, 'Label')]}), ('Shri Thaawar Chand Gehlot,Ministry of Social Justice and Empowerment  Contact Facebook Account Twitter Account', {'entities': [(26, 68, 'Label')]}), ('Ministry of Social Justice and Empowerment  Contact Facebook Account Twitter Account,Shri Thaawar Chand Gehlot', {'entities': [(0, 42, 'Label')]}), ('Dr. Subrahmanyam Jaishankar,Ministry of External Affairs  Contact Twitter Account', {'entities': [(28, 56, 'Label')]}), ('Ministry of External Affairs  Contact Twitter Account,Dr. Subrahmanyam Jaishankar', {'entities': [(0, 29, 'Label')]}), ("Shri Ramesh Pokhriyal 'Nishank',Ministry of Education  Contact Facebook Account Twitter Account", {'entities': [(32, 53, 'Label')]}), ("Ministry of Education  Contact Facebook Account Twitter Account,Shri Ramesh Pokhriyal 'Nishank'", {'entities': [(0, 22, 'Label')]}), ('Shri Arjun Munda,Ministry of Tribal Affairs  Contact Facebook Account Twitter Account', {'entities': [(17, 43, 'Label')]}), ('Ministry of Tribal Affairs  Contact Facebook Account Twitter Account,Shri Arjun Munda', {'entities': [(0, 27, 'Label')]}), ('Smt. Smriti Zubin Irani,Ministry of Women and Child DevelopmentMinistry of Textiles  Contact Facebook Account Twitter Account', {'entities': [(24, 83, 'Label')]}), ('Ministry of Women and Child DevelopmentMinistry of Textiles  Contact Facebook Account Twitter Account,Smt. Smriti Zubin Irani', {'entities': [(0, 59, 'Label')]}), ('Dr. Harsh Vardhan,Ministry of Health and Family WelfareMinistry of Science and TechnologyMinistry of Earth Sciences  Contact Facebook Account Twitter Account', {'entities': [(18, 115, 'Label')]}), ('Ministry of Health and Family WelfareMinistry of Science and TechnologyMinistry of Earth Sciences  Contact Facebook Account Twitter Account,Dr. Harsh Vardhan', {'entities': [(0, 98, 'Label')]}), ('Shri Prakash Javadekar,Ministry of Environment, Forest and Climate ChangeMinistry of Information and BroadcastingMinistry of Heavy Industries and Public Enterprises  Contact Facebook Account Twitter Account', {'entities': [(23, 165, 'Label')]}), ('Ministry of Environment, Forest and Climate ChangeMinistry of Information and BroadcastingMinistry of Heavy Industries and Public Enterprises  Contact Facebook Account Twitter Account,Shri Prakash Javadekar', {'entities': [(0, 141, 'Label')]}), ('Shri Piyush Goyal,Ministry of RailwaysMinistry of Commerce and Industry  Contact Facebook Account Twitter Account', {'entities': [(18, 72, 'Label')]}), ('Ministry of RailwaysMinistry of Commerce and Industry  Contact Facebook Account Twitter Account,Shri Piyush Goyal', {'entities': [(0, 53, 'Label')]}), ('Shri Dharmendra Pradhan,Ministry of Petroleum and Natural GasMinistry of Steel  Contact Facebook Account Twitter Account', {'entities': [(24, 78, 'Label')]}), ('Ministry of Petroleum and Natural GasMinistry of Steel  Contact Facebook Account Twitter Account,Shri Dharmendra Pradhan', {'entities': [(0, 54, 'Label')]}), ('Shri Mukhtar Abbas Naqvi,Ministry of Minority Affairs  Contact Facebook Account Twitter Account', {'entities': [(25, 53, 'Label')]}), ('Ministry of Minority Affairs  Contact Facebook Account Twitter Account,Shri Mukhtar Abbas Naqvi', {'entities': [(0, 28, 'Label')]}), ('Shri Pralhad Joshi,Ministry of Parliamentary AffairsMinistry of CoalMinistry of Mines  Contact Facebook Account', {'entities': [(19, 85, 'Label')]}), ('Ministry of Parliamentary AffairsMinistry of CoalMinistry of Mines  Contact Facebook Account,Shri Pralhad Joshi', {'entities': [(0, 66, 'Label')]}), ('Dr. Mahendra Nath Pandey,Ministry of Skill Development and Entrepreneurship  Contact Facebook Account Twitter Account', {'entities': [(25, 76, 'Label')]}), ('Ministry of Skill Development and Entrepreneurship  Contact Facebook Account Twitter Account,Dr. Mahendra Nath Pandey', {'entities': [(0, 50, 'Label')]}), ('Shri Giriraj Singh,Ministry of Animal Husbandry, Dairying and Fisheries  Contact Facebook Account Twitter Account', {'entities': [(19, 71, 'Label')]}), ('Ministry of Animal Husbandry, Dairying and Fisheries  Contact Facebook Account Twitter Account,Shri Giriraj Singh', {'entities': [(0, 52, 'Label')]}), ('Shri Gajendra Singh Shekhawat,Ministry of Jal Shakti  Contact Facebook Account Twitter Account', {'entities': [(30, 52, 'Label')]}), ('Ministry of Jal Shakti  Contact Facebook Account Twitter Account,Shri Gajendra Singh Shekhawat', {'entities': [(0, 22, 'Label')]}), (',Ministry of Labour and Employment', {'entities': [(1, 34, 'Label')]}), ('Ministry of Labour and Employment,', {'entities': [(0, 33, 'Label')]}), ('Shri Santosh Kumar Gangwar,Ministry of Statistics and Programme ImplementationMinistry of Planning', {'entities': [(27, 98, 'Label')]}), ('Ministry of Statistics and Programme ImplementationMinistry of Planning,Shri Santosh Kumar Gangwar', {'entities': [(0, 71, 'Label')]}), ('Shri Rao Inderjit Singh,Ministry of Ayurveda, Yoga and Naturopathy, Unani, Siddha and Homoeopathy (AYUSH)', {'entities': [(24, 105, 'Label')]}), ('Ministry of Ayurveda, Yoga and Naturopathy, Unani, Siddha and Homoeopathy (AYUSH),Shri Rao Inderjit Singh', {'entities': [(0, 81, 'Label')]}), ('Shri Shripad Yesso Naik,Ministry of Development of North Eastern Region', {'entities': [(24, 71, 'Label')]}), ('Ministry of Development of North Eastern Region,Shri Shripad Yesso Naik', {'entities': [(0, 47, 'Label')]}), ('Dr. Jitendra Singh,Ministry of Youth Affairs and Sports', {'entities': [(19, 55, 'Label')]}), ('Ministry of Youth Affairs and Sports,Dr. Jitendra Singh', {'entities': [(0, 36, 'Label')]}), ('Shri Kiren Rijiju,Ministry of CultureMinistry of Tourism', {'entities': [(18, 56, 'Label')]}), ('Ministry of CultureMinistry of Tourism,Shri Kiren Rijiju', {'entities': [(0, 38, 'Label')]}), ('Shri Prahalad Singh Patel,Ministry of PowerMinistry of New and Renewable Energy', {'entities': [(26, 79, 'Label')]}), ('Ministry of PowerMinistry of New and Renewable Energy,Shri Prahalad Singh Patel', {'entities': [(0, 53, 'Label')]}), ('Shri Raj Kumar Singh,Ministry of Housing and Urban AffairsMinistry of Civil Aviation', {'entities': [(21, 84, 'Label')]}), ('Ministry of Housing and Urban AffairsMinistry of Civil Aviation,Shri Raj Kumar Singh', {'entities': [(0, 63, 'Label')]}), ('Shri Hardeep Singh Puri,Ministry of Shipping', {'entities': [(24, 44, 'Label')]}), ('Ministry of Shipping,Shri Hardeep Singh Puri', {'entities': [(0, 20, 'Label')]}), ('Shri Mansukh L. Mandaviya,Ministry of Defence', {'entities': [(26, 45, 'Label')]}), ('Ministry of Defence,Shri Mansukh L. Mandaviya', {'entities': [(0, 19, 'Label')]}), (",Prime Minister's OfficeMinistry of Personnel, Public Grievances and PensionsDepartment of Atomic EnergyDepartment of Space", {'entities': [(24, 123, 'Label')]}), ("Prime Minister's OfficeMinistry of Personnel, Public Grievances and PensionsDepartment of Atomic EnergyDepartment of Space,", {'entities': [(23, 122, 'Label')]}), ('Shri Shripad Yesso Naik,Ministry of Minority Affairs', {'entities': [(24, 52, 'Label')]}), ('Ministry of Minority Affairs,Shri Shripad Yesso Naik', {'entities': [(0, 28, 'Label')]}), ('Dr. Jitendra Singh,Ministry of Skill Development and Entrepreneurship', {'entities': [(19, 69, 'Label')]}), ('Ministry of Skill Development and Entrepreneurship,Dr. Jitendra Singh', {'entities': [(0, 50, 'Label')]}), ('Shri Kiren Rijiju,Ministry of Commerce and Industry', {'entities': [(18, 51, 'Label')]}), ('Ministry of Commerce and Industry,Shri Kiren Rijiju', {'entities': [(0, 33, 'Label')]}), ('Shri Raj Kumar Singh,Ministry of Chemicals and Fertilizers', {'entities': [(21, 58, 'Label')]}), ('Ministry of Chemicals and Fertilizers,Shri Raj Kumar Singh', {'entities': [(0, 37, 'Label')]}), ('Shri Hardeep Singh Puri,Ministry of Steel', {'entities': [(24, 41, 'Label')]}), ('Ministry of Steel,Shri Hardeep Singh Puri', {'entities': [(0, 17, 'Label')]}), ('Shri Mansukh L. Mandaviya,Ministry of Health and Family Welfare', {'entities': [(26, 63, 'Label')]}), ('Ministry of Health and Family Welfare,Shri Mansukh L. Mandaviya', {'entities': [(0, 37, 'Label')]}), ('Shri Faggansingh Kulaste,Ministry of Parliamentary AffairsMinistry of Heavy Industries and Public Enterprises', {'entities': [(25, 109, 'Label')]}), ('Ministry of Parliamentary AffairsMinistry of Heavy Industries and Public Enterprises,Shri Faggansingh Kulaste', {'entities': [(0, 84, 'Label')]}), ('Shri Ashwini Kumar Choubey,Ministry of Road Transport and Highways', {'entities': [(27, 66, 'Label')]}), ('Ministry of Road Transport and Highways,Shri Ashwini Kumar Choubey', {'entities': [(0, 39, 'Label')]}), ('Shri Arjun Ram Meghwal,Ministry of Social Justice and Empowerment', {'entities': [(23, 65, 'Label')]}), ('Ministry of Social Justice and Empowerment,Shri Arjun Ram Meghwal', {'entities': [(0, 42, 'Label')]}), ('General (Retd.) V.K. Singh,Ministry of Consumer Affairs, Food and Public Distribution', {'entities': [(27, 85, 'Label')]}), ('Ministry of Consumer Affairs, Food and Public Distribution,General (Retd.) V.K. Singh', {'entities': [(0, 58, 'Label')]}), ('Shri Krishan Pal,Ministry of Home Affairs', {'entities': [(17, 41, 'Label')]}), ('Ministry of Home Affairs,Shri Krishan Pal', {'entities': [(0, 24, 'Label')]}), ('Shri Danve Raosaheb Dadarao,Ministry of Agriculture & Farmers Welfare', {'entities': [(28, 69, 'Label')]}), ('Ministry of Agriculture & Farmers Welfare,Shri Danve Raosaheb Dadarao', {'entities': [(0, 41, 'Label')]}), ('Shri G. Kishan Reddy,Ministry of Social Justice and Empowerment', {'entities': [(21, 63, 'Label')]}), ('Ministry of Social Justice and Empowerment,Shri G. Kishan Reddy', {'entities': [(0, 42, 'Label')]}), ('Shri Parshottam Rupala,Ministry of Rural Development', {'entities': [(23, 52, 'Label')]}), ('Ministry of Rural Development,Shri Parshottam Rupala', {'entities': [(0, 29, 'Label')]}), ('Shri Ramdas Athawale,Ministry of Environment, Forest and Climate Change', {'entities': [(21, 71, 'Label')]}), ('Ministry of Environment, Forest and Climate Change,Shri Ramdas Athawale', {'entities': [(0, 50, 'Label')]}), ('Sadhvi Niranjan Jyoti,Ministry of Animal Husbandry, Dairying and Fisheries', {'entities': [(22, 74, 'Label')]}), ('Ministry of Animal Husbandry, Dairying and Fisheries,Sadhvi Niranjan Jyoti', {'entities': [(0, 52, 'Label')]}), ('Shri Babul Supriyo,Ministry of EducationMinistry of CommunicationsMinistry of Electronics and Information Technology', {'entities': [(19, 116, 'Label')]}), ('Ministry of EducationMinistry of CommunicationsMinistry of Electronics and Information Technology,Shri Babul Supriyo', {'entities': [(0, 97, 'Label')]}), ('Shri Sanjeev Kumar Balyan,Ministry of FinanceMinistry of Corporate Affairs', {'entities': [(26, 74, 'Label')]}), ('Ministry of FinanceMinistry of Corporate Affairs,Shri Sanjeev Kumar Balyan', {'entities': [(0, 48, 'Label')]}), ('Shri Dhotre Sanjay Shamrao,Ministry of Railways', {'entities': [(27, 47, 'Label')]}), ('Ministry of Railways,Shri Dhotre Sanjay Shamrao', {'entities': [(0, 20, 'Label')]}), ('Shri Anurag Singh Thakur,Ministry of Home Affairs', {'entities': [(25, 49, 'Label')]}), ('Ministry of Home Affairs,Shri Anurag Singh Thakur', {'entities': [(0, 24, 'Label')]}), ('Shri Angadi Suresh Channabasappa,Ministry of Jal ShaktiMinistry of Social Justice and Empowerment', {'entities': [(33, 97, 'Label')]}), ('Ministry of Jal ShaktiMinistry of Social Justice and Empowerment,Shri Angadi Suresh Channabasappa', {'entities': [(0, 64, 'Label')]}), ('Shri Nityanand Rai,Ministry of External AffairsMinistry of Parliamentary Affairs', {'entities': [(19, 80, 'Label')]}), ('Ministry of External AffairsMinistry of Parliamentary Affairs,Shri Nityanand Rai', {'entities': [(0, 61, 'Label')]}), ('Shri Rattan Lal Kataria,Ministry of Tribal Affairs', {'entities': [(24, 50, 'Label')]}), ('Ministry of Tribal Affairs,Shri Rattan Lal Kataria', {'entities': [(0, 26, 'Label')]}), ('Shri V. Muraleedharan,Ministry of Commerce and Industry', {'entities': [(22, 55, 'Label')]}), ('Ministry of Commerce and Industry,Shri V. Muraleedharan', {'entities': [(0, 33, 'Label')]}), ('Smt. Renuka Singh Saruta,Ministry of Food Processing Industries', {'entities': [(25, 63, 'Label')]}), ('Ministry of Food Processing Industries,Smt. Renuka Singh Saruta', {'entities': [(0, 38, 'Label')]}), ('Shri Som Parkash,Ministry of Micro, Small and Medium EnterprisesMinistry of Animal Husbandry, Dairying and Fisheries', {'entities': [(17, 116, 'Label')]}), ('Ministry of Micro, Small and Medium EnterprisesMinistry of Animal Husbandry, Dairying and Fisheries,Shri Som Parkash', {'entities': [(0, 99, 'Label')]}), ('Shri Rameswar Teli,Ministry of Agriculture & Farmers Welfare', {'entities': [(19, 60, 'Label')]}), ('Ministry of Agriculture & Farmers Welfare,Shri Rameswar Teli', {'entities': [(0, 41, 'Label')]}), ('Shri Pratap Chandra Sarangi,Ministry of Women and Child Development', {'entities': [(28, 67, 'Label')]}), ('Ministry of Women and Child Development,Shri Pratap Chandra Sarangi']

from spacy import displacy
import warnings
import spacy
import random
import time
import numpy as np
from spacy.util import minibatch, compounding
import os
from pathlib import Path
warnings.filterwarnings("ignore")
nlp = spacy.load('en')

test_sentences = [x[0] for x in TRAIN_DATA[0:15]] # extract the sentences from [sentence, entity]
for x in test_sentences:

    doc = nlp(x)
    #print(doc)
    displacy.render(doc, jupyter = True, style = "ent")
warnings.filterwarnings("default")

[x[0] for x in TRAIN_DATA[120:170]]

def get_names(t):
    doc = nlp(t)
    name=[]
    rest=[]
    #print("Entities", [(ent.text, ent.label_) for ent in doc.ents])
    for ent in doc.ents:
        if ent.label_=='PERSON':
            name.append(ent.text)

            return name
    return 0
get_names('Prime Minister of Europe')

output_dir = Path('/content/Model')
nlp.to_disk(output_dir)
print("Saved model to", output_dir)


