{"id":55,"date":"2010-01-26T08:56:05","date_gmt":"2010-01-26T16:56:05","guid":{"rendered":"http:\/\/patternweaver.com\/blog\/?p=55"},"modified":"2011-12-19T19:26:23","modified_gmt":"2011-12-19T19:26:23","slug":"generating-simple-grammars-in-php-about-soup","status":"publish","type":"post","link":"http:\/\/patternweaver.com\/blog\/2010\/01\/generating-simple-grammars-in-php-about-soup\/","title":{"rendered":"Generating Simple Grammars in PHP about Soup"},"content":{"rendered":"<p>So, I&#8217;ve worked in a number of loose environments, and there tends to be some concept of recurring topics wherever I go&#8230; even more, there&#8217;s always that guy who sees that flicker of interest die in your eyes but still bravely forges ahead with the conversation. A coworker had probably the most effective blockade for this, he&#8217;d start on what I called the &#8220;soup talker&#8221; story. It started with him explaining about his overeager coworker from a long past job and how much that guy loved soup. Just couldn&#8217;t get over it. Wouldn&#8217;t quit talking about it. But as the story progressed, you got the uneasy feeling that you had become the soup talker, and indeed, he would used that story, like Pavlov&#8217;s bell to cause the other party to end their long winded diatribe. So I took it upon myself to write this script, so the world never had a shortage of talk about various soups, in honor of windbag coworkers.<\/p>\n<script src=\"https:\/\/gist.github.com\/1498504.js?file=soup_talker.php\"><\/script><noscript><pre><code class=\"language-php php\">&lt;?php \n$soups = array(&quot;Avgolemono &quot;, &quot;Borscht &quot;, &quot;Caldo verde &quot;, &quot;Fasolada &quot;,\n&quot;Goulash soup&quot;, &quot;Islensk Kjotsupa &quot;, &quot;Kimchi jjigae &quot;, &quot;Lentil soup&quot;,\n&quot;Menudo&quot;, &quot;Mulligan Stew&quot;, &quot;Philadelphia Pepper Pot &quot;, &quot;Pho&quot;,\n &quot;Sundubu jjigae &quot;, &quot;Snert &quot;, &quot;Solyanka &quot;, &quot;Tomato soup&quot;, &quot;Cold borscht&quot;,\n&quot;Dashi soup &quot;, &quot;Okroshka&quot;, &quot;Sour cherry soup&quot;, &quot;Vichyssois&quot;, &quot;Ginataan&quot;,\n&quot;Naengmyeon &quot;, &quot;Fruktsuppe&quot;, &quot;Cioppino &quot;, &quot;Fanesca &quot;, &quot;Lan Sikik &quot;,\n&quot;Sliced fish soup&quot;, &quot;Ukha or Lohikeitto&quot;, &quot;Bird's nest soup&quot;, &quot;Fuhn&quot;,\n&quot;Mian&quot;, &quot;Ramen&quot;, &quot;Saimin&quot;, &quot;French onion soup&quot;, &quot;Tinola&quot;, &quot;Crab bisque&quot;,\n&quot;Chestnut bisque&quot;, &quot;Cream of Crab Soup&quot;, &quot;Clam chowder&quot;, &quot;Chupe&quot;,\n&quot;Maryland Crab Soup &quot;, &quot;Brown Veal&quot;, &quot;Fumet&quot;, &quot;White Veal&quot;,\n&quot;Cream of mushroom soup&quot;, &quot;Cream of celery soup&quot;, &quot;Cream of potato soup&quot;,\n &quot;Wine soup&quot;, &quot;Chowder&quot;, &quot;Asparagus Soup with Hazelnut Butter&quot;,\n&quot;Avgolemono&quot;, &quot;Beef Barley Soup&quot;, &quot;Beer Cheese Soup&quot;,\n&quot;Broccoli Cheddar Soup&quot;, &quot;Brown Veal Stock&quot;, &quot;Carrot Vichyssoise&quot;,\n&quot;Chervil Butter&quot;, &quot;Chicken Noodle Soup&quot;, &quot;Chicken Stock&quot;,\n&quot;Chicken and Andouille Gumbo&quot;, &quot;Chilled Avocado Soup&quot;,\n&quot;Chilled Beet Borscht&quot;, &quot;Chilled Brie Soup&quot;,\n&quot;Chilled Cucumber Buttermilk Soup&quot;, &quot;Chilled Red Pepper Soup&quot;,\n&quot;Chilled Yellow Dal Soup&quot;, &quot;Chipotle Cream&quot;, &quot;Cilantro Cream&quot;,\n&quot;Cinnamon Chantilly&quot;, &quot;Cream of Broccoli Soup&quot;,\n&quot;Cream of Mushroom Soup&quot;, &quot;Cream of Tomato Soup&quot;, &quot;Croutes&quot;,\n&quot;Curry Chicken and Pineapple Soup&quot;, &quot;Danish Split Pea Soup&quot;,\n&quot;Dill Wafers&quot;, &quot;Duck Consomme&quot;, &quot;Duck Stock&quot;, &quot;Fish Chowder&quot;,\n&quot;Fish Stock #3&quot;, &quot;French Onion Soup&quot;, &quot;Frozen Coconut Yogurt&quot;,\n&quot;Gazpacho - Traditional Style&quot;, &quot;Gazpacho - Yucatan Style&quot;,\n&quot;Gorgonzola Soup with Bacon&quot;, &quot;Gougere&quot;, &quot;Hazelnut Butter&quot;,\n&quot;Hot Borscht&quot;, &quot;Hot and Sour Duck Consomme&quot;, &quot;Jamaican Calalou Soup&quot;,\n&quot;Lamb and Lentil Soup&quot;, &quot;Louisiana Oyster Stew&quot;, &quot;Manhattan Clam Chowder&quot;,\n&quot;Minestrone&quot;, &quot;Navy Bean Soup&quot;, &quot;New England Clam Chowder&quot;,\n&quot;Parmesan Twists&quot;, &quot;Pasta e Fagioli&quot;, &quot;Peanut Soup&quot;, &quot;Roasted Corn Bisque&quot;,\n&quot;Rouille&quot;, &quot;Scallop and Corn Chowder with Dill&quot;,\n&quot;Shrimp Bisque with Dill Wafers&quot;, &quot;Shrimp Stock&quot;, &quot;Shrimp and Oyster Gumbo&quot;,\n&quot;Smoked Salmon Chowder&quot;, &quot;Sopa de Tortilla&quot;, &quot;Soupe de Poisson&quot;,\n&quot;Spanish Almond Soup&quot;, &quot;Sparkling Peach Soup&quot;, &quot;St. Louis Chili&quot;,\n&quot;Steak Soup&quot;, &quot;Summer Fruit Gazpacho with Coconut Yogurt&quot;,\n&quot;Swedish Potato Soup&quot;, &quot;Tomato Basil Soup&quot;, &quot;Tomato Lime Bisque&quot;,\n&quot;Tortellini in Wild Mushroom Broth&quot;, &quot;Vegetarian Black Bean Soup&quot;,\n&quot;Vichyssoise&quot;, &quot;Vietnamese Hot and Sour Shrimp Soup&quot;,\n&quot;Wild Mushroom Soup with Chervil Butter&quot;, &quot;Wild Rice Soup&quot;,\n&quot;Winter Squash Soup with Toasted Pecans&quot;, &quot;Won Ton Soup&quot;\n);\n\n    $starting_sentences = array(\n        &quot;You know, I really love soup:&quot;,\n        &quot;Soups are really an excellent food...&quot;,\n        &quot;I love hot soup on a cold day!&quot;,\n        &quot;Soup is *so* delicious!&quot;\n    );\n\n    $first_soup = array(\n        &quot;There's [soup]&quot;,\n        &quot;I like [soup]&quot;,\n        &quot;I've tried [soup]&quot;,\n        &quot;I once ate [soup]&quot;,\n        &quot;I tend to enjoy [soup]&quot;\n    );\n\n    $middle_sentences = array(\n        &quot;[soup] is really good&quot;,\n        &quot;[soup] has a great texture&quot;,\n        &quot;[soup] is so tangy&quot;,\n        &quot;[soup] really pleases the palate&quot;,\n        &quot;[soup] is a traditional dish&quot;,\n        &quot;[soup] is packed with flavor&quot;,\n        &quot;constrasting strongly against [soup]&quot;\n    );\n\n    $conjunctions = array(\n        &quot;and&quot;,\n        &quot;but&quot;,\n        &quot;, although&quot;,\n        &quot;, however&quot;,\n        &quot;even though&quot;,\n        &quot;oddly enough, though&quot;,\n        &quot;...&quot;,\n        &quot;,&quot;\n    );\n\n    function substitute($template, $values, $cleanUnmatched=false){\n        if(file_exists($template)){\n            $template = file_get_contents($template);\n        }\n        foreach($values as $key=&gt;$value){\n            $template = preg_replace('~\\['.$key.'\\]~', '{'.$key.'}', $template);\n        }\n        foreach($values as $key=&gt;$value){\n            $template = preg_replace('~{'.$key.'}~', $value, $template);\n        }\n        if($cleanUnmatched) $template = preg_replace('~\\[.*?\\]~', '', $template);\n        return $template;\n    }\n\n    function get_random($array, $soup='Chicken Noodle'){\n        $value = $array[rand(0, count($array)-1)];\n        return substitute($value, array('soup'=&gt;strtolower($soup)));\n    }\n\n    if(!isset($_REQUEST['count'])){ \/\/dump the containing page\n        ?&gt;&lt;html&gt;\n    &lt;head&gt;\n        &lt;script&gt;\n            \n            \n            function SoupTalker(){\n                this.count = 0;\n            }\n            \/\/ FUNCTION DEFINITIONS\n            SoupTalker.prototype.getXMLHTTPRequest = function(){\n                var xRequest=null;\n                if (window.XMLHttpRequest) xRequest = new XMLHttpRequest();\n                if (typeof ActiveXObject != &quot;undefined&quot;){\n                    xRequest = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);\n                }\n                object = this;\n                xRequest.onreadystatechange = function(){\n                    if (this.readyState == 4){\n                        var node = document.getElementById('text');\n                        node.innerHTML = node.innerHTML + this.responseText;\n                        object.count++;\n                        \/\/object.talk_about_soup();\n                        setTimeout(&quot;object.talkAboutSoup()&quot;,1250);\n                    }\n                };\n                return xRequest;\n            }\n            SoupTalker.prototype.talkAboutSoup = function(){\n                if (\n                    typeof this.request == &quot;undefined&quot;||\n                    typeof ActiveXObject != &quot;undefined&quot;\n                )\n                    this.request = this.getXMLHTTPRequest(); \/\/IE bug\n                if(this.request){\n                    this.request.open('get',&quot;soup.php?count=&quot;+this.count,true);\n                    this.request.setRequestHeader(\n                        &quot;Content-Type&quot;, &quot;application\/x-www-form-urlencoded&quot;\n                    );\n                    this.request.send(&quot;?count=&quot;+this.count,true);\n                }\n            }\n\n            var soupTalker = new SoupTalker();\n        &lt;\/script&gt;\n    &lt;\/head&gt;\n    &lt;body onload=&quot;soupTalker.talkAboutSoup();&quot;&gt;\n        &lt;div id=&quot;text&quot;&gt;&lt;\/div&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;&lt;?\n    }else{\n        $soup = $soups[rand(0, count($soups)-1)];\n        if($_REQUEST['count'] == 0){ \/\/dump the first sentence\n            echo(get_random($starting_sentences));\n        }\n        if($_REQUEST['count'] &gt; 0){ \/\/ dump other sentences\n            $count = $_REQUEST['count'];\n            $last_pass_ended = ( ($count-1)% 3 == 0) &amp;&amp; ( ($count-1) % 2 == 0);\n            $this_pass_ends = ($count % 3 == 0) &amp;&amp; ($count % 2 == 0);\n            if($this_pass_ends){\n                echo(\n                    ' '.get_random($conjunctions).' '.\n                    get_random($middle_sentences, $soup).'.'\n                );\n            }else{\n                if($last_pass_ended){\n                    echo(' '.get_random($first_soup, $soup));\n                }else{\n                    echo(\n                        ' '.get_random($conjunctions).' '.\n                        get_random($middle_sentences, $soup)\n                    );\n                }\n            }\n        }\n    }<\/code><\/pre><\/noscript>\n<p>Don&#8217;t <b>you<\/b> like soup, too? Wouldn&#8217;t you like to <a href=\"http:\/\/patternweaver.com\/soup.php\">hear all about it<\/a>?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I&#8217;ve worked in a number of loose environments, and there tends to be some concept of recurring topics wherever I go&#8230; even more, there&#8217;s always that guy who sees that flicker of interest die in your eyes but still bravely forges ahead with the conversation. A coworker had probably the most effective blockade for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":0,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}