import pandas as pd
import nltk
sentence = "In 2012, Hillary Clinton sent some email messages."
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('maxent_ne_chunker')
nltk.download('words')
!pip install svgling
entities
entities.pprint()
type(entities)
for entity in entities.subtrees():
if entity.label() == "PERSON":
name = []
for leaf in entity.leaves():
name.append(leaf[0])
print(" ".join(name))
sentence = "In a rambling speech early Thursday, full of festering historical grievances and accusations of a relentless Western plot against his country, Mr. Putin reminded the world that Russia “remains one of the most powerful nuclear states” with “a certain advantage in several cutting edge weapons.”"
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
for entity in entities.subtrees():
if entity.label() == "PERSON":
name = []
for leaf in entity.leaves():
name.append(leaf[0])
print(" ".join(name))
sentence1 = "The White House tweeted Thursday morning that President Biden had “met with his national security team in the Situation Room,” where they “discussed how we will hold Russia accountable for its unprovoked and unjustified attack on Ukraine.” Mr. Biden is expected to deliver a speech to the American people later in the day.”"
tokens = nltk.word_tokenize(sentence1)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
for entity in entities.subtrees():
if entity.label() == "PERSON":
name = []
for leaf in entity.leaves():
name.append(leaf[0])
print(" ".join(name))
sentence2 = """British Prime Minister Boris Johnson told German Chancellor Olaf Scholz that Western "inaction or underreaction" in response to Russia invading Ukraine would have unthinkable consequences, a spokeswoman for his office said on Thursday. Johnson held calls with Norway's Prime Minister Jonas Gahr Stoere and Germany's Scholz to discuss sanctions after Russian forces invaded Ukraine in a massed assault by land, sea and air."""
tokens = nltk.word_tokenize(sentence2)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
for entity in entities.subtrees():
if entity.label() == "PERSON":
name = []
for leaf in entity.leaves():
name.append(leaf[0])
print(" ".join(name))
entities
# from https://www.reuters.com/markets/commodities/already-peril-biden-climate-agenda-faces-us-supreme-court-test-2022-02-24/
texts = pd.DataFrame({'text':[
"""President Joe Biden's climate-related agenda, already under threat due to congressional failure to pass key legislation, now faces the prospect of a hostile reception at the U.S. Supreme Court that could have lasting consequences on the use of federal power to tackle environmental issues.""",
"""The court's 6-3 conservative majority, suspicious of broad federal agency power, will weigh at oral arguments next Monday the Environmental Protection Agency's (EPA) authority to regulate greenhouse gas emissions from existing coal- and gas-fired power plants under the landmark Clean Air Act.""",
"""An eventual ruling restricting EPA authority could hobble the administration's ability to curb the power sector's emissions - representing about a quarter of U.S. greenhouse gases.""",
""""Could it be damaging? If it's an adverse decision, of course it could be," John Kerry, the Biden administration's special envoy on climate change, told Reuters.""",
"""The United States, trailing only China in greenhouse gas emissions, is a crucial player in global efforts to combat climate change.""",
"""The case before the Supreme Court was brought by Republican-led states led by coal producer West Virginia. Other challengers include coal companies and coal-friendly industry groups. Coal is among the most greenhouse gas-intensive fuels.""",
"""Democratic-led states and major power companies including Consolidated Edison Inc(ED.N), Exelon Corp (EXC.O) and PG&E Corp (PCG.N) sided with Biden's administration, as did the Edison Electric Institute, an investor-owned utility trade group. The utility industry believes regulatory certainty will help companies devise investment plans.""",
"""The justices will review the U.S. Court of Appeals for the District of Columbia Circuit's 2021 ruling striking down Republican former President Donald Trump's Affordable Clean Energy rule. That regulation would have imposed limits on a Clean Air Act provision called Section 111 that gives the EPA authority to regulate emissions from existing power plants.""",
"""The rule proposed by Trump, a supporter of the U.S. coal industry who also questioned climate change science, was meant to replace Democratic former President Barack Obama's Clean Power Plan requiring big cuts in carbon emissions from the power industry."""]})
texts
def text1(sentence):
list1 = []
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
for entity in entities.subtrees():
if entity.label() == "PERSON":
name = []
for leaf in entity.leaves():
name.append(leaf[0])
list1.append((" ".join(name)))
return list1
texts['persons'] = texts['text'].apply(text1)
texts