<commit_before>"""
Identifies common English writing mistakes
"""
import re
import unicodedata
from righter import dictionary
from righter import utils


def check_spelling(text):
    """
    Check if a text has spelling errors.
    Return a list with objects:
        {
            "selection": <wrong-spelled-word>,
            "start": <position-of-the-first-character-in-string>
        }
    """
    text = text.lower()
    text = utils.remove_punctuation(text)
    words = text.split()
    response = []
    for word in words:
        if not dictionary.is_english_word(word) and\
           not utils.contains_digit(word):
            item = {
                "selection": word,
                "start": text.find(word)
            }
            response.append(item)
    return response


def check_capitalization(text):
    """
    Check if a text has spelling errors.
    Return a list with objects:
        {
            "selection": <wrong-capitalized-word>,
            "start": <position-of-the-first-character-in-string>
        }
    """
    response = []
    sentences = re.split('[!?.]', text)  # TODO: add \n
    pos = 0
    for sentence in sentences:
        clean_sentence = sentence.strip()
        if not clean_sentence:
            continue
        # Check if first character is capital
        if clean_sentence[0].islower():
            first_word = clean_sentence.split()[0]
            first_word_position = pos + sentence.find(first_word)
            item = {
                "selection": first_word,
                "start": first_word_position
            }
            response.append(item)
        else:
            # check if a common English word in the middle of the text is
            # wrongly capitalized
            words = clean_sentence.split()
            for word in words[1:]:
                if word[0].isupper() and\
                   dictionary.is_english_word(word.lower()):
                    item = {
                        "selection": word,
                        "start": text.find(word)
                    }
                    response.append(item)
        pos += len(sentence) + 1
    return response<commit_msg>Wrap check functions<commit_after>"""
Identifies common English writing mistakes
"""
import re
import unicodedata
from righter import dictionary
from righter import utils


def check_spelling(text):
    """
    Check if a text has spelling errors.
    Return a list with objects:
        {
            "selection": <wrong-spelled-word>,
            "start": <position-of-the-first-character-in-string>
        }
    """
    text = text.lower()
    text = utils.remove_punctuation(text)
    words = text.split()
    response = []
    for word in words:
        if not dictionary.is_english_word(word) and\
           not utils.contains_digit(word):
            item = {
                "selection": word,
                "start": text.find(word)
            }
            response.append(item)
    return response


def check_capitalization(text):
    """
    Check if a text has spelling errors.
    Return a list with objects:
        {
            "selection": <wrong-capitalized-word>,
            "start": <position-of-the-first-character-in-string>
        }
    """
    response = []
    sentences = re.split('[!?.]', text)  # TODO: add \n
    pos = 0
    for sentence in sentences:
        clean_sentence = sentence.strip()
        if not clean_sentence:
            continue
        # Check if first character is capital
        if clean_sentence[0].islower():
            first_word = clean_sentence.split()[0]
            first_word_position = pos + sentence.find(first_word)
            item = {
                "selection": first_word,
                "start": first_word_position
            }
            response.append(item)
        else:
            # check if a common English word in the middle of the text is
            # wrongly capitalized
            words = clean_sentence.split()
            for word in words[1:]:
                if word[0].isupper() and\
                   dictionary.is_english_word(word.lower()):
                    item = {
                        "selection": word,
                        "start": text.find(word)
                    }
                    response.append(item)
        pos += len(sentence) + 1
    return response


def check(text):
    changes = []

    for change in check_capitalization(text):
        change['symbol'] = 'C'
        changes.append(change)

    for change in check_spelling(text):
        change['symbol'] = 'SP'
        changes.append(change)

    return changes
{'fraction_non_alphanumeric': 0.09948096885813149, 'fraction_numerical': 0.0025951557093425604, 'mean_word_length': 1.6780544296467863, 'pattern_counts': {'":': 20, '<': 11, '<?xml version=': 0, '>': 11, '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': '83060', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1212, 'n_words': 328}