開始使用免費開始

將劇本切分為幕(Acts)

將非結構化文字轉換成分層的詞彙圖是個反覆進行的流程,需要為每個詞彙實體建立分割器,並依序據此切分。

在這個練習中,你要設計一個分割器,把劇本《羅密歐與茱麗葉》切分成各個「幕(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...
...

本練習屬於課程

使用 LangChain 與 Neo4j 的 Graph RAG

檢視課程

練習說明

  • 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])
編輯並執行程式碼