<commit_before>"""Implements a number of text preprocessing utilities."""
#  Copyright 2015-present Scikit Flow Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

from __future__ import division, print_function, absolute_import

import re
import six

import numpy as np

from skflow.preprocessing.categorical_vocabulary import CategoricalVocabulary

TOKENIZER_RE = re.compile(
    r"[A-Z]{2,}(?![a-z])|[A-Z][a-z]+(?=[A-Z])|[\'\w\-]+", re.UNICODE)


def tokenizer(iterator):
    """Tokenizer generator.

    Args:
        iterator: Input iterator with strings.

    Yields:
        array of tokens per each value in the input.
    """
    for value in iterator:
        yield TOKENIZER_RE.findall(value)


class ByteProcessor(object):
    """Maps documents into sequence of ids for bytes."""

    def __init__(self, max_document_length):
        self.max_document_length = max_document_length

    def fit(self, X):
        """Does nothing."""
        pass

    def fit_transform(self, X):
        """Calls transform."""
        return self.transform(X)

    def transform(self, X):
        """Transforms input documents into sequence of ids.

        Args:
            X: iterator or list of input documents.
               Documents can be bytes or unicode strings, which will be encoded
               as utf-8 to map to bytes. Note, in Python2 str and bytes is the
               same type.
        Returns:
            iterator of byte ids.
        """
        for document in X:
            if isinstance(document, six.text_type):
                document = document.encode('utf-8')
            if six.PY3:
                mv = memoryview(document)
            else:
                mv = buffer(document)
            buff = np.frombuffer(mv[:self.max_document_length],
                                 dtype=np.uint8)<commit_msg>changed the way of using memoryview/buffer into more pythonic<commit_after>"""Implements a number of text preprocessing utilities."""
#  Copyright 2015-present Scikit Flow Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

from __future__ import division, print_function, absolute_import

import re
import six

import numpy as np

from skflow.preprocessing.categorical_vocabulary import CategoricalVocabulary

TOKENIZER_RE = re.compile(
    r"[A-Z]{2,}(?![a-z])|[A-Z][a-z]+(?=[A-Z])|[\'\w\-]+", re.UNICODE)


def tokenizer(iterator):
    """Tokenizer generator.

    Args:
        iterator: Input iterator with strings.

    Yields:
        array of tokens per each value in the input.
    """
    for value in iterator:
        yield TOKENIZER_RE.findall(value)


class ByteProcessor(object):
    """Maps documents into sequence of ids for bytes."""

    def __init__(self, max_document_length):
        self.max_document_length = max_document_length

    def fit(self, X):
        """Does nothing."""
        pass

    def fit_transform(self, X):
        """Calls transform."""
        return self.transform(X)

    def transform(self, X):
        """Transforms input documents into sequence of ids.

        Args:
            X: iterator or list of input documents.
               Documents can be bytes or unicode strings, which will be encoded
               as utf-8 to map to bytes. Note, in Python2 str and bytes is the
               same type.
        Returns:
            iterator of byte ids.
        """
        for document in X:
            if isinstance(document, six.text_type):
                document = document.encode('utf-8')
            if six.PY3:
                # For Python3 defined buffer as memoryview.
                buffer = memoryview
            document_mv = buffer(document)
            buff = np.frombuffer(document_mv[:self.max_document_length],
                                 dtype=np.uint8)
{'fraction_non_alphanumeric': 0.08819875776397515, 'fraction_numerical': 0.006004140786749482, 'mean_word_length': 2.498189717595945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3369287', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1297, 'n_words': 488}