Cut into Chunks چالش 1 از جلسهی 71، آسان
تابع chunk_words(text, size) را بنویس که متن را به تکههای size کلمهای تقسیم کند (تکهی آخر ممکن است کوتاهتر باشد) و لیست تکهها را برگرداند. برنامه با size = 20 تکهها را شمارهدار چاپ میکند. ببین جملهی «This is called condensation» کجا نصف شده! خروجی:0: The water cycle moves water around the Earth. The sun heats water in seas and lakes. The warm water turns1: into vapour and rises. This is called evaporation. High in the sky the vapour cools and forms clouds. This is2: called condensation. When the drops in a cloud get heavy, they fall as rain or snow. This is called precipitation.
NOTES = ("The water cycle moves water around the Earth. The sun heats water in seas and lakes. "
"The warm water turns into vapour and rises. This is called evaporation. High in the sky "
"the vapour cools and forms clouds. This is called condensation. When the drops in a cloud "
"get heavy, they fall as rain or snow. This is called precipitation.")
def chunk_words(text, size):
words = text.split()
chunks = []
return chunks
for i, chunk in enumerate(chunk_words(NOTES, 20)):
print(f"{i}: {chunk}")
راهنما و جواب این چالش · برگشت به درس جلسهی 71: RAG کوچک (۱): تکهی درست را پیدا کن · چالش بعدی: Clean Words Only
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال