Tách vở kịch theo Hồi (Acts)
Chuyển văn bản phi cấu trúc thành đồ thị từ vựng theo phân cấp là một quá trình lặp, bao gồm việc tạo các splitter cho từng thực thể từ vựng rồi lần lượt tách theo từng cấp.
Trong bài này, bạn sẽ thiết kế một splitter để tách vở kịch Romeo and Juliet thành các hồi (act). Dưới đây là phần xem trước cấu trúc của vở kịch:
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...
...
Bài tập này là một phần của khóa học
Graph RAG với LangChain và Neo4j
Hướng dẫn bài tập
- Cập nhật tham số
splittersđể tách văn bản theo mẫu\n\nACT. - Cấu hình
act_splitterđể coi danh sáchseparatorslà các biểu thức chính quy (regular expressions). - Tách
romeo_and_julietbằngact_splitter.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
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])