<commit_before>"""
twosheds.transformation
~~~~~~~~~~~~~~~~~~~~~~~

This module implements a number of grammatical transformations which are
used when parsing commands.

A grammatical transformation (or transformation) operates on a given
string with a given constituent structure and converts it into a new
string with a new derived constituent structure.
"""
import os

class Transformation(object):
    def expand(self, sentece):
        raise NotImplementedError("Transformations must implement ``expand``.")


class AliasTransformation(Transformation):
    """Expands user-defined aliases."""
    def __init__(self, aliases):
        self.aliases = aliases or {}

    def expand(self, sentence):
        """Expand aliases in a sentence.
        
        A token is a candidate for alias expansion only if it it the first
        token in a sentence.
        """
        try:
            command, args = sentence.split(" ", 1)
        except ValueError:
            command, args = sentence, ""
        try:
            return "%s %s" % (self.aliases[command], args)
        except KeyError:
            return sentence


class VariableTransformation(Transformation):
    """Expands environmental variables."""
    def expand(self, sentence):
        """Expand environmental variables in a sentence."""
        tokens = sentence.split()
        new_tokens = []
        for token in tokens:
            if token.startswith("$"):
                try:
                    token = os.environ[token[1:]]
                except KeyError:
                    pass
            new_tokens.append(token)
        return " ".join(new_tokens)


class TildeTransformation(Transformation):
    """Expands ``~`` to ``$HOME``"""
    def expand(self, sentence):
        return sentence.replace("~", "$HOME")<commit_msg>Expand environmental variables properly<commit_after>"""
twosheds.transformation
~~~~~~~~~~~~~~~~~~~~~~~

This module implements a number of grammatical transformations which are
used when parsing commands.

A grammatical transformation (or transformation) operates on a given
string with a given constituent structure and converts it into a new
string with a new derived constituent structure.
"""
import os

class Transformation(object):
    def expand(self, sentece):
        raise NotImplementedError("Transformations must implement ``expand``.")


class AliasTransformation(Transformation):
    """Expands user-defined aliases."""
    def __init__(self, aliases):
        self.aliases = aliases or {}

    def expand(self, sentence):
        """Expand aliases in a sentence.
        
        A token is a candidate for alias expansion only if it it the first
        token in a sentence.
        """
        try:
            command, args = sentence.split(" ", 1)
        except ValueError:
            command, args = sentence, ""
        try:
            return "%s %s" % (self.aliases[command], args)
        except KeyError:
            return sentence


class VariableTransformation(Transformation):
    """Expands environmental variables."""
    def expand(self, sentence):
        """Expand environmental variables in a sentence."""
        for k, v in os.environ.iteritems():
            sentence = sentence.replace("$" + k, v)
        return sentence


class TildeTransformation(Transformation):
    """Expands ``~`` to ``$HOME``"""
    def expand(self, sentence):
        return sentence.replace("~", "$HOME")
{'fraction_non_alphanumeric': 0.110527850685331, 'fraction_numerical': 0.0008748906386701663, 'mean_word_length': 2.736383442265795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3714245', 'n_tokens_mistral': 864, 'n_tokens_neox': 827, 'n_words': 307}