{"id":1784,"date":"2012-10-17T21:52:41","date_gmt":"2012-10-17T19:52:41","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=1784"},"modified":"2012-10-17T21:55:23","modified_gmt":"2012-10-17T19:55:23","slug":"generate-random-pronouceable-words","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2012\/10\/generate-random-pronouceable-words\/","title":{"rendered":"Generate Random Pronouceable Words"},"content":{"rendered":"<p>Ho ha\u00ef, blog, long time no see! :)<\/p>\n<p>I was checking stuff on various URL shorteners and noticed <a href=\"http:\/\/is.gd\/\">is.gd<\/a> has one interesting feature: you can generate short URLs that are &quot;pronounceable&quot; (no &quot;vgfhgt&quot;). This is a great little touch: a random but pronounceable word will be more memorable and the probability for typos is reduced, which makes it a killer feature for random generated passwords for example.<\/p>\n<p>Generating pronounceable random words isn&#39;t difficult :<\/p>\n<ul>\n<li>start by a vowel or a consonant<\/li>\n<li>alternate and add letters till proper word length<\/li>\n<\/ul>\n<p>Simple, but this generates words that are <em>too simple<\/em> maybe: &#39;abuco&#39;, &#39;misolo&#39;, &#39;xulanipo&#39;, etc&#8230; Natural words also use a few consecutive consonant combinations as well as vowel combos (&#39;cheepo&#39;, &#39;bergam&#39;, &#8230;)<\/p>\n<p>I ended up with this simple piece of code that gives more natural words:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\n\/**\r\n * Generate random pronounceable words\r\n *\r\n * @param int $length Word length\r\n * @return string Random word\r\n *\/\r\nfunction random_pronounceable_word( $length = 6 ) {\r\n\t\r\n\t\/\/ consonant sounds\r\n\t$cons = array(\r\n\t\t\/\/ single consonants. Beware of Q, it&#039;s often awkward in words\r\n\t\t&#039;b&#039;, &#039;c&#039;, &#039;d&#039;, &#039;f&#039;, &#039;g&#039;, &#039;h&#039;, &#039;j&#039;, &#039;k&#039;, &#039;l&#039;, &#039;m&#039;,\r\n\t\t&#039;n&#039;, &#039;p&#039;, &#039;r&#039;, &#039;s&#039;, &#039;t&#039;, &#039;v&#039;, &#039;w&#039;, &#039;x&#039;, &#039;z&#039;,\r\n\t\t\/\/ possible combinations excluding those which cannot start a word\r\n\t\t&#039;pt&#039;, &#039;gl&#039;, &#039;gr&#039;, &#039;ch&#039;, &#039;ph&#039;, &#039;ps&#039;, &#039;sh&#039;, &#039;st&#039;, &#039;th&#039;, &#039;wh&#039;, \r\n\t);\r\n\t\r\n\t\/\/ consonant combinations that cannot start a word\r\n\t$cons_cant_start = array( \r\n\t\t&#039;ck&#039;, &#039;cm&#039;,\r\n\t\t&#039;dr&#039;, &#039;ds&#039;,\r\n\t\t&#039;ft&#039;,\r\n\t\t&#039;gh&#039;, &#039;gn&#039;,\r\n\t\t&#039;kr&#039;, &#039;ks&#039;,\r\n\t\t&#039;ls&#039;, &#039;lt&#039;, &#039;lr&#039;,\r\n\t\t&#039;mp&#039;, &#039;mt&#039;, &#039;ms&#039;,\r\n\t\t&#039;ng&#039;, &#039;ns&#039;,\r\n\t\t&#039;rd&#039;, &#039;rg&#039;, &#039;rs&#039;, &#039;rt&#039;,\r\n\t\t&#039;ss&#039;,\r\n\t\t&#039;ts&#039;, &#039;tch&#039;, \r\n\t);\r\n\t\r\n\t\/\/ wovels\r\n\t$vows = array(\r\n\t\t\/\/ single vowels\r\n\t\t&#039;a&#039;, &#039;e&#039;, &#039;i&#039;, &#039;o&#039;, &#039;u&#039;, &#039;y&#039;, \r\n\t\t\/\/ vowel combinations your language allows\r\n\t\t&#039;ee&#039;, &#039;oa&#039;, &#039;oo&#039;, \r\n\t);\r\n\t\r\n\t\/\/ start by vowel or consonant ?\r\n\t$current = ( mt_rand( 0, 1 ) == &#039;0&#039; ? &#039;cons&#039; : &#039;vows&#039; );\r\n\t\r\n\t$word = &#039;&#039;;\r\n\t\t\r\n\twhile( strlen( $word ) &lt; $length ) {\r\n\t\r\n\t\t\/\/ After first letter, use all consonant combos\r\n\t\tif( strlen( $word ) == 2 ) \r\n\t\t\t$cons = array_merge( $cons, $cons_cant_start );\r\n\r\n\t\t \/\/ random sign from either $cons or $vows\r\n\t\t$rnd = ${$current}[ mt_rand( 0, count( ${$current} ) -1 ) ];\r\n\t\t\r\n\t\t\/\/ check if random sign fits in word length\r\n\t\tif( strlen( $word . $rnd ) &lt;= $length ) {\r\n\t\t\t$word .= $rnd;\r\n\t\t\t\/\/ alternate sounds\r\n\t\t\t$current = ( $current == &#039;cons&#039; ? &#039;vows&#039; : &#039;cons&#039; );\r\n\t\t}\r\n\t}\r\n\t\r\n\treturn $word;\r\n}\r\n\r\n?&gt;<\/code><\/pre><\/div>\n<p>(bleh, just noticed the <del datetime=\"2012-10-17T19:52:58+00:00\">fucking<\/del>fancy curled quoted are back in code blocks. <a href=\"http:\/\/pastebin.com\/hvi658Qp\">pastebin<\/a> for cut&#39;n&#39;paste code)<\/p>\n<p>Play with the demo: <a href=\"http:\/\/planetozh.com\/projects\/random-pronounceable-words\/\">random pronounceable words<\/a>.<\/p>\n<p>Nothing elaborated enough to help you create an alien language for your next sci-fi movie, but I&#39;m rather pleased with the random words it creates. It&#39;s also easy to implement this in another language: modify the group of consecutive vowels and consonants to match what exists in your language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ho ha\u00ef, blog, long time no see! :) I was checking stuff on various URL shorteners and noticed is.gd has one interesting feature: you can generate short URLs that are &quot;pronounceable&quot; (no &quot;vgfhgt&quot;). This is a great little touch: a random but pronounceable word will be more memorable and the probability for typos is reduced, which makes it a killer\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[2,393,10,392],"class_list":["post-1784","post","type-post","status-publish","format-standard","hentry","category-published","tag-code","tag-password","tag-php","tag-random"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=1784"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1784\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=1784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=1784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=1784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}