Exemple #1
0
    def load_alias(self, filename: str) -> None:
        """
        Load aliases from file.

        Args:
            filename (str): Filename to read aliases from
        """
        with open(filename, "r") as f:

            for line in f.readlines():
                # Remove comments (starting with '#'), unless they are escaped
                line = re.sub(r"(?<!\\)#.*", "", line)

                if not line.strip():
                    continue  # Skip empty lines

                # Get key and aliases
                primary, aliases = parse_aliases(line)

                for name in [primary] + aliases:
                    # Stor a separate entry for each type of key_mod
                    # For each entry, also store the value itself and the
                    # primary name

                    self.vocab["none"][name] = addict.Dict(value=name,
                                                           primary=primary)
                    self.vocab["lower"][name.lower()] = addict.Dict(
                        value=name, primary=primary)
                    self.vocab["stem"][self.stemmer(name)] = addict.Dict(
                        value=name, primary=primary)

                    self.vocab["norm"][aliasregex.normalize(
                        name)] = addict.Dict(value=name, primary=primary)
Exemple #2
0
def normalize_ta(name: Text, uppercase_abbr: List[Text]) -> Text:
    """Normalize TA names. Words are capitalized unless configured
    to be excempt"""

    res: Text = normalize(
        name,
        capitalize=True,
        uppercase_abbr=uppercase_abbr,
    )

    return res
Exemple #3
0
def test_aliasregex_normalization() -> None:
    "Normalize test"

    assert normalize("oceanlotusGroup") == "oceanlotus group"
    assert normalize("APT 27") == "apt 27"
    assert normalize("APT.27") == "apt 27"
    assert normalize("APT-27") == "apt 27"
    assert normalize("APT- 27") == "apt 27"
    assert normalize("winntiGroup") == "winnti group"
Exemple #4
0
    def get(self,
            key: str,
            key_mod: str = "DEFAULT",
            primary: Optional[bool] = None,
            default: Optional[str] = None) -> Optional[str]:
        """
        Search for entry in vocabulary

        Args:
            key (str):           Key to search for
            key_mod (str):       Key modifier ("lower", "stem" or "none"). If not set,
                                 use default value form self.config.key_mod.
            primary (bool|None): If True, return primary name, if not return the
                                 value itself from the vocabulary
            default (str|None):  Default value if key is not found. Default == None
        """

        key_mod = self.get_key_mod(key_mod)

        # If primary is not set, get from config whether we should retrieve the primary name
        if primary is None:
            primary = self.config.primary

        # If default is None, get default value from config
        if default is None:
            default = self.config.default

        if key_mod == "stem":
            key = self.stemmer(key)
        elif key_mod == "lower":
            key = key.lower()
        elif key_mod == "norm":
            key = aliasregex.normalize(key)

        value = self.vocab[key_mod].get(key)

        if not value:
            return default

        if primary:  # Return primary value
            return value.primary  # type: ignore

        # Return value ifself
        return value.value  # type: ignore
Exemple #5
0
def normalize_ta(name: Text) -> Text:
    return normalize(
        name,
        capitalize=True,
        uppercase_abbr=["APT", "BRONZE", "IRON", "GOLD"],
    )