Lab 1: Recursion Introduction Tracery (tracery.io) is a simple text-expansion language made by one of your TAs as a homework assignment for one of Prof. Mateas's previous courses. It now has tens of thousands of users, and runs about 7000 chatbots on Twitter (you never know where a homework will take you!). Tracery uses context-free grammars ( https://en.wikipedia.org/wiki/Context-free_grammar) to store information about how to expand rules. A Tracery grammar is a set of keys, each of which has some set of expansions that can replace it. In our version, the line "beverage:tea|coffee|cola|milk" means that the symbol "beverage" can be replaced with any of those four options. You replace a symbol whenever you see it in hashtags. This rule "#name# drank a glass of #beverage#" will have the "name" and "beverage" symbols replaced with expansions associated with those symbols. In the case of the "beverage" rule above, which has four possible expansions, one will be picked at random. If the replacement rule also has hashtags in it, we replace those, and if those replacements have hashtags.... we keep replacing things
until all the hashtags are gone, recursively. In this assignment, you will be implementing a simplified version of Tracery in Java, and then using it to generate generative text. You will also be writing your own grammar to generate new texts (hipster cocktails, emoji stories, or nonsense poems). Outline ● Compile and run a Java program ● Save all the arguments ● Load the Tracery files ● Output all the rules ● Expand rules and print them to the screen Compile and run a Java program This program has several files (Rule.java, and TraceryRecursion.java). We can't run these files as code, as we would with other “interpreted” languages (like Javascript https://en.wikipedia.org/wiki/Context-free_grammar or Python). For Java, we need the computer to put them all together and translate it to machine code, in a process called “compiling”. You will compile and run your Java program from the command line. When you see $, this means that it is a command that you will enter in the command line. Windows and
Unix (such as Linux or the Mac terminal) command lines are a little different, be sure you know the basics of navigating the one you are using. Do you have Java installed on your machine? What version? Let's find out! Type the following into your command line: $ javac -version $ java - version You should have at least Javac 1.8 and Java 1.8 (often called “Java 8”) installed. If that's not the case, this is a good time to fix that by updating your Java. We will be using some features of Java 8 in this class. Compile your java program. $ javac TraceryRecursion.java So far, it will compile without errors. Look in the folder, and you will see that you have a new file TraceryRecursion.class. This is the compiled version of your file. You can now run this file by typing: $ java TraceryRecursion You should get the output Running TraceryRecursion.... Try typing javac TraceryRecursion or java TraceryRecursion.java. Both of these should give your errors. Compiling requires the .java extension, and running requires not having it. But it’s easy and common to misremember that, so look at what there errors look like, so that when it happens later, you know what
caused it. You can compile and run in separate steps. But sometimes you want to compile and run with one line, and you can combine them like this: $ javac TraceryRecursion.java && java TraceryRecursion . Store arguments Many java programs need outside information provided by the user. This lets users change the behavior of the program (or tell it which data to use) without having to modify the source code. The main method of TraceryRecursion will be the first method called when running the program. This method is static, meaning that the method is associated directly with a class and can thus be called without creating an object that is an instance of the class, and its return type is void, meaning that it won't return anything. It also has parameters, an array of Strings called args (you could call this parameter anything, but the tradition is to call it args or arguments). Our program will take 0-4 arguments: ● the name of the file containing the grammar (e.g.: "grammar.txt") ● the starting symbol to begin the grammar expansion (e.g.: "#story#") ● the number of times we want to perform that expansion (e.g:
"5") ● a seed value for the random number generator (e.g: "123") (Seeded random values allow us to make the random generator predictable) You can see that in main, each variable is given a default value. For example, if no seed value is given in the arguments, the seed value will be the current time (this means you will get different random results each time you run the program, unless a specific seed is specified in the arguments). TODO #1 For each of these parameters, test whether enough arguments have been specified to set that parameter. For example, to set the grammarFile parameter there would have to be at least one argument given. You can test the length of the args array to determine how many arguments have been passed in. Override each of these default parameters with its argument override. For the numbers, you will need to use Integer.parseInt and Long.parseLong to turn the arguments from Strings into numbers. NOTE: Do not add or remove code outside of the START and END comments. Keeping all your code between those two comments will help us grade your work. Load a Tracery file
Java has many helpful built-in data structures: Lists, Arrays, and Hashtables are only a few of them. In this assignment, we will be loading and storing a Tracery grammar as a Hashtable. The Hashtable has a list of keys, and each key has some data associated with it, much like a dictionary stores words with their definition. Hashtables come with some helpful methods included: ● table.get(String s) returns the data for that key, but it will throw an error if that key is not in the table. ● table.put(String s, DATA) creates an entry for that key (or replaces it) and stores the data there. In this part of the assignment, we will implement loadGrammar, which takes a String representing a file path, then loads a file, parses it (breaking it into sections), and stores it in our grammar object. In the loadGrammar method, we have already added the line that creates the Hashtable. You can see that the data type has a weird ending <String, Rule[]>, which tells Java that this Hashtable uses Strings to look up arrays of Rules, so if we try to use numbers as look up values, or store Strings instead of arrays of Rules, Java knows to throw an error.
We also included a basic way to load a file. Loading files might cause an IO exception (for example, if the file does not exist), so for complex reasons, we need to have the loading line new String(Files.readAllBytes(Paths.get(path))) wrapped in a try/catch block. We will talk more about throwing and catching exceptions later this quarter. For right now, this loads a whole file as a string. By appending .split('r?n') on the end, we take the resulting string and splits it into an array of lines. TODO #2 For each line in the array of lines, store the data in the grammar hashtable. You can use either a familiar for (int i = 0...) loop, or use the newer for (String line: lines) loop, if you don't need to see the index value. We used the split String method above to split apart the file into lines. Use split again, but splitting on the character ":". Everything before the ":" is the key. Store it in a variable named key. Everything after it is the rule expansion(s). First split apart the rules into an array of Strings. For each line in the file, this will give you a String array of length 2, where the first element of the array is an expandable symbol (which will be a key in the grammar hashtable), and the
second element of the array is the expansion(s) for this symbol. But there is possibly more than one expansion for a symbol. If you look at the grammar files, you can see where there is more than one expansion for a symbol when there's a comma-delimited list of expansions after a symbol. So we have to further split the expansions into an array of Strings, one for each expansion (by splitting on ","). Once you have an array of Strings (of expansions) we have to turn it into an array of Rules. Initialize an array, Rule[], of the right size. Use another loop (inside the loop iterating through lines) to iterate through your array of strings and, for each expansion, create a Rule (it takes a single string as a parameter), and add it to your array of Rules. You now have the right kind of data (Rule[]) to store in the hashtable. Store your data in grammar. What was that method that stores data in a grammar (hint: we describe it above)? In the main() method there's already a call to outputGrammar; this prints your grammar to the console, so you can verify that it loaded it correctly (that the correct expansions are associated with the correct symbols). Implementing Recursion
Now that you have successfully loaded a file and stored it as a Hashtable<String, Rule[]> grammar, we can implement the method that generates the generative text! Open up Rule.java, and look at the constructor for this class: Rule(String raw). This constructor takes the string that you passed to it in TODO #2 and splits it into sections (using "#"). The even indexed entries of sections are plain text (ie. the zeroth, second, fourth, etc.), and the odd indexed entries of the variable sections are symbols for expanding (ie. the first, third, fifth, etc.). This might seem a bit confusing at first. In an expansion like "once #character# and #character# went to a #place#" it's clear that splitting on # will cause the zeroth, second and fourth elements (the even elements) to be plain text (i.e. "once", "and" and "went to a") and the first, third and fifth elements (the odd elements) to be symbols (i.e. "#character#", "#character#", "#place#"). But consider the expansion "#name# the #adjective# #animal#". Won't this even and odd approach get reversed, since the expansion starts with a symbol? It turns out it won't because, if there's a symbol at the beginning, or two symbols right next to each other, splitting on "#" produces two strings, the empty string (which is what is before the "#") and then the string with the symbol (which is what is after the "#", minus the closing "#" since we're splitting on "#"). So splitting this expansion on # will produce the following array:
sections[0]: "" sections[1]: "name" sections[2]: "the" sections[3]: "adjective" sections[4]: "" sections[5]: "animal" So the even and odd relationship still works out, it's just that two of our even entries are the empty string (which is still plain text), which, when we include it in the output of the expansion, won't be visible, and so won't cause us any problems. Phew! Now that we've explained that we can get back to actually writing the code in Rule.java to expand the text. The method expand takes a grammar (the type is Hashtable<String, Rule[]>) as an argument, and returns the expanded String. But right now, it only returns a copy of its original expansion (with any symbols not expanded). We want it to instead recursively expand the odd sections. TODO #3 Create an array of Strings named results that is the same size as the rule sections ( sections.length). This is where we will store the results of expanding each section.
Create a for-loop that iterates down the sections array. Since we need to know whether each section is odd or even, you will need to use for (int i = 0 ...), rather than for (String section: sections) style loops. For each section, if it is even ( i%2 == 0), copy the text from the rule section into the results array. If it is odd, then this is a symbol that we need to expand. Use the grammar to find the array of expansions for this symbol. We need to pick a “random” index in the array, but we will use the seeded random number generator (so we can reproduce our results using the same random seed). You can get a random int with random.nextInt(int bound). This will return a random integer between 0 (inclusive) and the bound argument you pass in (exclusive). Since you're randomly picking from an array of possible expansions, what's the largest random value you will want? Pick out an expansion, and save it as the variable Rule selectedExpansion. We want to store that new value in our results array, but since is is an expansion string we need to process it first because it might contain symbols that need to be expanded as well. What method should you call on selectedExpansion to do that? Store the resulting text in the results array. We now have results values for each section in this rule! Use String.join("", results) to join all of the results together into a single string (with no spaces between
them), and return the results of that operation. This new return statement should replace the return statement that was in the starter code. That is, your new return statement should replace the line: return "[" + raw + "]"; Finishing up Run your program. It should now output however many outputs of the text you want (as specified by the count parameter). Try it with and without a seed value. Does it always output something new? Try out a few other grammars, we included grammar-recipe.txt which makes bad recipes, and grammar- emojistory.txt which tells stories with emoji. Now edit grammar-yourgrammar.txt. Create something interesting (of about equal complexity to grammar-story.txt). We won't judge you on what it makes, consider this a freeplay exercise. Some inspirational twitterbots include @unicode_garden, @DUNSONnDRAGGAN, @orcish_insults, @GameIdeaGarden , @indulgine, @CombinationBot, @thinkpiecebot, @infinite_scream, FartingEmoji. Make sure your grammar generates without causing errors! We didn't implement a graceful way to recover from bad formatting or missing symbols, so these grammars are
brittle, they break easily. Turn in Wow, you're done! Congrats on finishing your first 12B lab. Checklist: ● Works for no command‑line arguments, a few, or all of them ● If a random seed is specified, the output stays consistent. If not, it changes ● If you switch the grammar file, you get output from a different grammar ● You have edited grammar-yourgrammar.txt and it runs. To check that your code is right, compare it with the following output. This output assumes that you’re using Random.nextInt() to generate the random numbers (as described above). This is running TraceryRecursion with the following arguments: $ java TraceryRecursion grammar-story.txt "#origin#" 5 10 The output should be: Running TraceryRecursion... with grammar:'grammar-story.txt' startsymbol:'#origin#' count:5 seed:10 Set seed 10 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# GRAMMAR: adjective: "#color#","#emotion#", place: "school","the beach","the zoo","Burning Man", emotion: "happy","sad","elated","curious","sleepy", origin: "once #character# and #character# went to #place#", color: "red","green","blue", name: "emily","luis","otavio","anna","charlie", character: "#name# the #adjective# #animal#", animal: "cat","emu","okapi", once anna the green emu and anna the sleepy emu went to the zoo once emily the green emu and anna the green cat went to school once charlie the blue okapi and otavio the blue emu went to school once emily the blue emu and otavio the blue cat went to the beach
once emily the sad okapi and charlie the elated okapi went to the zoo To turn in your code: ● Create a directory with the following name: <student ID>_lab1 where you replace <student ID> with your actual student ID. For example, if your student ID is 1234567, then the directory name is 1234567_lab1. ● Put a copy of your edited TraceryRecursion.java, Rule.java and grammar-yourgrammar.txt files in the directory. ● Compress the folder using zip to create. Zip is a compression utility available on mac, linux and windows that can compress a directory into a single file. This should result in a file named <student ID>_lab1.zip (with <student ID> replaced with your real ID of course). ● Upload the zip file through the page for Lab 1 in canvas ( https://canvas.ucsc.edu/courses/12730/assignments/36933). Additional resources ● How should I name things in Java? https://www.javatpoint.com/java‑naming‑conventions ● Basic Java Syntax
https://www.tutorialspoint.com/java/java_basic_syntax.htm ● Need some in‑depth Java tutorials? There are free courses online: https://www.codecademy.com/learn/learn‑java , https://www.udemy.com/java‑tutorial/ ● Command line basics: https://hellowebbooks.com/learn‑command‑line/ ● Get Linux/OSX style command line tools on Windows with Cygwin https://cygwin.com/install.html https://canvas.ucsc.edu/courses/12730/assignments/36933 https://www.javatpoint.com/java%E2%80%91naming%E2%80% 91conventions https://www.tutorialspoint.com/java/java_basic_syntax.htm https://www.codecademy.com/learn/learn%E2%80%91java https://www.udemy.com/java%E2%80%91tutorial/ https://hellowebbooks.com/learn%E2%80%91command%E2%80 %91line/ https://cygwin.com/install.html Utilizing the readings from the text and your research, provide an in-depth analysis of 3 social media platforms and how businesses are utilizing them to reach new and existing customers. Specific groups to be addressed are Generation X, Generation Y, Millennials, Baby Boomers, and 60+ market segments. An example of a discussion idea is to research the trend toward mobility in social media and write a paper on how the different markets described in your Week 1 Individual Project use technology. Note: Research is to be academic or professional in scope. Use of blogs, personal Web sites, corporate Web sites, wikis, or
other social-media-related sources are not acceptable. A minimum of 4 pages is required. Title and abstract pages do not count toward the page minimum. A minimum of 5 professional or academic references is required to support your paper and to provide additional discussion points. 17 VIDEO AND PODCASTING MADE EASY Creating audio and video content for marketing and PR purposes requires the same attention to appropriate topics as other techniques outlined in this book. It requires targeting individual buyer personas with thoughtful information that addresses some aspect of their lives or a problem they face. By doing so, you brand your organization as smart and worthy of doing business with. However, unlike text-based content such as blogs or news releases, audio and video might require a modest investment in additional hardware such as microphones and video cameras, as well as software, and, depending on the level of quality you want to achieve, may also necessitate time- consuming editing of the files. Although the actual procedures for podcasting and video are a bit more convoluted than, say, starting a blog, they are still not all that difficult. Video and Your Buyers Organizations that deliver products or services that naturally lend themselves to video have been among the first to actively use the medium to market and deliver information about their offerings. For example, many churches routinely shoot video of weekly services and offer it online for anyone to watch, drawing more people into the congregation. Many amateur and professional sports teams, musicians, and theater groups also use video as a marketing and PR tool. Video follows both blogs and podcasting on the adoption curve at organizations that don't have a service that naturally lends itself to video. 303Companies are certainly experimenting, typically by embedding video (hosted at YouTube or another video site) into their existing blogs and online media rooms. I'm also seeing video snippets of CEO speeches, customer
interviews, and quick product demonstrations. Business-Casual Video In the United States, there has been a 20-year trend toward so- called business-casual clothing in the workplace. My first job, on Wall Street in the 1980s, required me to wear a suit and tie wi 18 HOW TO USE NEWS RELEASES TO REACH BUYERS DIRECTLY Guess what? Press releases have never been exclusively for the press. My first job in the mid-1980s was on a Wall Street trading desk. Every day, I would come to work and watch the Dow Jones Telerate and Reuters screens as they displayed specialized financial data, economic information, and stock prices. The screens also displayed news feeds, and within these news feeds were press releases. For decades, financial market professionals have had access to company press releases distributed through Business Wire, PR Newswire, and other electronic press release distribution services. And they weren't just for publicly traded corporations; any company's release would appear in trading rooms within seconds. I distinctly remember traders intently watching the newswires for any signs of market-moving events. Often the headline of a press release would cause frenzy: “Did you see? IBM is acquiring a software company!” “It's on the wire; Boeing just got a 20-plane order from Singapore Airlines!” For years, markets often moved and stock prices rose and fell based on the press release content issued directly by companies, noton the news stories written minutes or hours later by reporters from newswire outlets like Reuters and Dow Jones (and later Bloomberg). Press releases have also been available to professionals working within corporations, government agencies, and law firms, all of which have had access to press releases through services like those from NewsEdge, Dow Jones, and LexisNexis. These
services have been delivering press releases to all kinds of professionals for competitive intelligence, research, discovery, and other purposes for decades. 316 And since about 1995, the wide availability of the web has meant that press releases have been available for free to anyone with an Internet connection and a web browser. Millions of people read press releases directly, unfiltered by the media. You need to be speaking directly. YOUR NEWSROOM: A FRONT DOOR FOR MUCH MORE THAN THE MEDIA The online newsroom (sometimes called a press room, media room, or press page) is the part of your organization's website that you create specifically for the media. In some organizations, this section is simply a list of news releases with contact information for the organization's PR person. But many companies and nonprofits have elaborate newsrooms with a great deal of information available in many different formats: audio, video, photos, news releases, background information, financial data, and much more. A close cousin to the newsroom is the online investor relations (IR) room that many public companies maintain; however, I don't cover IR sites in this book. Before I give you ideas on how to create a valuable newsroom of your own, I want you to consider something that is vitally important: All kinds of people visit your newsroom, not just journalists. Stop and really let that soak in for a moment. Your buyers are snooping around your organization by visiting the media pages on your website. Your current customers, partners, investors, suppliers, and employees all visit those pages. Why is that? Based on casual research I've done (I often speak about visitor statistics with employees who are responsible for their organizations' newsrooms), I'm convinced that when people want to know what's currentabout an organization, they go to a newsroom. Your newsroom is for your buyers, not just the media.
329Visitors expect that the main pages of a website are basically static (i.e., they are not updated often), but they also expect that the news releases and media-targeted pages on a site will reveal the very latest about a company. For many companies, the news release section is one of the most frequently visited parts of the website. Check out your own website statistics; you may be amazed at how many visitors are already reading your news releases and other media pages online. So I want you to do something that many traditional PR people think is nuts. I want you to design your newsroom for your buyers. By building a media room that targets buyers, you will not only enhance those pages as a powerful marketing tool but also make a better media site for journalists. I've reviewed hundreds of newsrooms, and the best ones are built with buyers in mind. This approach may sound a bit radical, but believe me, it works. When news releases are posted on your site, search engine crawlers will find the content, index it, and rank it based on words, phrases, and other factors. Because news release pages update more often than any other part of a typical organization's website, search engine algorithms (tuned to pay attention to pages that update frequently) tend to rank news release pages among the highest on your site, driving traffic there first. “There's no question that a well-organized media room often has higher search results and drives more traffic because of the way the search engines work,” says Dee Rambeau, vice president of customer engagement at PR Newswire. “A news release dynamically builds out a new set of content in your newsroom, with each news release generating its own indexable page, which the search engines all capture. Google and the other search engines love fresh content that relates back to similar content on the other pages of the site. Aggressive companies take advantage of this by sending news releases frequently to get high rankings from the search engines. Frequency has a
great deal to do with search engine rankings—if you do 10 news releases, that's great; 20 is better, and 100 is better still.” The Kellogg Company—the world's leading producer of cereal and second-largest producer of cookies, crackers, and savory snacks—uses its newsroom1for search engine optimization (SEO) purposes and as a tool to reach various audiences, including reporters and editors who cover the company. “What we found through our research is that, more and more, our newsroom is extending beyond just media to other stakeholders,” says Stephanie Slingerland, senior manager of corporate communications at the Kellogg Company. “Anyone coming to the site, be it an investor, an NGO or other partner agency, or even a consumer—the newsroom is inviting for them to get the information which may be relevant.” Slingerland has strong partnerships with people who work in other departments at the company and who also provide information to the public. “Our investor relations team are having conversations and engagements with analysts and investors. So, from our partnership with that team, we know what those stakeholders might be looking for. Or, for example, our government relations team are regularly engaging with government and nongovernmental officials. Again, we have a strong partnership with them. We know what they're looking for and can make sure that they have what they might be looking for on our site. The same with our corporate social responsibility team, who engage with agencies and others as part of our philanthropic activities.” Based on what she learns about the needs of the news media and other stakeholders, Slingerland creates the right content, including news releases, fact sheets, news alerts, and more. “Since we are the folks that regularly engage with the media, we know what we're getting asked for over and over, like the fact sheet section,” she says. “And we also know that many people want to know the latest news about the company, but they don't necessarily come to our newsroom every day. So
that's why we created our news alerts section, so they opt in to be alerted whenever One important consideration that many marketing and PR people overlook when considering the benefits of a newsroom is that you control the content, not your IT department, webmaster, or anyone else. Youshould design your newsroom as a tool to reach buyers and journalists, and you don't need to take into consideration the rules for posting content that the rest of the organization's site may require. If you build this part of your site using a specialized newsroom content-management application, such as the MediaRoom2product from PR Newswire, you will control a corner of your organization's website that you can update whenever you like using simple tools, and you won't need to request help from anyone in other departments or locations. So start with your needs and the needs of your buyers and journalists, not the needs of those who own the other parts of your organization's website. Start with a Needs Analysis When designing a new newsroom (or planning an extensive redesign), start with a needs analysis. Before you just jump right into site aesthetics and the organization of news releases, take time to analyze how the site fits into your 332larger marketing, PR, and media relations strategy. Consider the buyer persona profiles you built as part of your marketing and PR plan. Talk with friendly journalists so you can understand what they need. Who are the potential users of the newsroom, and what content will be valuable to them? When you have collected some information, build buyers' and journalists' needs into your newsroom. As you work toward starting your design, try to think a bit more like a publisher and less like a marketing and PR person. A publisher carefully identifies and defines target audiences and then develops the content required to meet the needs of each distinct demographic. Graphical elements, colors, fonts, and other visual manifestations of the site are also important but
should take a backseat during the content needs analysis process.

Lab 1 Recursion  Introduction   Tracery (tracery.io.docx

  • 1.
    Lab 1: Recursion Introduction Tracery(tracery.io) is a simple text-expansion language made by one of your TAs as a homework assignment for one of Prof. Mateas's previous courses. It now has tens of thousands of users, and runs about 7000 chatbots on Twitter (you never know where a homework will take you!). Tracery uses context-free grammars ( https://en.wikipedia.org/wiki/Context-free_grammar) to store information about how to expand rules. A Tracery grammar is a set of keys, each of which has some set of expansions that can replace it. In our version, the line "beverage:tea|coffee|cola|milk" means that the symbol "beverage" can be replaced with any of those four options. You replace a symbol whenever you see it in hashtags. This rule "#name# drank a glass of #beverage#" will have the "name" and "beverage" symbols replaced with expansions associated with those symbols. In the case of the "beverage" rule above, which has four possible expansions, one will be picked at random. If the replacement rule also has hashtags in it, we replace those, and if those replacements have hashtags.... we keep replacing things
  • 2.
    until all thehashtags are gone, recursively. In this assignment, you will be implementing a simplified version of Tracery in Java, and then using it to generate generative text. You will also be writing your own grammar to generate new texts (hipster cocktails, emoji stories, or nonsense poems). Outline ● Compile and run a Java program ● Save all the arguments ● Load the Tracery files ● Output all the rules ● Expand rules and print them to the screen Compile and run a Java program This program has several files (Rule.java, and TraceryRecursion.java). We can't run these files as code, as we would with other “interpreted” languages (like Javascript https://en.wikipedia.org/wiki/Context-free_grammar or Python). For Java, we need the computer to put them all together and translate it to machine code, in a process called “compiling”. You will compile and run your Java program from the command line. When you see $, this means that it is a command that you will enter in the command line. Windows and
  • 3.
    Unix (such asLinux or the Mac terminal) command lines are a little different, be sure you know the basics of navigating the one you are using. Do you have Java installed on your machine? What version? Let's find out! Type the following into your command line: $ javac -version $ java - version You should have at least Javac 1.8 and Java 1.8 (often called “Java 8”) installed. If that's not the case, this is a good time to fix that by updating your Java. We will be using some features of Java 8 in this class. Compile your java program. $ javac TraceryRecursion.java So far, it will compile without errors. Look in the folder, and you will see that you have a new file TraceryRecursion.class. This is the compiled version of your file. You can now run this file by typing: $ java TraceryRecursion You should get the output Running TraceryRecursion.... Try typing javac TraceryRecursion or java TraceryRecursion.java. Both of these should give your errors. Compiling requires the .java extension, and running requires not having it. But it’s easy and common to misremember that, so look at what there errors look like, so that when it happens later, you know what
  • 4.
    caused it. You cancompile and run in separate steps. But sometimes you want to compile and run with one line, and you can combine them like this: $ javac TraceryRecursion.java && java TraceryRecursion . Store arguments Many java programs need outside information provided by the user. This lets users change the behavior of the program (or tell it which data to use) without having to modify the source code. The main method of TraceryRecursion will be the first method called when running the program. This method is static, meaning that the method is associated directly with a class and can thus be called without creating an object that is an instance of the class, and its return type is void, meaning that it won't return anything. It also has parameters, an array of Strings called args (you could call this parameter anything, but the tradition is to call it args or arguments). Our program will take 0-4 arguments: ● the name of the file containing the grammar (e.g.: "grammar.txt") ● the starting symbol to begin the grammar expansion (e.g.: "#story#") ● the number of times we want to perform that expansion (e.g:
  • 5.
    "5") ● a seedvalue for the random number generator (e.g: "123") (Seeded random values allow us to make the random generator predictable) You can see that in main, each variable is given a default value. For example, if no seed value is given in the arguments, the seed value will be the current time (this means you will get different random results each time you run the program, unless a specific seed is specified in the arguments). TODO #1 For each of these parameters, test whether enough arguments have been specified to set that parameter. For example, to set the grammarFile parameter there would have to be at least one argument given. You can test the length of the args array to determine how many arguments have been passed in. Override each of these default parameters with its argument override. For the numbers, you will need to use Integer.parseInt and Long.parseLong to turn the arguments from Strings into numbers. NOTE: Do not add or remove code outside of the START and END comments. Keeping all your code between those two comments will help us grade your work. Load a Tracery file
  • 6.
    Java has manyhelpful built-in data structures: Lists, Arrays, and Hashtables are only a few of them. In this assignment, we will be loading and storing a Tracery grammar as a Hashtable. The Hashtable has a list of keys, and each key has some data associated with it, much like a dictionary stores words with their definition. Hashtables come with some helpful methods included: ● table.get(String s) returns the data for that key, but it will throw an error if that key is not in the table. ● table.put(String s, DATA) creates an entry for that key (or replaces it) and stores the data there. In this part of the assignment, we will implement loadGrammar, which takes a String representing a file path, then loads a file, parses it (breaking it into sections), and stores it in our grammar object. In the loadGrammar method, we have already added the line that creates the Hashtable. You can see that the data type has a weird ending <String, Rule[]>, which tells Java that this Hashtable uses Strings to look up arrays of Rules, so if we try to use numbers as look up values, or store Strings instead of arrays of Rules, Java knows to throw an error.
  • 7.
    We also includeda basic way to load a file. Loading files might cause an IO exception (for example, if the file does not exist), so for complex reasons, we need to have the loading line new String(Files.readAllBytes(Paths.get(path))) wrapped in a try/catch block. We will talk more about throwing and catching exceptions later this quarter. For right now, this loads a whole file as a string. By appending .split('r?n') on the end, we take the resulting string and splits it into an array of lines. TODO #2 For each line in the array of lines, store the data in the grammar hashtable. You can use either a familiar for (int i = 0...) loop, or use the newer for (String line: lines) loop, if you don't need to see the index value. We used the split String method above to split apart the file into lines. Use split again, but splitting on the character ":". Everything before the ":" is the key. Store it in a variable named key. Everything after it is the rule expansion(s). First split apart the rules into an array of Strings. For each line in the file, this will give you a String array of length 2, where the first element of the array is an expandable symbol (which will be a key in the grammar hashtable), and the
  • 8.
    second element ofthe array is the expansion(s) for this symbol. But there is possibly more than one expansion for a symbol. If you look at the grammar files, you can see where there is more than one expansion for a symbol when there's a comma-delimited list of expansions after a symbol. So we have to further split the expansions into an array of Strings, one for each expansion (by splitting on ","). Once you have an array of Strings (of expansions) we have to turn it into an array of Rules. Initialize an array, Rule[], of the right size. Use another loop (inside the loop iterating through lines) to iterate through your array of strings and, for each expansion, create a Rule (it takes a single string as a parameter), and add it to your array of Rules. You now have the right kind of data (Rule[]) to store in the hashtable. Store your data in grammar. What was that method that stores data in a grammar (hint: we describe it above)? In the main() method there's already a call to outputGrammar; this prints your grammar to the console, so you can verify that it loaded it correctly (that the correct expansions are associated with the correct symbols). Implementing Recursion
  • 9.
    Now that youhave successfully loaded a file and stored it as a Hashtable<String, Rule[]> grammar, we can implement the method that generates the generative text! Open up Rule.java, and look at the constructor for this class: Rule(String raw). This constructor takes the string that you passed to it in TODO #2 and splits it into sections (using "#"). The even indexed entries of sections are plain text (ie. the zeroth, second, fourth, etc.), and the odd indexed entries of the variable sections are symbols for expanding (ie. the first, third, fifth, etc.). This might seem a bit confusing at first. In an expansion like "once #character# and #character# went to a #place#" it's clear that splitting on # will cause the zeroth, second and fourth elements (the even elements) to be plain text (i.e. "once", "and" and "went to a") and the first, third and fifth elements (the odd elements) to be symbols (i.e. "#character#", "#character#", "#place#"). But consider the expansion "#name# the #adjective# #animal#". Won't this even and odd approach get reversed, since the expansion starts with a symbol? It turns out it won't because, if there's a symbol at the beginning, or two symbols right next to each other, splitting on "#" produces two strings, the empty string (which is what is before the "#") and then the string with the symbol (which is what is after the "#", minus the closing "#" since we're splitting on "#"). So splitting this expansion on # will produce the following array:
  • 10.
    sections[0]: "" sections[1]: "name" sections[2]:"the" sections[3]: "adjective" sections[4]: "" sections[5]: "animal" So the even and odd relationship still works out, it's just that two of our even entries are the empty string (which is still plain text), which, when we include it in the output of the expansion, won't be visible, and so won't cause us any problems. Phew! Now that we've explained that we can get back to actually writing the code in Rule.java to expand the text. The method expand takes a grammar (the type is Hashtable<String, Rule[]>) as an argument, and returns the expanded String. But right now, it only returns a copy of its original expansion (with any symbols not expanded). We want it to instead recursively expand the odd sections. TODO #3 Create an array of Strings named results that is the same size as the rule sections ( sections.length). This is where we will store the results of expanding each section.
  • 11.
    Create a for-loopthat iterates down the sections array. Since we need to know whether each section is odd or even, you will need to use for (int i = 0 ...), rather than for (String section: sections) style loops. For each section, if it is even ( i%2 == 0), copy the text from the rule section into the results array. If it is odd, then this is a symbol that we need to expand. Use the grammar to find the array of expansions for this symbol. We need to pick a “random” index in the array, but we will use the seeded random number generator (so we can reproduce our results using the same random seed). You can get a random int with random.nextInt(int bound). This will return a random integer between 0 (inclusive) and the bound argument you pass in (exclusive). Since you're randomly picking from an array of possible expansions, what's the largest random value you will want? Pick out an expansion, and save it as the variable Rule selectedExpansion. We want to store that new value in our results array, but since is is an expansion string we need to process it first because it might contain symbols that need to be expanded as well. What method should you call on selectedExpansion to do that? Store the resulting text in the results array. We now have results values for each section in this rule! Use String.join("", results) to join all of the results together into a single string (with no spaces between
  • 12.
    them), and returnthe results of that operation. This new return statement should replace the return statement that was in the starter code. That is, your new return statement should replace the line: return "[" + raw + "]"; Finishing up Run your program. It should now output however many outputs of the text you want (as specified by the count parameter). Try it with and without a seed value. Does it always output something new? Try out a few other grammars, we included grammar-recipe.txt which makes bad recipes, and grammar- emojistory.txt which tells stories with emoji. Now edit grammar-yourgrammar.txt. Create something interesting (of about equal complexity to grammar-story.txt). We won't judge you on what it makes, consider this a freeplay exercise. Some inspirational twitterbots include @unicode_garden, @DUNSONnDRAGGAN, @orcish_insults, @GameIdeaGarden , @indulgine, @CombinationBot, @thinkpiecebot, @infinite_scream, FartingEmoji. Make sure your grammar generates without causing errors! We didn't implement a graceful way to recover from bad formatting or missing symbols, so these grammars are
  • 13.
    brittle, they breakeasily. Turn in Wow, you're done! Congrats on finishing your first 12B lab. Checklist: ● Works for no command‑line arguments, a few, or all of them ● If a random seed is specified, the output stays consistent. If not, it changes ● If you switch the grammar file, you get output from a different grammar ● You have edited grammar-yourgrammar.txt and it runs. To check that your code is right, compare it with the following output. This output assumes that you’re using Random.nextInt() to generate the random numbers (as described above). This is running TraceryRecursion with the following arguments: $ java TraceryRecursion grammar-story.txt "#origin#" 5 10 The output should be: Running TraceryRecursion... with grammar:'grammar-story.txt' startsymbol:'#origin#' count:5 seed:10 Set seed 10 animal:cat,emu,okapi emotion:happy,sad,elated,curious,sleepy
  • 14.
    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# GRAMMAR: adjective: "#color#","#emotion#", place: "school","the beach","the zoo","Burning Man", emotion: "happy","sad","elated","curious","sleepy", origin: "once #character# and #character# went to #place#", color: "red","green","blue", name: "emily","luis","otavio","anna","charlie", character: "#name# the #adjective# #animal#", animal: "cat","emu","okapi", once anna the green emu and anna the sleepy emu went to the zoo once emily the green emu and anna the green cat went to school once charlie the blue okapi and otavio the blue emu went to school once emily the blue emu and otavio the blue cat went to the beach
  • 15.
    once emily thesad okapi and charlie the elated okapi went to the zoo To turn in your code: ● Create a directory with the following name: <student ID>_lab1 where you replace <student ID> with your actual student ID. For example, if your student ID is 1234567, then the directory name is 1234567_lab1. ● Put a copy of your edited TraceryRecursion.java, Rule.java and grammar-yourgrammar.txt files in the directory. ● Compress the folder using zip to create. Zip is a compression utility available on mac, linux and windows that can compress a directory into a single file. This should result in a file named <student ID>_lab1.zip (with <student ID> replaced with your real ID of course). ● Upload the zip file through the page for Lab 1 in canvas ( https://canvas.ucsc.edu/courses/12730/assignments/36933). Additional resources ● How should I name things in Java? https://www.javatpoint.com/java‑naming‑conventions ● Basic Java Syntax
  • 16.
    https://www.tutorialspoint.com/java/java_basic_syntax.htm ● Need somein‑depth Java tutorials? There are free courses online: https://www.codecademy.com/learn/learn‑java , https://www.udemy.com/java‑tutorial/ ● Command line basics: https://hellowebbooks.com/learn‑command‑line/ ● Get Linux/OSX style command line tools on Windows with Cygwin https://cygwin.com/install.html https://canvas.ucsc.edu/courses/12730/assignments/36933 https://www.javatpoint.com/java%E2%80%91naming%E2%80% 91conventions https://www.tutorialspoint.com/java/java_basic_syntax.htm https://www.codecademy.com/learn/learn%E2%80%91java https://www.udemy.com/java%E2%80%91tutorial/ https://hellowebbooks.com/learn%E2%80%91command%E2%80 %91line/ https://cygwin.com/install.html Utilizing the readings from the text and your research, provide an in-depth analysis of 3 social media platforms and how businesses are utilizing them to reach new and existing customers. Specific groups to be addressed are Generation X, Generation Y, Millennials, Baby Boomers, and 60+ market segments. An example of a discussion idea is to research the trend toward mobility in social media and write a paper on how the different markets described in your Week 1 Individual Project use technology. Note: Research is to be academic or professional in scope. Use of blogs, personal Web sites, corporate Web sites, wikis, or
  • 17.
    other social-media-related sourcesare not acceptable. A minimum of 4 pages is required. Title and abstract pages do not count toward the page minimum. A minimum of 5 professional or academic references is required to support your paper and to provide additional discussion points. 17 VIDEO AND PODCASTING MADE EASY Creating audio and video content for marketing and PR purposes requires the same attention to appropriate topics as other techniques outlined in this book. It requires targeting individual buyer personas with thoughtful information that addresses some aspect of their lives or a problem they face. By doing so, you brand your organization as smart and worthy of doing business with. However, unlike text-based content such as blogs or news releases, audio and video might require a modest investment in additional hardware such as microphones and video cameras, as well as software, and, depending on the level of quality you want to achieve, may also necessitate time- consuming editing of the files. Although the actual procedures for podcasting and video are a bit more convoluted than, say, starting a blog, they are still not all that difficult. Video and Your Buyers Organizations that deliver products or services that naturally lend themselves to video have been among the first to actively use the medium to market and deliver information about their offerings. For example, many churches routinely shoot video of weekly services and offer it online for anyone to watch, drawing more people into the congregation. Many amateur and professional sports teams, musicians, and theater groups also use video as a marketing and PR tool. Video follows both blogs and podcasting on the adoption curve at organizations that don't have a service that naturally lends itself to video. 303Companies are certainly experimenting, typically by embedding video (hosted at YouTube or another video site) into their existing blogs and online media rooms. I'm also seeing video snippets of CEO speeches, customer
  • 18.
    interviews, and quickproduct demonstrations. Business-Casual Video In the United States, there has been a 20-year trend toward so- called business-casual clothing in the workplace. My first job, on Wall Street in the 1980s, required me to wear a suit and tie wi 18 HOW TO USE NEWS RELEASES TO REACH BUYERS DIRECTLY Guess what? Press releases have never been exclusively for the press. My first job in the mid-1980s was on a Wall Street trading desk. Every day, I would come to work and watch the Dow Jones Telerate and Reuters screens as they displayed specialized financial data, economic information, and stock prices. The screens also displayed news feeds, and within these news feeds were press releases. For decades, financial market professionals have had access to company press releases distributed through Business Wire, PR Newswire, and other electronic press release distribution services. And they weren't just for publicly traded corporations; any company's release would appear in trading rooms within seconds. I distinctly remember traders intently watching the newswires for any signs of market-moving events. Often the headline of a press release would cause frenzy: “Did you see? IBM is acquiring a software company!” “It's on the wire; Boeing just got a 20-plane order from Singapore Airlines!” For years, markets often moved and stock prices rose and fell based on the press release content issued directly by companies, noton the news stories written minutes or hours later by reporters from newswire outlets like Reuters and Dow Jones (and later Bloomberg). Press releases have also been available to professionals working within corporations, government agencies, and law firms, all of which have had access to press releases through services like those from NewsEdge, Dow Jones, and LexisNexis. These
  • 19.
    services have beendelivering press releases to all kinds of professionals for competitive intelligence, research, discovery, and other purposes for decades. 316 And since about 1995, the wide availability of the web has meant that press releases have been available for free to anyone with an Internet connection and a web browser. Millions of people read press releases directly, unfiltered by the media. You need to be speaking directly. YOUR NEWSROOM: A FRONT DOOR FOR MUCH MORE THAN THE MEDIA The online newsroom (sometimes called a press room, media room, or press page) is the part of your organization's website that you create specifically for the media. In some organizations, this section is simply a list of news releases with contact information for the organization's PR person. But many companies and nonprofits have elaborate newsrooms with a great deal of information available in many different formats: audio, video, photos, news releases, background information, financial data, and much more. A close cousin to the newsroom is the online investor relations (IR) room that many public companies maintain; however, I don't cover IR sites in this book. Before I give you ideas on how to create a valuable newsroom of your own, I want you to consider something that is vitally important: All kinds of people visit your newsroom, not just journalists. Stop and really let that soak in for a moment. Your buyers are snooping around your organization by visiting the media pages on your website. Your current customers, partners, investors, suppliers, and employees all visit those pages. Why is that? Based on casual research I've done (I often speak about visitor statistics with employees who are responsible for their organizations' newsrooms), I'm convinced that when people want to know what's currentabout an organization, they go to a newsroom. Your newsroom is for your buyers, not just the media.
  • 20.
    329Visitors expect thatthe main pages of a website are basically static (i.e., they are not updated often), but they also expect that the news releases and media-targeted pages on a site will reveal the very latest about a company. For many companies, the news release section is one of the most frequently visited parts of the website. Check out your own website statistics; you may be amazed at how many visitors are already reading your news releases and other media pages online. So I want you to do something that many traditional PR people think is nuts. I want you to design your newsroom for your buyers. By building a media room that targets buyers, you will not only enhance those pages as a powerful marketing tool but also make a better media site for journalists. I've reviewed hundreds of newsrooms, and the best ones are built with buyers in mind. This approach may sound a bit radical, but believe me, it works. When news releases are posted on your site, search engine crawlers will find the content, index it, and rank it based on words, phrases, and other factors. Because news release pages update more often than any other part of a typical organization's website, search engine algorithms (tuned to pay attention to pages that update frequently) tend to rank news release pages among the highest on your site, driving traffic there first. “There's no question that a well-organized media room often has higher search results and drives more traffic because of the way the search engines work,” says Dee Rambeau, vice president of customer engagement at PR Newswire. “A news release dynamically builds out a new set of content in your newsroom, with each news release generating its own indexable page, which the search engines all capture. Google and the other search engines love fresh content that relates back to similar content on the other pages of the site. Aggressive companies take advantage of this by sending news releases frequently to get high rankings from the search engines. Frequency has a
  • 21.
    great deal todo with search engine rankings—if you do 10 news releases, that's great; 20 is better, and 100 is better still.” The Kellogg Company—the world's leading producer of cereal and second-largest producer of cookies, crackers, and savory snacks—uses its newsroom1for search engine optimization (SEO) purposes and as a tool to reach various audiences, including reporters and editors who cover the company. “What we found through our research is that, more and more, our newsroom is extending beyond just media to other stakeholders,” says Stephanie Slingerland, senior manager of corporate communications at the Kellogg Company. “Anyone coming to the site, be it an investor, an NGO or other partner agency, or even a consumer—the newsroom is inviting for them to get the information which may be relevant.” Slingerland has strong partnerships with people who work in other departments at the company and who also provide information to the public. “Our investor relations team are having conversations and engagements with analysts and investors. So, from our partnership with that team, we know what those stakeholders might be looking for. Or, for example, our government relations team are regularly engaging with government and nongovernmental officials. Again, we have a strong partnership with them. We know what they're looking for and can make sure that they have what they might be looking for on our site. The same with our corporate social responsibility team, who engage with agencies and others as part of our philanthropic activities.” Based on what she learns about the needs of the news media and other stakeholders, Slingerland creates the right content, including news releases, fact sheets, news alerts, and more. “Since we are the folks that regularly engage with the media, we know what we're getting asked for over and over, like the fact sheet section,” she says. “And we also know that many people want to know the latest news about the company, but they don't necessarily come to our newsroom every day. So
  • 22.
    that's why wecreated our news alerts section, so they opt in to be alerted whenever One important consideration that many marketing and PR people overlook when considering the benefits of a newsroom is that you control the content, not your IT department, webmaster, or anyone else. Youshould design your newsroom as a tool to reach buyers and journalists, and you don't need to take into consideration the rules for posting content that the rest of the organization's site may require. If you build this part of your site using a specialized newsroom content-management application, such as the MediaRoom2product from PR Newswire, you will control a corner of your organization's website that you can update whenever you like using simple tools, and you won't need to request help from anyone in other departments or locations. So start with your needs and the needs of your buyers and journalists, not the needs of those who own the other parts of your organization's website. Start with a Needs Analysis When designing a new newsroom (or planning an extensive redesign), start with a needs analysis. Before you just jump right into site aesthetics and the organization of news releases, take time to analyze how the site fits into your 332larger marketing, PR, and media relations strategy. Consider the buyer persona profiles you built as part of your marketing and PR plan. Talk with friendly journalists so you can understand what they need. Who are the potential users of the newsroom, and what content will be valuable to them? When you have collected some information, build buyers' and journalists' needs into your newsroom. As you work toward starting your design, try to think a bit more like a publisher and less like a marketing and PR person. A publisher carefully identifies and defines target audiences and then develops the content required to meet the needs of each distinct demographic. Graphical elements, colors, fonts, and other visual manifestations of the site are also important but
  • 23.
    should take abackseat during the content needs analysis process.