User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,995 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,218 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:

Projects for the Beginner

Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
Kickbutt Moderator

Solution Re: Projects for the Beginner

  #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 3:17 pm. Reason: missing lines
May 'the Google' be with you!
Reply With Quote  
All times are GMT -4. The time now is 9:58 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC