Lab
CMPS12B-02Lab2/grammar-emojistory.txt
emoji:😀,😂,😍,🤠,🤡,👿,😈,👽,☠️,💀,💩,👻,👾,🤖,🖖,👋,💋,🐙,🦋,🌙,⭐️,🌟,✨,🌈,☃️,🥙,⚽️,🏀,🎾,🚕,🚗,🚌,🎳,🎯,🎺,🎸,🥁,⛱,🗻,🔧,🔨,💰,💸💵,🗑,🔌,💡,💎,⚙️,⏰,🎈,🎁,🛏,📫,📎,❤️,💛,💚,💙,💔,🖤,💜,💕,💞,💖,💘,💓,💞 origin:How was my day? First I was like #emoji# then #emoji# but eventually #emoji# #emoji#
CMPS12B-02Lab2/grammar-recipe.txt
hpnStart:Brax,Brock,Brash,Stump,Willow,Briar,Tangle,Grey,Fire,Ghost,Rush,Thrush,Star,Stage,Black,Red,Chamb,Whit,Gren,Brook,Bright,North,Hells,Saints,Fair hpnEnd: bridge,bury,ton,hall,wall,fright,dragon,bright,wind,borough,ford,blood,ly,brox,bray,bay,smith,town,lyn,ferry,ghost,haven,brook,oaks,ox,wood hpn:#hpnStart##hpnEnd# recipeName:#hpn#,#flavorMod#,#hpn#'s #flavorMod# flavorMod:special,dark,light,bitter,burnt,savory,flavorful,aromatic,fermented,herbal,pleasant,harsh,smoky,sweet,fresh,refreshing,somber,bright,perky,sullen,acidic,sour,peaty,juicy,perfumed,buttery,lush,brisk,strong,weak,tart,tangy,bold,overpowering,light,faint,subtle,pleasant flavor:spearmint,tobacco,agave,coffee,cocoa powder,chocolate,sea salt,kosher salt,brown sugar,cinnamon,motor oil,lavender,spice,black pepper,cardamom,pumpkin spice,caramel,toffee,butterscotch,peppermint,walnut,acid,pear,citrus,grenadine,smoke,iodine,coriander,cinnamon,acid,salt,sugar,maple,coffee,whiskey,regret,sorrow,blood,gasoline,grass,cigarettes,pine,tar,saltwater,rosewater,jasmine,espresso,green apple,#fruit#,#fruit#,#fruit#,#fruit#,#fruit# foodAdjective:fun,festive,healthy,bountiful,decadent sauceType:salsa,reduction,vinagrette,sauce,jam,puree,butter,jelly,preserves preparedMeat:crispy bacon,pancetta,salami,prosciutto,corned beef,pastrami,roast game hen,seared ahi juiceNoun:juice,juice,cider,puree,syrup,paste juiceAdj:sparkling,fizzy,carbonated,housemade,filtered,unfiltered,fresh-squeezed,fermented flavoring:beef stock,champagne,red wine,white wine,soy sauce,fish sauce,ketchup,rice vinegar,white vinegar,apple-cider vinegar fancyWater:sparkling water,#flavor#-infused water,carbonated water,mineral water,tap water herb:fennel,cilantro,mint,basil,Thai basil,oregano,peppermint,spearmint,rosemary spice:cardamom,saffron,cinnamon,chili,cayenne,coriander,black pepper,white pepper,ginger,za’atar largeFruit:kumquat,honeydew<melon>,cherimoya,peach,sugar apple,persimmon,green apple,jackfruit,damson plum,kiwi,lime,key lime,meyer lemon,pomegranate,green apple,pineapple,mandarin orange,blood orange,plum,bosque pear,fig,persimmon,durian,mango,lychee smallFruit:black cherry,raisin,cranberry,blueberry,raspberry,lingonberry,boysenberry,elderberry,black grape,champagne grape,blackberry,marionberry,açaí berry,blackcurrant fruit:#largeFruit#,#smallFruit# greens:baby spinach,endive,radicchio,arugula,beet greens,mustard greens,green onion,watercress nut:almond,macademia nut,cacao nib,walnut,pumpkin seed,cashew,pecan,pistachio vegetable:kale,summer squash,rampion,napa cabbage,rutabaga,carrots,fennel,spring onions,arugula,kabocha,artichokes sauce:#flavor#-#oil# vinagrette,#flavor# reduction oil:duck fat,coconut oil,sesame oil,toasted sesame oil,olive oil,EVOO cream:coconut milk,whipped cream,almond milk,hemp milk,organic milk,soy,fermented dairy,yoghurt,goat's milk bread:challah,raisin bread,pumpernickel,wheat bread,sourdough,white bread,rye,puff pastry greens:salad,greens,bowl flavor:#fruit#,#flavoring#,#spice#,#herb#,#nut# smallThings:#nut#s,#smallFruit#s mix:toss,combine,mix,puree saladName:#recipeName# #greens# preparation:diced,chopped,sauteed,minced,wilted,deep-fried veg:plaintains,onions,kale,spinach,artichoke hearts preparedVeg:#preparation# #veg# salad:#saladName# - #mix# #preparedVeg# and #preparedVeg# in #sauce# and garnish with #smallThings# pizzaRecipe:#flavor# and #preparedMeat# pizza,#flavor# and #preparedMeat# pizza on #bread# dough pizza:#recipeName# Pizza - #pizzaRecipe# origin:#salad#,#pizza#
CMPS12B-02Lab2/grammar-story.txt
animal:cat,emu,okapi emotion:happy,sad,elated,curious,sleepy color:red,green,blue name:emily,luis,otavio,anna,charlie character:#name# the #adjective# #animal# place:school,the beach,the zoo,Burning Man adjective:#color#,#emotion#, origin:once #character# and #character# went to #place#
CMPS12B-02Lab2/grammar-yourgrammar.txt
animal:cat,emu,okapi emotion:happy,sad,elated,curious,sleepy color:red,green,blue name:emily,luis,otavio,anna,charlie character:#name# the #adjective# #animal place:school,the beach,the zoo,Burning Man adjective:#color#,#emotion# origin:once #character# and #character# went to #place#
CMPS12B-02Lab2/Rule.java
CMPS12B-02Lab2/Rule.java
import
java
.
io
.
*
;
import
java
.
util
.
*
;
import
java
.
util
.
Random
;
/*
* A rule is an object with alternating plaintext and symbols. This class is used to store the expansion on the righthand
* side of a rule. The starting symbol is also represented using a Rule object. The main thing you do with a Rule object
* is expand any symbols that appear in it looking up the rule indexed by the symbol.
*/
public
class
Rule
{
private
static
Random
random
;
// The raw expansion string. It can include a mixture of text and symbols (hashtagged text)
private
String
raw
;
// Array with text and symbols as separate entries
private
String
[]
sections
;
public
static
void
setSeed
(
long
seed
)
{
System
.
out
.
println
(
"Set seed "
+
seed
);
random
=
new
Random
(
seed
);
// Create a new random number generator with the specified seed
}
public
String
expand
(
Hashtable
<
String
,
Rule
[]
>
grammar
)
{
String
[]
results
=
new
String
[
sections
.
length
];
for
(
int
i
=
0
;
i
<
sections
.
length
;
i
++
)
if
(
i
%
2
==
0
)
{
// even index - sections[i] is plain text
results
[
i
]
=
sections
[
i
];
}
else
{
// odd index - need to recursively expand
Rule
[]
expansions
=
grammar
.
get
(
sections
[
i
]);
Rule
selectedExpansion
=
expansions
[
random
.
nextInt
(
expansions
.
length
)];
results
[
i
]
=
selectedExpansion
.
expand
(
grammar
);
}
return
String
.
join
(
""
,
results
);
}
public
String
toString
()
{
return
raw
;
}
Rule
(
String
raw
)
{
this
.
raw
=
raw
;
sections
=
raw
.
split
(
"#"
);
}
}
CMPS12B-02Lab2/TraceryRecursion.java
CMPS12B-02Lab2/TraceryRecursion.java
/*
* START: TO DO: Import the packages you need to support your I/O operations.
*/
/*
* END: TO DO: Import the packages you need to support your I/O operations.
*/
public
class
TraceryRecursion
{
/*
* START: TO DO: outputGrammar(Hashtable<String, Rule> grammar, PrintStream ps)
* Change the code so everything that is currently output to the console using System.out.println is now output to the PrintStream
* using the PrinStream.println() method.
*/
public
static
void
outputGrammar
(
Hashtable
<
String
,
Rule
[]
>
grammar
,
PrintStream
ps
)
{
System
.
out
.
println
(
"\nGRAMMAR:"
);
for
(
String
key
:
grammar
.
keySet
()
)
{
String
line
=
""
;
line
+=
key
+
": "
+
String
.
format
(
"%1$"
+
(
20
-
key
.
length
())
+
"s"
,
" "
);
for
(
Rule
rule
:
grammar
.
get
(
key
))
{
line
+=
"\""
+
rule
+
"\","
;
}
System
.
out
.
println
(
line
);
}
}
/*
* END: TO DO: changing outputGrammar to use a PrintStream
*/
// Given an InputStream, load the grammar at that InputStream
public
static
Hashtable
<
String
,
Rule
[]
>
loadGrammar
(
InputStream
inStream
)
{
Hashtable
<
String
,
Rule
[]
>
grammar
=
new
Hashtable
<
String
,
Rule
[]
>
();
// TO DO: create a new BufferedReader based on inStream that you'll use to read the stream line by line (using readLine())
/*
* START: TO DO: Make a loop that reads a new line from the BufferedReader line by line and adds it to the grammar
*/
/*
* Put your code that takes each line and adds it to the grammar inside the loop. Below is the code from our solution for doing this,
* but feel free to substitute this with the code from your own assignment.
*/
/* String[] ruleString = line.split(":");
String[] expansions = ruleString[1].split(",");
Rule[] rules = new Rule[expansions.length];
for(int i=0; i < expansions.length; i++) {
rules[i] = new Rule(expansions[i]);
}
grammar.put(ruleString[0], rules); */
/*
* END: TO DO: Make a loop that reads a new line from the BufferedReader line by line and processes it.
*/
return
grammar
;
}
/*
* START: TO DO: public static InputStream getInputStream(String[] args)
*/
/*
* END: TO DO: public static InputStream getInputStream(String[] args)
*/
/*
* START: TO DO: public static PrintStream getOutputStream(String[] args)
*/
/*
* END: TO DO: public static PrintStream getOutputStream(String[] args)
*/
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Running TraceryRecursion..."
);
String
startSymbol
=
"#origin#"
;
int
count
=
10
;
long
seed
=
1
;
/*
* START: TO DO: call getInputStream(args) and getOutputStream(args) to get the InputStream and PrintStream to use
*/
/*
* END: TO DO: call getInputStream(args) and getOutputStream(args) to get the InputStream and PrintStream to use
*/
Rule
.
setSeed
(
seed
);
// Set the seed using a static method defined on Rule
// To DO: comment this line back in to load the grammar into the Hashtable once you've set the inputStream you're using
// Hashtable<String, Rule[]> grammar = loadGrammar(<put your InputStream variable here>);
// TO DO: comment this line back in to print the loaded grammar. You'll need to set outStream correctly
// outputGrammar(grammar, <put your PrintStream variable here>);
Rule
rule
=
new
Rule
(
startSymbol
);
// Create a new Rule object using the startSymbol
// Expand the start symbol until there are no more symbols to expand. Do this 'count' number of times.
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
// TO DO: Change the line below so it prints to the correct PrintStream instead of always System.out
// System.out.println(rule.expand(grammar));
}
}
}