Levidromes - Our Secret Sauce

Posted on
Levidrome List Secret Sauce

When we first started this website, we wanted to publish all known levidromes. We started with English, then expanded to other languages. We have achieved that goal for all the dictionary lists we have found.

We probably took the fun out of trying to find new levidromes. All you have to do is visit our list pages and there they are. But how did we find them?

A bit of a hint. When we created our FAQ page, we actually went through the process of how our levidrome finding script worked. But today, we are going to reveal the actual script. Our secret sauce in our finding levidromes recipe.

The script was developed in PHP, a programming language which is used highly in web environments. Wordpress, one of the the most popular Web development tools used today, is written in PHP.

Although highly used on the web, PHP is still a programming language which can take data as input and spit out results as output. I am very familiar with the language and it has a bunch of built in functions which handle arrays (or lists) beautifully.

To quote what we said on our FAQ page:

We extracted the words programmatically. We developed an app or script (in PHP) which does the necessary steps to create the lists. Essentially, we take the list of words, convert to UTF-8 (to standardize so non-english words will show properly on our website), change uppercase to lowercase, remove duplicates, then reverse each word and add it to the existing list. Any duplicates at that point will either be levidromes or palindromes. We remove the palindromes and are left with levidromes.

That was basically the procedure or method to our goal. The only pre-work which had to be done was to ensure the list of words was in UTF-8 format (which is a format which can display foreign characters). Our DICT words lists are already in this format.

Here is the script (using tagalog as an example):

<?php
ini_set("memory_limit","-1");
$langfile="tagalog";
function array_not_unique( $a = array() )
{
 return array_diff_key( $a , array_unique( $a ) );
}
function mb_strrev($string, $encoding = null) {
 if ($encoding === null) {
  $encoding = mb_detect_encoding($string);
}

$length = mb_strlen($string, $encoding);
$reversed = '';
while ($length-- > 0) {
  $reversed .= mb_substr($string, $length, 1, $encoding);
}

return $reversed;
}
$f1=fopen($langfile."-levidrome.txt",'a');
$f2=fopen($langfile."-palindrome.txt",'a');
$words=file($langfile,FILE_IGNORE_NEW_LINES);
$rev=array();
for($i=0;$i<=sizeof($words)-1;$i++){
  $words[$i]=mb_strtolower($words[$i],"UTF-8");
  $rev[$i]=mb_strrev($words[$i],"UTF-8");
}
$words=array_unique($words);
$rev=array_unique($rev);
$all=array_merge($words,$rev);
sort($all);
$uniq=array_not_unique($all);
foreach($uniq as $word){
  if(mb_strrev($word,"UTF-8")<>$word){
    fwrite($f1,$word."\n");
  }else{
    fwrite($f2,$word."\n");
}
}
fclose($f1);
fclose($f2);
?>

So there you have it. Once the script is run, you will have 2 files, one with levidrome words and the other with palindrome words. The secret sauce or coding which we used to created our lists.

Aside note: Most instances of PHP by default do not have the MBSTRING functions enabled. You may need to add these features to your PHP install by either downloading the extensions, or enabled them in the php.ini file by uncommenting the extension=php_mbstring.dll line.

I apologize in advance. This was probably the most geeky post I have ever written for this website. However, that is me...geeky...but also a levidromist geek.