将剧本按幕拆分
将非结构化文本转换为分层的词汇图是一个迭代过程,需要为每类词汇实体构建分割器,并依次按这些实体进行拆分。
在本练习中,您将设计一个分割器,把剧本《罗密欧与朱丽叶》按幕进行拆分。以下是剧本结构的预览:
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])