"""Text normalisation and tokenisation. Tokens keep their character offsets in the normalised text, so every value the copilot fills can point back at the words that produced it (the UI highlights them). """ from __future__ import annotations import re import unicodedata from dataclasses import dataclass MAX_CHARS = 2000 _QUOTES = {'‘': "'", '’': "'", '‚': "'", '‛': "'", '′': "'", '“': '"', '”': '"', '„': '"', '″': '"', '«': '"', '»': '"', '–': '-', '—': '-', '−': '-', ' ': ' '} CONTRACTIONS = { "don't": 'do not', 'dont': 'do not', "doesn't": 'does not', 'doesnt': 'does not', "didn't": 'did not', "can't": 'can not', 'cant': 'can not', 'cannot': 'can not', "won't": 'will not', 'wont': 'will not', "shouldn't": 'should not', 'shouldnt': 'should not', "mustn't": 'must not', 'mustnt': 'must not', "isn't": 'is not', "aren't": 'are not', "wasn't": 'was not', "weren't": 'were not', "wouldn't": 'would not', "couldn't": 'could not', "haven't": 'have not', "hasn't": 'has not', "shan't": 'shall not', "needn't": 'need not', "mayn't": 'may not', "ain't": 'is not', "let's": 'let us', "it's": 'it is', "i'm": 'i am', "they're": 'they are', "we're": 'we are', "you're": 'you are', "that's": 'that is', "what's": 'what is', "there's": 'there is', "i'd": 'i would', "we'd": 'we would', "i'll": 'i will', "we'll": 'we will', "i've": 'i have', "we've": 'we have', } NUMBER_WORDS = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15, 'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20, 'thirty': 30, 'forty': 40, 'fifty': 50, 'sixty': 60, 'seventy': 70, 'eighty': 80, 'ninety': 90, 'hundred': 100, 'thousand': 1000, 'dozen': 12, } # Order matters: the first alternative that matches at a position wins. _TOKEN_RE = re.compile(r""" (?P"[^"\n]{1,80}"|(?[a-z0-9._%+-]+@[a-z0-9-]+(?:\.[a-z0-9-]+)+) | (?Phttps?://[^\s,;"'<>()]+) | (?P(?:\*\*/|\*|~)?[./\\]?[a-z0-9_*-]*\.(?:env|pem|key|pfx|p12|kdbx|ssh|aws|npmrc|pypirc|netrc|git-credentials)\b|\*\*?/[^\s,;]+|\*\.[a-z0-9]{1,8}\b) | (?P