<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
# Imports =====================================================================


# Functions & objects =========================================================
def _params_or_none(params):
    return params if params else None


def neighbours_pattern(element):  #TODO: test
    def neighbour_to_params(neig_type, neighbour):
        neighbour_data = None
        if neighbour.isTag():
            neighbour_data = (
                neig_type + "_neighbour_tag",
                0,
                [
                    neighbour.getTagName(),
                    _params_or_none(neighbour.params),
                    neighbour.getContent().strip()
                ]
            )
        else:
            neighbour_data = (
                neig_type + "_neighbour_tag",
                0,
                [None, None, neighbour.getContent().strip()]
            )

        return neighbour_data

    # check if there are any neighbours
    if not element.parent or len(element.parent.childs) <= 1:
        return []

    parent = element.parent
    neighbours = parent.childs
    element_index = neighbours.index(element)

    output = []

    # pick left neighbour
    if element_index >= 1:
        output.append(
            neighbour_to_params("left", neighbours[element_index - 1])
        )

    # pick right neighbour
    if element_index < len(neighbours) - 2:
        output.append(
            neighbour_to_params("right", neighbours[element_index + 1])
        )

    return output


def predecesors_pattern(element, root):
    if not element.parent or not element.parent.parent:
        return []

    trail = [
        [
            element.parent.parent.getTagName(),
            _params_or_none(element.parent.parent.params)
        ],
        [
            element.parent.getTagName(),
            _params_or_none(element.parent.params)
        ],
        [element.getTagName(), _params_or_none(element.params)],
    ]

    match = root.match(*trail)
    if element in match:
        return [
            ("match", match.index(element), trail)
        ]
<commit_msg>#20: Added class PathCall().<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
# Imports =====================================================================


# Functions & objects =========================================================
class PathCall(object):
    def __init__(self, call_type, index, parameters):
        self.call_type = call_type
        self.index = index
        self.parameters = parameters


def _params_or_none(params):
    return params if params else None


def neighbours_pattern(element):  #TODO: test
    def neighbour_to_path_call(neig_type, neighbour):
        if neighbour.isTag():
            return PathCall(
                neig_type + "_neighbour_tag",
                0,
                [
                    neighbour.getTagName(),
                    _params_or_none(neighbour.params),
                    neighbour.getContent().strip()
                ]
            )

        return PathCall(
            neig_type + "_neighbour_tag",
            0,
            [None, None, neighbour.getContent().strip()]
        )

    # check if there are any neighbours
    if not element.parent or len(element.parent.childs) <= 1:
        return []

    parent = element.parent
    neighbours = parent.childs
    element_index = neighbours.index(element)

    output = []

    # pick left neighbour
    if element_index >= 1:
        output.append(
            neighbour_to_path_call("left", neighbours[element_index - 1])
        )

    # pick right neighbour
    if element_index < len(neighbours) - 2:
        output.append(
            neighbour_to_path_call("right", neighbours[element_index + 1])
        )

    return output


def predecesors_pattern(element, root):
    """

    Returns:
        list: ``[PathCall()]` - list with one :class:`PathCall` object (to allow
              use with ``.extend(predecesors_pattern())``).
    """
    if not element.parent or not element.parent.parent:
        return []

    trail = [
        [
            element.parent.parent.getTagName(),
            _params_or_none(element.parent.parent.params)
        ],
        [
            element.parent.getTagName(),
            _params_or_none(element.parent.params)
        ],
        [element.getTagName(), _params_or_none(element.params)],
    ]

    match = root.match(*trail)
    if element in match:
        return [
            PathCall("match", match.index(element), trail)
        ]

{'fraction_non_alphanumeric': 0.15242593387720052, 'fraction_numerical': 0.0047230571060541005, 'mean_word_length': 2.0371577574967406, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1787701', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1257, 'n_words': 262}