Skip to content

A real-world example NER example Chapter 03 #10

Description

@mauuuuu5

Hi everyone I am copying from the book the code that takes the NY times article but I cannot get the book's output and also the code is not in chapter 03

from bs4 import BeautifulSoup
import requests
import spacy
def url_text(url_string):
res = requests.get(url_string)
html = res.text
soup = BeautifulSoup(html, 'html5lib')
for script in soup(["script", "style", 'aside']):
script.extract()
text = soup.get_text()
return " ".join(text.split())
ny_art = url_text("https://www.nytimes.com/2021/01/12/opinion/trump-america-allies.html")
nlp = spacy.load("en_core_web_md")
doc = nlp(ny_art)
len(doc.ents)
from collections import Counter
labels = [ent.label_ for ent in doc.ents]
Counter(labels)

Thank you

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions