トークン、Span、Doc オブジェクト
spaCy のデータ構造を操作する
「トークン、Span、Doc オブジェクト」はCoddyKit上の無料NLP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNLP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 NLP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Three Core Objects
spaCy gives you three building blocks: the Doc, the Token, and the Span. Learn these and the whole library opens up.
The Doc Container
A Doc is the full processed document. It holds the original text plus every token and all the analysis spaCy produced.
doc = nlp("Maria leads the data team.")Index Like a List
You can grab one item by position because a Doc is indexable. Indexing returns a single Token, just like a Python list.
first = doc[0]What a Token Holds
A Token is one unit of text plus rich metadata: its part of speech, its lemma, and whether it is punctuation.
print(doc[0].text, doc[0].pos_)Useful Token Flags
Tokens carry handy flags like is_stop, is_alpha, and is_punct. They let you filter words without writing your own checks.
for t in doc:
print(t.text, t.is_stop)Slice to Get a Span
Slice a Doc and you get a Span, a contiguous run of tokens. It is perfect for phrases like a person or a place.
phrase = doc[0:2]Spans Keep Context
A Span is not a copy. It still points back into the Doc, so it knows its position and shares all the original analysis.
Entities Are Spans
Named entities show up as Spans in doc.ents. Each one bundles the words plus a label like PERSON or ORG.
for ent in doc.ents:
print(ent.text, ent.label_)Noun Chunks
spaCy also offers noun_chunks, base noun phrases served as Spans. They are a quick way to pull out the things a sentence talks about.
list(doc.noun_chunks)Sentences as Spans
Need sentences? Iterate doc.sents. Each sentence comes back as a Span, so segmentation is already handled for you.
for sent in doc.sents:
print(sent.text)Original Text Stays
Through all of this the original string is preserved in doc.text. Tokens and spans are views, never destructive edits.
Quick Check
What do you get when you slice a Doc with doc[0:3]?
Recap
The Doc holds everything, indexing gives one Token, and slicing gives a Span. Entities, noun chunks, and sentences all arrive as Spans. 🧩
よくある質問
「トークン、Span、Doc オブジェクト」レッスンは無料ですか?
はい。「トークン、Span、Doc オブジェクト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、NLP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 NLP Academyコースには全4レッスンが含まれています。
「トークン、Span、Doc オブジェクト」で何を学びますか?
spaCy のデータ構造を操作する ブラウザで直接実行するハンズオンコードでNLP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
NLP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのNLP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「トークン、Span、Doc オブジェクト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このNLP Academyレッスンでコードを書いて実行できますか?
はい。すべてのNLP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 実プロジェクトで spaCy を使う理由
- モデルを読み込み Doc を処理する
- トークン、Span、Doc オブジェクト
- パイプラインをカスタマイズする