#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    Random sentence generator


    Random Sentence Generator
    Write a program that reads in files of words and produces random sentences, according to the syntax (rules of grammar) specified below.
    Here is the output of a demo program. These sentences weren't selected – they were the first six produced:

    It is commonly believed that your ghastly Mortician deliberately befriends my annoying bulb.

    Lo! our green lecturer often hibernates.

    This foreign burglar usually cooks and ate the flesh of her inconceivable soil.

    Indeed, the orbiting boy inexorably recognizes this half-hidden monstrosity.

    My vaporized model knowingly reaches an understanding with his dainty terror.


    What the symbols mean when defining a grammar:
    • "::=" is to be read as “is defined as”.
    • Square brackets [ ] around an item mean it's optional – it may be included or omitted.
    • the vertical bar | means OR (choose either the item to the left or right of the | )
    • Angle brackets (e.g. < > ) around an item means it’s defined in more detail later

    To explain how a grammar can be defined and used, let’s say we have four lists of words:
    NOUNS cat, fire, sky
    VERBS eats, dreams, digs up ADJECTIVES red, huge, slimy NOUN-MARKERS our, my, the
    and the following is the definition for valid noun phrases:
    <Noun-Phrase> ::= [ <Noun-marker> ] [ <Adjective> ] <Noun>
    Square brackets around an item means it's optional, so both the noun marker and adjective can be omitted. Therefore some valid noun phrases for the grammar above are:
    cat our fire our slimy sky huge fire my red cat
    Just for this example, let the definition for Sentence be:
    Sentence ::= Noun-Phrase
    the red cat
    our sky my cat
    Verb
    eats
    dreams
    digs up
    Noun-phrase
    our slimy sky
    the huge fire
    the red sky
    
    This simplified definition of "sentence" is just for this example.
    For your program, use the sentence syntax from "Grammatical Definition of Sentences" below:

    If we capitalise the first word & add a full stop, each of those three lines is a valid (silly) sentence.


    Suggestions
    • There are multiple ways to build the sentence:
    • you can print out each word as you go. This method is simple but has the limitation that
    you never have the complete sentence as a string.
    • you can build up the sentence by making a string onto that gets longer as you add the next part of the sentence :
    e.g. s = a random selection from Leadin
    s = s + random selection from NounMarker # and keep adding • you can get all the pieces
    • If an item is optional, either don't include that option some of the time, or set the string for that option to the null string (e.g. adjective = "") and then join it on as usual.
    • Include the optional items 50% of the time. To control this you can use
    • the random.random() function which returns a real number somewhere between 0 and 1, or
    • random.choice(['heads', 'tails']) - try it and see what it does
    Don't forget to import the random module
    • Your program will have a cleaner structure and be easier to follow if you create functions for <noun-phrase> and <verb-phrase>, and possibly <sentence> as well.
    • Adding the method .title() to a string will capitalise each word in it: >>> s = 'hello'
    >>> print s.title() # convert it to title case Hello


    I dont know where to star with this assignment as it is my first big program assignment, please help
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    First, you should do some research on sentence construction.
    (Ex: Subject, Verb, Predicate). From there on an easy way is to create different txt files. One containing subjects, one containing verbes, etc. You then draw random words from those files, put them into the correct order and voila, you have yourself a random phrase generator.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    I would generalize the various words, probabilities, and phrases with classes.
    Code:
    import random
    
    class phrase:
    
        def __init__(self, list_of_parts, probability = 1, part_of_speech = None):
            '''
                each object in iterable list_of_parts should be callable and must return a list of words.
                probability [0,1]  determines the probability this part of speech is used.
                    The default of 1 means "mandatory".
                part_of_speech is used for the __str__ method and might remain unused.
            '''
    class word:
    
        def __init__(self, source, probability = 1, part_of_speech = None):
            '''
                source.read().split() must produce a list of words
                probability [0,1]  determines the probability this part of speech is used.
                    The default of 1 means "mandatory".
                part_of_speech is used for the __str__ method and might remain unused.
            '''
    
    def rsg_input(a = None):
        try:
            return random.choice(a)
        except:
            return random.random()
    
    def setup():
        with open('nouns') as inf:
            noun = word(inf, part_of_speech = 'noun')
        with open('adjectives') as inf:
            adj = word(inf, probability = 0.6, part_of_speech = 'adjective')
        with open('noun_markers') as inf:
            nm = word(inf, probability = 0.4, part_of_speech = 'noun-marker')
        #<Noun-Phrase> ::= [ <Noun-marker> ] [ <Adjective> ] <Noun>
        np = phrase((nm, adj, noun,), part_of_speech = 'noun-phrase')
        sentence = phrase((np,), part_of_speech = 'sentence')
        return sentence
    
    def punctuate(s):
        return s + rsg_input('!.?')[0]
    
    def finishing_touch(L):
        L[0] = L[0].capitalize()
        return punctuate(' '.join(L))
    
    def main():
        sentence = setup()
        for i in range(6):
            silly_saying = sentence()
            print(finishing_touch(silly_saying))
    
    if __name__ == '__main__':
        main()

    Decrypt by xor with ASCII values of 'SPOILER' repeated enough times
    Code:
     58  61  63  38  62  49 114  33  49  33  45  35  40  88  89
     51  35  40  63  54 114  35  56  61  40  63  32 104  89  90
    111 105 108 101  54  54  54 111  22  19  44  60  58  36  16
     22 100  54  55  63  54  99 105  32  44  33  39  15  32  47
     19  53  51  33  36  60 101 108  53  32  60  50  46  43  37
     41  59  39  41 111 116 108 116 126 115  32  46  59  56  26
     61  53  15  60  57  41  32  49  59 112 114 105   2  42  60
     54 121 117  67 108 101 114 115 112 111 105 108  98 117 116
     90 111 105 108 101 114 115 112 111 105 108 101 114  54  49
     44  33 108  42  48  57  53  44  61 108  44  60 115  57  59
     44  62  36  48  63  53 111  37  37  54  38  12  63  41  22
     60  36  32  39  35 111  58  36  42  39  63  52 111  43  41
    101  49  50  60  35  40  46  41  55 115  49  33  45 108  40
     39  32  36 111  59  41  49  39  33  62 111  40 108  41  59
     32  36 111  38  42 101  37  60  34  43  58  98  79 114 115
    112 111 105 108 101 114 115 112 111 105  60  55  61  49  49
     45  32  32  44  38  42 112  20 121  96 116  15 115 112  43
     44  56  32  32  62  57  33  44  63 101  38  59  53 111  57
     62  42  48  50  50  38  37  37  49  43 115  36  39  32  63
    101  34  50  34  59 105  35  35 114  32  32  42  44  47  45
    114  58  35 111  60  63  32  54 125  90 111 105 108 101 114
    115 112 111 105 108 101 114 115 112 111 105  24  45  55 115
     52  42  47  45  48  62  39 112  32  47 108 116 114  62  53
     46  39  63 101 112  62  49  33  45  45  49  61  33  41 109
    103  70 101 114 115 112 111 105 108 101 114 115 112 111  57
     45  55  38  12  63  41  22  63  53  55  54  51  39 105  37
     54 114  38  35  42  45 108  35  61  33 112  59  33  41 101
     13  12  35  59  59  19  26 114  62  53  59  33  35  33 114
     50  62  43 105  33  44  53  59  36 111  59  41  40  51  58
     62 111  60  34  48  33  54  52  97  67 108 101 114 115 112
    111 105 108  98 117 116  90 111 105 108 101 114 115 112 111
     58  41  41  52 125  32  46  59  56  54 114 110 112  35  32
     63  49  13  60  54  16  57  45  55  38  32  90 111 105 108
    101 114 115 112 111  58  41  41  52 125  36  39  59  41  54
     58  60  60  43 105 113 101  99 115 125 111  57  62  42  48
     50  50  38  37  37  49  43  89 112 111 105 108 101 114 115
    112  59  59  53 127  88 115 112 111 105 108 101 114 115 112
    111 105 108  54  55  63  54  97  58  56  55  59  61  55 111
    116 108  98 110  40  45 106 105  55  56 108 116 126  41  38
     62  40  51  39 120  38  39  56 109  32  60  37  33  45 100
    116  98  99 122  63  59  35  39  51  49  57  35  32  56  60
    123 122 124 111  58  56  55 122  35  49  61  61  19  42  52
     12  35  63  44  41  38  58 122 121  69 105 108 101 114 115
    112 111 105  41  61  49  54  32  59 115  70 101 114 115 112
    111 105 108 101 114 115 112 111  58  41  41  52 125  35  59
     59  37  43  53 115 109 111 110 112  48  60  56  62  32  62
     34 123 117  89  90 111 105 108 101  54  54  54 111  22  19
     54  38  33  15  16  97  63  32  62  53 121 117  67 108 101
    114 115 112 111 105 108  55  55  39  37  61  39 108  54  55
     63  54  97  58  56  55  59  61  55  69  67 108 101 114 115
     52  42  47 108  26  13  48  49  35  37  19  26 122  32  53
     35  47 101 127  88 115 112 111 105 108 101 114 115 119 104
    110  70 101 114 115 112 111 105 108 101 114 115 112 111  27
     41  49  39  33  62 111  61  36  32 114  48  56  32  58  41
     43 114  36  63  61  45 108  44  60 115  49 111  37  37  54
     38 125  90 111 105 108 101 114 115 112 111 105 108 101 114
     28  54 111  42  35  48  32  32  53 111  61  36  32 114  63
     57  60  61 108  38  61  38  60  43 105  46  32 114   8  13
    111  44  33  53  38  42 126  69 105 108 101 114 115 112 111
    105 107  98 117  89 112 111 105 108 101 114 115 112  61  44
     56  48  32  61  15  57  40  32  48  55 115 109 111  18  17
     79 114 115 112 111 105 108 101 114  58  54 111  58  41  41
     52 125  36  39  59  41  54  58  60  60  43 105 112 101  32
     32  55  16  32  34  53  39  39 120 102 115  70 101 114 115
    112 111 105 108 101 114 115 112 111  47  35  55 114  60 112
     38  39 108  54  55  63  54  97  57  45  55  38  32 106  69
    105 108 101 114 115 112 111 105 108 101 114 115 112 111 105
    108  55  55  39  37  61  39  19  51  51  63  37  42 103  41
     61  38  54  62  43  97  35 109 123 122  90 111 105 108 101
    114 115 112 111  59  41  49  39  33  62 111  59  41  49  39
     33  62  16  63  45  41  39  54  90  69  42  32  36  33  32
    112  56  38  62  33 104  89  90 111 105 108 101  54  54  54
    111  22  19  44  60  58  36  16  22 100  54  55  63  54  99
    105  63  42  39  33  51  42 101 108  53  32  60  50  46  43
     37  41  59  39  41 111 116 108 116 126 115  32  46  59  56
     26  61  53  15  60  57  41  32  49  59 112 114 105   2  42
     60  54 121 117  67 108 101 114 115 112 111 105 108  98 117
    116  90 111 105 108 101 114 115 112 111 105 108 101 114  32
     63  58  59  47  32 124  33  53  46  45 100 108 124  32  32
     35  32  56 109 123 115  61  58  58  56 101  34  33  63  43
     60  47  32 114  50 112  35  32  63  49 114  60  54 111  62
     35  55  54  32  90 111 105 108 101 114 115 112 111 105 108
    101 114  35  34  32  43  45  39  59  63  57  59  48 108  30
     98 127  97  18 105 108  33  55  39  53  61  36  37  43  55
     32 112  59  33  41 101  34  33  63  45  40  46  44  62  58
     36  54 105  56  45  59  32 112  63  40  62  49 114  60  54
    111  58  60  32  55  48  56 111  32  63 101  39  32  53  43
    103  70 101 114 115 112 111 105 108 101 114 115 112 111 105
    108 101 114   7  56  42 105  40  32  52  50  37  35  61 108
     42  52 115  97 111  36  41  36  60  32 112 109  36  45  43
     54  50  36  32  59  53 103 124  89 112 111 105 108 101 114
    115 112 111 105 108 101  34  50  34  59  22  35  35  13  32
     32  42  44  47  45 114  58  35 111  60  63  32  54 115  54
     32  59 108  49  58  54 112  16  22  63  49  32  12  15 111
     36  41  49  58  60  52 111  40  34  33 114  62  57  40  33
     56 101  32  54  61  46  32  34 101  39  61  37  60  44  40
    107  88 115 112 111 105 108 101 114 115 119 104 110  70 101
    114 115 112 111 105 108 101  33  54  60  41 103  59  42  32
     55  35 111 116 108  54  61  38  34  44  44  98  55  55  50
     52 103  96  98  54  34  63  57  59  97 101  79 114 115 112
    111 105 108 101 114  32  53  35  47  98  49  58  33  53  60
     33  35  41  54 115 109 111 120 108 104 114  35  34  32  43
     45  39  59  63  57  59  48  70 101 114 115 112 111 105 108
    101  38  33  41 117  67 108 101 114 115 112 111 105 108 101
    114 115 112  60  44  32  35 124  32  36  61  32  34  34 114
    110 112 104 117  55  56 119 115  43  50 119 107 107  52  60
     34  34  40  56 109  59  61  36 103  59  35  48  60  55 120
    126 121 124 111  34  33  63  45  40  46  44  62  58  36  54
     96 101 105 114  32  36  61  97  60  36  32  39  15  32  47
     19  54  34  54  53  44  33 101 108  88 115 112 111 105 108
    101 114 115  53  55  42  41  53  38 105  90 111 105 108 101
    114 115 112 111 105 108 101 114  32  53  35  47  98  54  38
     33  57  33  46 108 120 114 116 108  58  39  39  43  61  36
     62 113 110  70  79 114 115 112 111  45  41  35 114  12  15
     60  61  62  26  13 123  35  42  37  42 108 104  89 112 111
    105 108 101 114 115 112  61  44  56  48  32  61 112  60  44
     32  35 124  32  36  61  32  34  34  88  89 112 111 105 108
     33  55  53 112  16  22  47  36  62  63  15  16  97  63  32
     62  53 121 117  67 108 101 114 115 112 111 105 108  98 117
    116  90 111 105 108 101 114 115 112 111 105 108 101 114   1
     53  59  60  62  43 114  39  56  42 105  47  45  61  32  53
     33 105  59  42  32  55 112  38  39 108  36 114  63  57  60
     61  98  79 114 115 112 111 105 108 101 114 115 112 111 105
      3  35 114  48  63  58  59  63  32 114  39  56  42 105  32
     44  33  39 112  44  38  57  41  54 115  50  42 105  23  24
    114  54  61  63  61  53 107  88 115 112 111 105 108 101 114
    115 119 104 110  70 101 114 115 112 111 105 108 101  32  54
     36  58  59  34  26  36  50  60  58  44 108 120 114   8  13
     69 105 108 101 114 115 112 111 105  37  35 114  32  53  35
     47  98  49  58  33  53  60  33  35  41  54 115 108 111  59
     63  34  13  58  62  63  60  56 109 123 105  90 111 105 108
    101 114 115 112 111 105 108 101 114  33  53  59  60  62  43
     13  37  49  35  60  41 107  51  35  32  42  39  40 109  32
     32  55  16  32  34  53  39  39 120  60  44  32  35 124  36
     63  61  45  63 108 123  89 112 111 105 108 101 114 115 112
     61  44  56  48  32  61 112  61  44  56  48  32  61  15  57
     40  32  48  55  89  90  43  44  42 101  32  32  55  16  32
     34  53  39  39 120  46 105 113 101  28  60  62  42  96 118
     79 114 115 112 111  61  62  60 104  89 112 111 105 108 101
    114 115 112  61  44  56  48  32  61 112  61  40  34  33  61
     62 126  44  33  35  44  49  54 120  46  96  70 101 114 115
    112  42  49  47  32  34  39 106  69 105 108 101 114 115 112
    111 105  62  32  38  38  34  33 105  62  36  60  55  63  34
    103  62  36  60  55  63  34  97 101  79  88  55  53  41 105
     63  32  38  38  32 103  96 118  79 114 115 112 111  62  37
     49  58 115  63  63  44  34 109 117  61  63  58  39  63  98
    123 115  49  60 105  37  43  52 105  90 111 105 108 101 114
    115 112 111  39  35  48  60 115 109 111  62  35  55  54 123
     57  33  47  96 101  34  50  34  59  22  35  35  13  32  32
     42  44  47  45 114 110 112 104  39  35  48  60 116 121  69
    105 108 101 114  36  57  59  33 108  42  34  54  62 103 110
     45  33  56  54  51  59  32  58  32  33 116 121 111  40  63
    101  59  61  54 117  67 108 101 114 115 112 111 105 108  36
     54  57 112 114 105  59  42  32  55 120  38  39  42 105 114
     35  34  32  43  45  39  59  63  57  59  48 108 120 114  99
    126 121 101 108  53  51  33  36  16  38  42  26  33  35  53
     42  42  36 101 111 115 119  46  45  38  32  49  39  57  57
     44 107 108  88 115 112 111 105  59  44  38  59 112  32  57
     41  43 122 116  62  32  60  34  26  63  50  34  36  44  62
     54 117 122 112  46  58 108  44  60  53 106  69 105 108 101
    114 115 112 111 105  34  40 114 110 112  56  38  62  33 122
     58  62  41 101 108  53  32  60  50  46  43  37  41  59  39
     41 111 116 108 117 124 103 124 111  57  45  55  38  12  63
     41  22  63  53  55  54  51  39 105 113 101 117  61  63  58
     39  97  40  51  33  59  42  59 107 108  88 115 112 111 105
    111 121  28  60  37  33 100  28  45  32  50  35  42 119 108
    127 104 110 112  20 105 112  11  61  38  62  98  36  45  55
     57  54  34 113 105  17 101   9 115 108  14  45  38  32  49
     39  57  57  44 114 101  15 115 108   1  38  57  43 108  89
    112 111 105 108  43  34 115 109 111  57  36  55  51  32  53
    103  97  34  40 126 115  49  43  35  96 101  60  60  37  33
    101 101 105 114  35  49  61  61  19  42  52  12  35  63  44
     41  38  58 115 109 111 110  34  42  39  61 125  63  33  62
     36  33  54 119 102  67 108 101 114 115  35  42  39  56  32
     60  48  53 111 116 108  53  58  33  49  60  44 100 109  60
     35 124 102 101 108  53  51  33  36  16  38  42  26  33  35
     53  42  42  36 101 111 115 119  60  44  34  49  55  61  51
     42 110 101  79 114 115 112 111  59  41  49  39  33  62 111
     58  41  43  38  54  62  44  44  70  79  54  54  54 111  57
     57  43  49  39  37  46  61  41 109  33 122 106  69 105 108
    101 114  33  53  59  60  62  43 114  32 112 100 105  62  54
     53  12  57  33  57  57  49 122 116 113  97 118 107 108   9
     99  13  69  67  40  32  52 115  54  38  39  37  54  58  58
     62  40  22  56  42  39  48  56 103   5 101 127  88 115 112
    111 105   0  30  98  14 112 114 105   0  30  98  14 126  44
     40  60  44  38  50  60  38  51  41 109 123  89 112 111 105
    108  55  55  39  37  61  39 108  53  39  61  51  59  60  45
     49  55 123 119 111 110  98  47  61  58  62 103   5 101 108
     88  89  52  42  47 108  40  51  58  62 103  96 118  79 114
    115 112 111  58  41  43  38  54  62  44  44 108 120 114  32
     53  59  60  60 109 123  89 112 111 105 108  35  61  33 112
     38 105  37  43 114  33  49  33  46  41 109 100 122 106  69
    105 108 101 114 115 112 111 105  63  44  62  63  41  16  58
     45  60  59  61  55 111 116 108  54  55  61  36  42  39  47
     32 122 122  90 111 105 108 101 114 115 112 111  57  62  44
     60  39 120  41  32  34  44  33  59  57  33  46  19  49  61
     38  51  39  97  63  44  62  63  41  16  58  45  60  59  61
     55 102  96  70  79  59  53 112  16  22  34  36  63  54  15
     16 105 113 120 114 116  15  16  36  45  44  60  12  15 104
    115  70 101 114 115 112  34  40  37  43 122 122  90 111 105
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo