Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Homework- Random Insult Generator (outline of code inside)

  1. Nov 9, 2006 #1
    Hey i'm new to this java stuff, Im taking my first class in it at the university of pittsburgh, and for my homework I need to write code to make a thing that will generate 10 random insults from a list of words. I have this outline to it and I was wondering if someone could help me fill in the blanks (i.e. I know i need to make a while/for loop that says something along the lines of while x < 10 blah blah x++). Here's the outline:
     
  2. jcsd
  3. Nov 9, 2006 #2
    thats interesting...its almost like BSP. I haven't done Java in a few years, but I think my

    general knowledge of programming might help. I might need some help filling in the java

    blanks though.

    Code (Text):


    import java.util.Random ;


    public class Generator {


    ////////////////////////////////////////////////////////////////////
    //
    // Generate 10 sentences.
    //

    public static void main(String[] args) {
    for (int n = 0 ; n < 10 ; n++) {
    System.out.println( generate("S") ) ; [COLOR="SeaGreen"] ok so here we print out whats

    returned from generate.  we do this 10 times so generate will return a string with the insult

    [/COLOR]
    }
    }


    ////////////////////////////////////////////////////////////////////
    //
    // Completely expand category (usually S).
    //

    public static String generate(String category) {
    [COLOR="SeaGreen"]ok having looked at the Select_Expansion function, it seems that all you have to do is call the function with the correct category.  This leads me to believe i've underestimated the function.  have you tried calling Select_Expansion() here with the category "S"?[/COLOR]
    }


    ... YOUR OTHER FUNCTIONS HERE ...


    ////////////////////////////////////////////////////////////////////
    //
    // Select_Expansion(Category) will randomly expand Category
    // according to the following grammar. Returns "error" if there is
    // no expansion.
    //

    static String[] Grammar = {
    "S -> NAME VP",
    "VP -> V NP",
    "NP -> ART NN",
    "NN -> ADJ NN",
    "NN -> N",
    "ART -> a",
    "V -> is",
    "V -> looks like",
    "V -> acts like",
    "NAME -> dr aronis",
    "NAME -> mr aronis",
    "NAME -> john",
    "ADJ -> nice",
    "ADJ -> mean",
    "ADJ -> smart",
    "ADJ -> stupid",
    "ADJ -> hip",
    "ADJ -> geeky",
    "ADJ -> normal",
    "ADJ -> strange",
    "ADJ -> great",
    "ADJ -> terrible",
    "N -> person",
    "N -> teacher",
    "N -> fool"
    } ;

    static Random generator = new Random() ;

    public static String Select_Expansion(String Category)
    {
    // Variables:
    String This_Rule = "", Rule_LHS = "", Rule_RHS = "" ;

    int Number_of_Expansions = 0, Expansion_to_Use = 0 ;
    // Count expansions for Category:

    for (int n = 0 ; n < Grammar.length ; n++)
    {
    This_Rule = Grammar[n] ;
    Rule_LHS = This_Rule.substring(0,This_Rule.indexOf( " -> ")) ;

    if ( Rule_LHS.equals(Category) )
    {
                Number_of_Expansions++ ;
    }

    }
    // Return error if no expansions:
    if ( Number_of_Expansions == 0 )
    {
         return "error" ;
    }

    // Randomly select which expansion to use:
    Expansion_to_Use = Math.abs(generator.nextInt()Number_of_Expansions);

    // Go through rules while counting down through expansions of Category,
    // and break with selected expansion:
    for (int n = 0 ; n < Grammar.length ; n++)
    {
    This_Rule = Grammar[n] ;
    Rule_LHS = This_Rule.substring(0,This_Rule.indexOf( " -> ")) ;
    Rule_RHS = This_Rule.substring(This_Rule.indexOf(" -> ") + 4) ;
    if ( Rule_LHS.equals(Category) && (Expansion_to_Use == 0) ) break ;
    if ( Rule_LHS.equals(Category) ) Expansion_to_Use-- ;
    }

    return Rule_RHS ;
    }


    ////////////////////////////////////////////////////////////////////

    }

    // End-of-File


     
     
  4. Nov 9, 2006 #3

    0rthodontist

    User Avatar
    Science Advisor

    First, the code you've shown looks moderately smooth, so I'm going to guess it was given to you as part of the assignment (am I wrong?). What you need to fill in, in the generate function, is the following:

    --select the next expansion using the function they gave you
    --split up that expansion into some number of words, and do generate for each of those words

    You'll need to do a bunch of other stuff too but this should get you started.
     
  5. Nov 10, 2006 #4
    A bit complex task for first lessons, grammar and stuff, anyway I do not know who teaches you Java but

    Code (Text):

    ////////////////////////////////////////////////////////////////////
    //
    // Select_Expansion(Category) will randomly expand Category
    // according to the following grammar. Returns "error" if there is
    // no expansion.
    //
     
    THIS IS SOMETHING YOU DO NOT DO IN JAVA

    Use JavaDocs! And conventionaly you use _ for naming only in constants.

    "S -> NAME VP",
    "VP -> V NP",
    "NP -> ART NN",
    "NN -> ADJ NN",
    "NN -> N",
    "ART -> a",

    That means you want a sentance in the form of

    NAME + VERB + ARTICLE + ADJECTIVE + NOUN?

    If possible maybe you can simplify a little and make 5 arrays of Strings
    one array for names one for verb one for article.... and then you just randomly select one entry from the each array when making the String. Use StringBuffer or StringBuilder when making a long String. You would have something in the form:

    Code (Text):

    String generateRandomSentance(){
    StringBuilder builder = new StringBuilder();
    builder.append(getRandomName());
    builder.append(getRandomVerb());
    ....
    return builder.toString();
    }
     
    P.S.: Only a certified Java programmer should be allowed to teach Java.
     
    Last edited: Nov 10, 2006
  6. Nov 10, 2006 #5

    0rthodontist

    User Avatar
    Science Advisor

    I thought it wasn't a bad way to write that program. It introduces him to context-free grammars, which is useful. Also, the sentences according to the grammar do not necessarily take the form you stated, since they may have more than one adjective. Certainly it could be made faster by using a bunch of arrays (or a HashMap<String, String[]>) instead of string parsing to extract the grammar, but speed isn't a concern.

    By the way, I'm unsure what you think should be done in JavaDoc instead of Java. The String[] Grammar is part of the program.
     
    Last edited: Nov 10, 2006
  7. Nov 10, 2006 #6
    JavaDoc comment is a Java Style Comment

    e.g.
    Code (Text):

    /**
     * Adds the two numbers together and returns the result.
     * @param a first int to add
     * @param b second int to add
     * @return returns the value of a + b
     */
    public int add(int a, int b){
    return a + b;
    }
     
    the you can use the JavaDoc utility to make html style documents from the source code as I am sure you have came across this page

    http://java.sun.com/j2se/1.5.0/docs/api/

    all the pages are generated from the JavaDoc comments in the sources of the standard library. Therefore one is supose to write JavaDoc comments and not

    Code (Text):

    ////////////////////
    // My own
    // non Java style
    // comment
    //////////////////
    // Methods returns a + b
    ///////////////////
    public int add(int a, int b){
    return a + b;
    }

     
    I would not introduce advanced things such as context-free grammars and complex data structures such as a HashMap in the beginning of the course. First you need to know how to crawl, then to walk and then finally to run, but ofcorse then you also need to learn how to take the bus, but for first lessons lets first crawl:)
     
  8. Nov 10, 2006 #7

    0rthodontist

    User Avatar
    Science Advisor

    I know what javadoc is, having written a fair amount of it myself. I guess it would have been a bit better to use that for this, though it's such a small program it doesn't make too much difference. The only thing that annoyed me a little was the capitalized method and variable names.

    Context free grammars are easy, just a bunch of replacement rules--I didn't know what they were until a discussion in Theory of Computation, and it took about 5 minutes to get the basic concept (it wasn't even really explained, just learning by example).

    But maybe for a beginning programmer it is hard to learn them just from reading code. d33bo, do you understand what Select_Expansion does? Can you give an example of what might be its output if you called Select_Expansion("ADJ")? How about an example of what its output might be if you called Select_Expansion("NP")? Select_Expansion("S")?

    A pretty good introduction to context-free grammars can be found at Wikipedia, by looking up context-free grammar. Alternatively, here's one with more pictures of parse trees, which make it intuitive:
    http://www.coli.uni-saarland.de/~kris/nlp-with-prolog/html/node37.html
     
    Last edited: Nov 10, 2006
  9. Nov 10, 2006 #8
    OK I'm pretty lost, I'm not gonna lie, but to answer the question about what Select_Expansion would do for things. I understand that select_expansion for lets say S could return dr.aronis or john etc, and the VP part of it could return any V and N combo. I understand that, but what I don't understand is what you're telling me to do lol. Looking at the thing Bucky posted, is that code actually doing anything or did you just add comments? Am I adding these expansions etc into the place in my outline that says "YOUR FUNCTIONS HERE" or what? I'm truly lost.
     
  10. Nov 10, 2006 #9

    0rthodontist

    User Avatar
    Science Advisor

    No, Select_Expansion only goes one level down. For example:
    Select_Expansion("S") returns "NAME VP"
    Select_Expansion("NP") returns "ART NN"
    Select_Expansion("ADJ") returns one word chosen from "nice", "mean", "smart", "stupid", "hip", "geeky", "normal", "strange", "great", or "terrible".
     
  11. Nov 11, 2006 #10
    I have a couple of questions:

    1. For a beginning course in Java, couldn't you write a much simpler program? I understand if you want to make it more complex, I'm just wondering if all the grammar stuff is required. Personally, I'd create a.) a string[] array containing insulting nouns, b.) a string[] array of insulting adjectives, and c.) a method that takes a random element of each array and returns a String that's something like "you " + adjective + noun + "!".

    2. Some of your code looks complicated, especially the Select_Expansion method. Have you tested this section of the code separately?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Java Homework- Random Insult Generator (outline of code inside)
  1. Random # Generator (Replies: 2)

Loading...