Class Notes (837,548)
Canada (510,312)
CSC108H1 (113)
Lecture

november 04 notes

2 Pages
66 Views
Unlock Document

Department
Computer Science
Course
CSC108H1
Professor
Michelle Craig
Semester
Fall

Description
 Imporved lazy lit 1. import random 2. 3. def build_dict(r, context_length): 4. '''Return a dictionary where the keys are tuples of context_length elements 5. where the elements are words in reader r and the value 6. for a key is the list of words that were found in r following the key.''' 7. d = {} 8. words = r.read().split() 9. 10. context = ('',) * context_length 11. for word in words: 12. # this doesn't work because we replace d[context] instead of adding 13. # to the list of words, and we don't have a list as the value 14. #d[context] = word 15. if context in d: 16. d[context].append(word) 17. else: 18. d[context] = [word] 19. context = context[1:] + (word,) 20. 21. return d 22. 23. def pick_next_word(words, context): 24. '''Return a tuple of the next word and the updated context. 25. The next word for our story based on dictionary words and the 26. current context.''' 27. 28. if context in words: 29. follow_on_words = words[context] 30. # pick a random word from the list that goes with context 31. # it is fine with us to shuffle the list order inside the words dict 32. random.shuffle(follow_on_words) 33. new_word = follow_on_words[0] 34. # make that word the new context 35. context = conte
More Less

Related notes for CSC108H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit