戯曲を幕(Act)に分割する
非構造テキストを階層的な語彙グラフに変換するには、各語彙エンティティごとにスプリッターを作成し、順番に分割していく反復的なプロセスが必要です。
この演習では、戯曲『Romeo and Juliet』を幕(Act)ごとに分割するスプリッターを設計します。以下は戯曲の構造のプレビューです。
The Project Gutenberg eBook of Romeo and Juliet
This ebook is for the use of anyone anywhere in the United States...
**PROLOGUE:**
Enter Chorus.
CHORUS.
Two households, both alike in dignity...
ACT I
SCENE I. A public place.
Enter Sampson and Gregory armed with swords and bucklers.
SAMPSON.
Gregory, on my word, we’ll not carry coals...
...
この演習はコースの一部です
Graph RAG with LangChain and Neo4j
演習の手順
splitters引数を更新し、\n\nACTのパターンでもテキストを分割するようにします。act_splitterを構成し、separatorsリストを正規表現として扱うように設定します。act_splitterを使ってromeo_and_julietを分割します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
act_splitter = RecursiveCharacterTextSplitter(
separators=[
r"\n\nTHE PROLOGUE.",
r"\n\n\*\*\* END",
# Split by the word ACT
r"____"
],
# Configure the patterns as regular expressions
____=True
)
# Split the play using act_splitter
acts = act_splitter.____(____)
for act in acts:
print(act.strip().split("\n")[0])