<jupyter_start><jupyter_text>A anatomia de um CEP:    > CEP: 44.097-324Encontrando CEPs usando o _Matcher_: Quando criando um novo _matcher_ é importante verificar como o Spacytokeniza o objeto desejado. Para isso você pode checar o texto e o formato:```for text in text_with_zipcodes:    doc = nlp(text)    for t in doc:        print(t.text, t.shape_)```<jupyter_code>from spacy.matcher import Matcher


matcher = Matcher(nlp.vocab)

pattern_with_dot = [  # 44.050-024
    {"SHAPE": "dd."}, {"SHAPE": "ddd-ddd"}
]
pattern_without_dot = [  # 44050-024
    {"SHAPE": "dddd-ddd"}
]
matcher.add("zipcode", [pattern_with_dot, pattern_without_dot])

for text in text_with_zipcodes:
    doc = nlp(text)
    matches = matcher(doc)
    for match_id, start, end in matches:
        string_id = nlp.vocab.strings[match_id]
        span = doc[start:end]
        print(string_id, start, end, span.text)<jupyter_output>zipcode 11 12 44550-090
zipcode 14 16 44.550-090
zipcode 98 100 44.050-024
zipcode 61 63 44.050-024
zipcode 97 99 44.097-324
zipcode 83 85 44.100-000
zipcode 34 35 44002-024
zipcode 29 31 44.001-584
zipcode 24 25 44001-512
zipcode 65 66 44002-175<jupyter_text>O CEP é um exemplo que é bem direto e que pode ser resolvido tranquilamentecom regex. O spacy facilita isso também:<jupyter_code>import re

expression = r"\d{2}\.?\d{3}-\d{3}"

for text in text_with_zipcodes:
    doc = nlp(text)
    for match in re.finditer(expression, doc.text):
        start, end = match.span()
        span = doc.char_span(start, end)
        # This is a Span object or None if match doesn't map to valid token sequence
        if span is not None:
            print("Found match:", span.text)<jupyter_output>Found match: 44550-090
Found match: 44.550-090
Found match: 44.050-024
Found match: 44.050-024
Found match: 44.097-324
Found match: 44.100-000
Found match: 44002-024
Found match: 44.001-584
Found match: 44001-512
Found match: 44002-175
{'fraction_non_alphanumeric': 0.10512687726566546, 'fraction_numerical': 0.11807353702744691, 'mean_word_length': 3.6220095693779903, 'pattern_counts': {'":': 3, '<': 7, '<?xml version=': 0, '>': 8, '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}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '340592', 'license': 'MIT', 'n_tokens_mistral': 892, 'n_tokens_neox': 701, 'n_words': 217, 'path': 'notebooks/find-zipcodes.ipynb', 'repo_name': 'andrewsmedina/tomba', 'size': '1931'}