HTMLStripNormalizer#

class scikitplot.corpus.HTMLStripNormalizer(use_beautifulsoup=False, parser='html.parser', decode_entities=True)[source]#

Remove HTML and XML tags from the document text.

Two modes are available:

  • use_beautifulsoup=False (default): regex-based stripping. Zero additional dependencies; handles well-formed HTML.

  • use_beautifulsoup=True: uses bs4.BeautifulSoup for robust parsing of malformed or deeply nested HTML. Requires pip install beautifulsoup4.

Parameters:
use_beautifulsoupbool, optional

Use BeautifulSoup for parsing. Default: False.

parserstr, optional

BeautifulSoup parser (\"html.parser\", \"lxml\", \"html5lib\"). Ignored when use_beautifulsoup=False. Default: \"html.parser\" (stdlib, no extra deps).

decode_entitiesbool, optional

Decode HTML entities (e.g. & → &). Default: True.

Parameters:
  • use_beautifulsoup (bool)

  • parser (str)

  • decode_entities (bool)

Notes

Regex mode: comments go first (<!-- ... -->, even holding <), then every <...> holding no other <. So a lone < in text keeps what follows it (x < y and <b>b</b> gives x < y and b), and the cost is linear in the text however many < it holds. A tag whose attribute value holds an unescaped < (title="a < b") is not recognised and leaves a fragment; use use_beautifulsoup=True for markup that needs exact parsing.

Examples

>>> norm = HTMLStripNormalizer()
>>> doc = CorpusDocument.create("f.txt", 0, "<p>Hello <b>world</b>.</p>")
>>> norm.normalize_doc(doc).normalized_text
'Hello world.'
normalize_doc(doc)[source]#

Apply normalize_text to the document.

Parameters:
docCorpusDocument

Input document.

Returns:
CorpusDocument

New instance with normalized_text updated. text is never modified.

Parameters:

doc (CorpusDocument)

Return type:

CorpusDocument

normalize_text(text)[source]#

Strip HTML tags from the text.

Parameters:
textstr

The text to normalise.

Returns:
str
Raises:
ImportError

If use_beautifulsoup=True and beautifulsoup4 is not installed.

Parameters:

text (str)

Return type:

str