View Single Post
Join Date: Oct 2004
Posts: 4,135
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 946
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Projects for the Beginner

 
0
  #4
Sep 9th, 2005
Construct a sentence generator. Write down a bunch of sentences. Now pick them apart into lists of nouns, verbs, adverbs, adjectives and so forth. Use these pieces and create new sentences more or less at random. Some of those will be silly, some utter nonsense, but some could be the start of poetry or a novel.

Just to get you started, here is a very simple sentence generator, that takes the three parts (subject, action, object) of a standard sentence, shuffles these parts and reassembles the sentence ...
  1. # goofy sentence generator
  2.  
  3. import random
  4.  
  5. def make_sentence(part1, part2, part3, n=1):
  6. """return n random sentences"""
  7. # convert to lists
  8. p1 = part1.split('\n')
  9. p2 = part2.split('\n')
  10. p3 = part3.split('\n')
  11. # shuffle the lists
  12. random.shuffle(p1)
  13. random.shuffle(p2)
  14. random.shuffle(p3)
  15. # concatinate the sentences
  16. sentence = []
  17. for k in range(n):
  18. try:
  19. s = p1[k] + ' ' + p2[k] + ' ' + p3[k]
  20. s = s.capitalize() + '.'
  21. sentence.append(s)
  22. except IndexError:
  23. break
  24. return sentence
  25.  
  26. # break a typical sentence into 3 parts
  27. # first part of a sentence (subject)
  28. part1 = """\
  29. a drunken sailor
  30. a giggling goose
  31. the yearning youth
  32. the obese ostrich
  33. this mean mouse
  34. the skinny sister"""
  35.  
  36. # middle part of a sentence (action)
  37. part2 = """\
  38. jumps over
  39. flies over
  40. runs across
  41. openly ogles
  42. twice tastes
  43. vomits on"""
  44.  
  45. # ending part of a sentence (object)
  46. part3 = """\
  47. a rusty fence
  48. the laughing cow
  49. the weedcovered backyard
  50. the timid trucker
  51. the rancid old cheese
  52. the jolly jelly"""
  53.  
  54. print '-'*60
  55.  
  56. sentence = make_sentence(part1, part2, part3, 3)
  57. for item in sentence:
  58. print item
  59. print '-'*60
  60.  
  61. """
  62. a typical result -->
  63. A drunken sailor flies over the laughing cow.
  64. The obese ostrich runs across the weedcovered backyard.
  65. This mean mouse openly ogles the jolly jelly.
  66. """
Click on "Toggle Plain Text" so you can highlight and copy the code to your editor without the line numbers.
Last edited by vegaseat; Mar 1st, 2007 at 4:17 pm. Reason: missing lines
May 'the Google' be with you!
Reply With Quote