Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make normalize_whitespace faster #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions breadability/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def get_link_density(node, node_text=None):
"""
if node_text is None:
node_text = node.text_content()
node_text = normalize_whitespace(node_text.strip())
node_text = normalize_whitespace(node_text)

text_length = len(node_text)
if text_length == 0:
Expand All @@ -101,7 +101,7 @@ def get_link_density(node, node_text=None):


def _get_normalized_text_length(node):
return len(normalize_whitespace(node.text_content().strip()))
return len(normalize_whitespace(node.text_content()))


def get_class_weight(node):
Expand Down
19 changes: 2 additions & 17 deletions breadability/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ def ignored(*exceptions):
pass


MULTIPLE_WHITESPACE_PATTERN = re.compile(r"\s+", re.UNICODE)


def is_blank(text):
"""
Returns ``True`` if string contains only whitespace characters
Expand All @@ -29,26 +26,14 @@ def is_blank(text):
return not text or text.isspace()


def shrink_text(text):
return normalize_whitespace(text.strip())


def normalize_whitespace(text):
"""
Translates multiple whitespace into single space character.
If there is at least one new line character chunk is replaced
by single LF (Unix new line) character.
"""
return MULTIPLE_WHITESPACE_PATTERN.sub(_replace_whitespace, text)

return ' '.join(text.split())

def _replace_whitespace(match):
text = match.group()

if "\n" in text or "\r" in text:
return "\n"
else:
return " "
shrink_text = normalize_whitespace


def cached_property(getter):
Expand Down