Build the Vocabulary چالش 3 از جلسهی 33، متوسط
تابع build_vocab(sentences) را کامل کن که همهی جملهها را توکن کند، کلمههای یکتا را در یک set جمع کند و لیست مرتبشده برگرداند. خروجی: ['a', 'and', 'cat', 'dog', 'sat', 'the'] و بعد 6.
def tokens(text):
clean = "".join(ch for ch in text.lower() if ch.isalpha() or ch == " ")
return clean.split()
def build_vocab(sentences):
words = set()
# add every token of every sentence
return sorted(words)
vocab = build_vocab(["The cat sat.", "The dog sat!", "A cat and a dog"])
print(vocab)
print(len(vocab))
راهنما و جواب این چالش · برگشت به درس جلسهی 33: متن برای کامپیوتر · چالش بعدی: Bag of Words
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال