<commit_before>"""Text processing utilities."""

from collections import Counter
import string

from nltk.corpus import stopwords
import nltk


def generate_fingerprint(text):
    """Generate a fingerprint to be used for comparison to other texts."""
    text_tokens = tokenize(text)
    filtered_tokens = [word for word in text_tokens if word not in stopwords.words('english')]
    count_tokens = Counter(filtered_tokens)
    common_tokens = count_tokens.most_common(15)
    fingerprint = sorted([x for x, y in common_tokens])
    return '-'.join(fingerprint)[:255]


def tokenize(text):
    """Split text into words."""
    # translator = str.maketrans('', '', string.punctuation)
    # no_punctuation = text.lower().translate(None, string.punctuation)
    exclude = set(string.punctuation)
    no_punctuation = ''.join(char.lower() for char in text if char not in exclude)
    return nltk.word_tokenize(no_punctuation)<commit_msg>try to do the downloads if necessary<commit_after>"""Text processing utilities."""

from collections import Counter
import string

from nltk.corpus import stopwords
import nltk

try:
    nltk.word_tokenize('test phrase')
except LookupError:
    nltk.download('punkt')
    nltk.download('stopwords')


def generate_fingerprint(text):
    """Generate a fingerprint to be used for comparison to other texts."""
    text_tokens = tokenize(text)
    filtered_tokens = [word for word in text_tokens if word not in stopwords.words('english')]
    count_tokens = Counter(filtered_tokens)
    common_tokens = count_tokens.most_common(15)
    fingerprint = sorted([x for x, y in common_tokens])
    return '-'.join(fingerprint)[:255]


def tokenize(text):
    """Split text into words."""
    exclude = set(string.punctuation)
    no_punctuation = ''.join(char.lower() for char in text if char not in exclude)
    return nltk.word_tokenize(no_punctuation)
{'fraction_non_alphanumeric': 0.10596379126730564, 'fraction_numerical': 0.005324813631522897, 'mean_word_length': 4.526470588235294, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5260191', 'n_tokens_mistral': 586, 'n_tokens_neox': 556, 'n_words': 168}