Estrarre frasi in base al pattern dei tag
Estrarre sintagmi nominali e verbi
Estrarre frasi in base al pattern dei tag è una lezione NLP Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento NLP Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso NLP Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
From Tags to Phrases
Single tags are useful, but meaning often lives in groups of words. Tag patterns let you pull out whole phrases instead of lone tokens. 🧩
What Is a Noun Chunk?
A noun chunk is a noun plus the words describing it, like the fluffy cat. These chunks usually name the key things a sentence is about.
Get Noun Chunks Free
spaCy finds noun chunks for you. Iterate over doc.noun_chunks to grab each one as a span without writing any rules.
for chunk in doc.noun_chunks:
print(chunk.text)Chunks Are Spans
Each noun chunk is a Span, a slice of the Doc. A span knows its text, start, and end, so you can reuse it like any other token group.
chunk = list(doc.noun_chunks)[0]
print(chunk.start, chunk.end)Pull Out Every Verb
To collect actions, keep tokens whose pos_ is VERB. A list comprehension gathers them all in a single readable line.
verbs = [t.text for t in doc if t.pos_ == "VERB"]Match Custom Patterns
For your own rules, use the Matcher. You describe a sequence of token attributes, and spaCy finds every place that sequence appears.
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)Define a Tag Sequence
A pattern is a list of dictionaries, one per token. This one matches an adjective followed by a noun, like fluffy cat.
pattern = [{"POS": "ADJ"}, {"POS": "NOUN"}]Add the Pattern
Register your pattern under a name with matcher.add(). The name lets you group related rules and identify matches later.
matcher.add("ADJ_NOUN", [pattern])Run the Matcher
Call the matcher on a Doc to get matches. Each result is a tuple of a match id plus the start and end token positions.
matches = matcher(doc)Read Each Match
Slice the Doc with the start and end to turn a match into readable text. That slice is the phrase your pattern captured.
for mid, start, end in matches:
print(doc[start:end].text)Patterns Beat Regex Here
Tag patterns understand grammar, so they catch phrases regex would miss. Matching on POS is far cleaner than chasing exact letters.
Quick Check
Think about how to extract grouped nouns.
Recap
You extracted phrases two ways: built-in noun_chunks and custom tag patterns with the Matcher. Now you can mine grammar, not just words. 🎉
Domande Frequenti
La lezione «Estrarre frasi in base al pattern dei tag» è gratuita?
Sì — il testo completo di «Estrarre frasi in base al pattern dei tag» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso NLP Academy, passa a CoddyKit PRO. Il corso NLP Academy include 4 lezioni in totale.
Cosa imparerò in «Estrarre frasi in base al pattern dei tag»?
Estrarre sintagmi nominali e verbi Eserciti NLP Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare NLP Academy?
Non è richiesta alcuna esperienza precedente. NLP Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Estrarre frasi in base al pattern dei tag»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione NLP Academy?
Sì. Ogni lezione NLP Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Nomi, verbi e importanza dei tag
- Assegnare tag alle parole con spaCy
- Estrarre frasi in base al pattern dei tag
- Basi del dependency parsing