from bs4 import BeautifulSoup
import re


def _text_cleaner(text):
    """Text cleaning function. Removes HTML tags, escaped characters (e.g. \n)
    and removes NewsAPI text patterns and URLs

    Args:
        text (string): text of a news article

    Returns:
        string: cleaned text
    """
    # Removes HTML tags
    text = BeautifulSoup(text, features="lxml").get_text()
    # Remove escaped characters
    escapes = "".join([chr(char) for char in range(1, 32)])
    text = text.translate(str.maketrans("", "", escapes))
    # Remove patterns
    expressions = ["… [+ [0-9]*chars]$", "…", "https?://\S+"]
    for i in expressions:
        text = re.sub(i, "", text)
    return text