HTML Entity Parser

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def entityParser(self, text: str) -> str:
        if text is None or len(text) == 0:
            return ""

        # Mapping of HTML entities to their corresponding characters
        entities = {
            """: '"',
            "'": "'",
            ">": '>',
            "&lt;": '<',
            "&frasl;": '/',
        }

        # Iterate through the entities and replace them in the text
        for entity, char in entities.items():
            text = text.replace(entity, char)

        return text.replace("&amp;", "&")  # "&" must be put in last;