Sunday, January 10, 2010

Anagram (Word Finder) Mobile

image This is a ported version of my Anagram Desktop version found here.

Using Regular Expression, RE, or RegEx. Though not purely RegEx way .. it has some help of Hashtable and StringBuilder for counting characters (which is MY WAY of counting characters) and appending the words found.

Anyway, at the left .. is the screenshot of the application (click to view larger version). If you noticed, I used “regular expression” as my word..

You can use this app on your scrabble game!

look at the total result. 
1,905 words found in 1 minute and 16 seconds and I have total of 192,719 English words in textfile. Am not sure if that’s fast enough .. am using Xperia X1 device. Here’s the specs of this device. Desktop version is much faster. Look here

Download it here
arrow AnagramMobile.rar
18.3 KB

192,719 English Words
english.txt
2.25 MB

8,143 Tagalog Words
tagalog.txt
69 KB

How To Use
1. After downloading AnagramMobile.rar and extracting the .exe file in your device. Download english.txt or tagalog.txt.

2. Run the application and tap Open and select english.txt or tagalog.txt file in Open Window.

3. Wait for a few seconds or a minute to load the word list.

4. Enter word, a sentence, or a jumbled letters in “Enter word or sentence here” box

5. and click Find.

6. and wait for the results.

XDA Dev Thread Here
Link 1

Code Snippet for Finder Class 3.
Fast string concatenation using StringBuilder.
Duplicates problem solved using Hashtable.

Methods
OpenWordDatabaseTextFile(string filename)
Find(string word, string separator, int min)
CountCharacters(string str)
DoRegEx(string pattern, bool UseAllRegexOptionFlag)
string[] Split(string s, string delimeter)

Properties
string TimeDuration
int TotalFound
int TotalWordsFoundInDatabase

// Finder Class version 3
// Author: Jayson Ragasa, December 22, 2009
// Copyright© 2009 Jayson Ragasa, Baguio City, Philippines

namespace Anagram
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

public class Finder
{
string _time_duration = string.Empty;
int _ttl_found = 0;
int _ttl_words_in_database = 0;
string new_line_separated_words = string.Empty;

public Finder() { }

public void OpenWordDatabaseTextFile(string filename)
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(filename))
{
new_line_separated_words = reader.ReadToEnd();
}

//_ttl_words_in_database = new_line_separated_words.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Length;
_ttl_words_in_database = Split(new_line_separated_words, "\r\n").Length;
}

public string Find(string word, string separator, int min)
{
string ret = string.Empty;

StringBuilder list = new StringBuilder();

string regex = "^[" + word + " ]+\\r\\n";

DateTime startTime = DateTime.Now;

Hashtable words = new Hashtable();
IDictionaryEnumerator ide;

#region start
{
Regex r = DoRegEx(regex, false);

if (r.IsMatch(new_line_separated_words))
{
Hashtable ht_x;
Hashtable ht_y;

bool notValid = false;

MatchCollection mc = r.Matches(new_line_separated_words);

foreach (Match m in mc)
{
string w = m.Value.Trim();

if (w.Length >= min)
{
ht_x = CountCharacters(m.Value.Trim());
ht_y = CountCharacters(word.Trim());

notValid = false;

ide = ht_x.GetEnumerator();
while (ide.MoveNext())
{
if (Convert.ToInt32(ht_x[ide.Key]) > Convert.ToInt32(ht_y[ide.Key]))
{
notValid = true;
}
}

if (notValid) { continue; }

string thisWord = m.Value.Replace("\r\n", string.Empty);
thisWord = thisWord.Trim();

// don't add the word if the word already exists
// in hashtable
if (!words.ContainsKey(thisWord))
{
words.Add(thisWord, thisWord);
}
}
}

ht_x = null; ht_y = null;
}
}
#endregion

#region build result
{
ide = words.GetEnumerator();
while (ide.MoveNext())
{
list.AppendFormat("{0}" + separator, ide.Value);
}
}
#endregion

DateTime endTime = DateTime.Now;

TimeSpan _duration = endTime - startTime;
ret = list.ToString();
if (ret != string.Empty) ret = ret.Substring(0, ret.Length - separator.Length);
this._time_duration = _duration.ToString();
this._ttl_found = words.Count;

return ret;
}

// Count instance of a character in a string
protected Hashtable CountCharacters(string str)
{
Hashtable ht = new Hashtable();

char[] a_chars = str.ToCharArray();

foreach (char c in a_chars)
{
if (ht.ContainsKey(c))
{
ht[c] = (Convert.ToInt32(ht[c]) + 1).ToString();
}
else
{
ht.Add(c, 1);
}
}

return ht;
}

protected Regex DoRegEx(string pattern, bool UseAllRegexOptionFlag)
{
string regex = pattern;

RegexOptions options;

if (UseAllRegexOptionFlag)
{
options = (((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.Singleline) | System.Text.RegularExpressions.RegexOptions.Multiline) | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
else
{
options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
}

return new Regex(regex, options);
}

protected string[] Split(string s, string delimeter)
{
if (s == null)
throw new ArgumentNullException("stringToBeSplitted is null.");
if (delimeter == null)
throw new ArgumentNullException("delimeter is null.");

int dsum = 0;
int ssum = 0;
int dl = delimeter.Length;
int sl = s.Length;

if (dl == 0 || sl == 0 || sl < dl)
return new string[] { s };

char[] cd = delimeter.ToCharArray();
char[] cs = s.ToCharArray();
List<string> retlist = new List<string>();

for (int i = 0; i < dl; i++)
{
dsum += cd[i];
ssum += cs[i];
}

int start = 0;
for (int i = start; i < sl - dl; i++)
{
if (i >= start && dsum == ssum && s.Substring(i, dl) == delimeter)
{
retlist.Add(s.Substring(start, i - start));
start = i + dl;
}

ssum += cs[i + dl] - cs[i];
}

if (dsum == ssum && s.Substring(sl - dl, dl) == delimeter)
{
retlist.Add(s.Substring(start, sl - dl - start));
retlist.Add("");
}
else
{
retlist.Add(s.Substring(start, sl - start));
}

return retlist.ToArray();
}

public string TimeDuration
{
get { return _time_duration; }
}

public int TotalFound
{
get { return _ttl_found; }
}

public int TotalWordsFoundInDatabase
{
get { return this._ttl_words_in_database; }
}
}
}


5 comments:

  1. this is cool, but it's not working on my Touch Pro2. I click Find and nothing happens. Still says 0 words found in 0 in 00:00:00. Any ideas?

    ReplyDelete
  2. You have to Open a word list (txt file) first. download english.txt or tagalog.txt word lists

    ReplyDelete
  3. Nice one mate. I will add a link to this app from my site.

    ReplyDelete
  4. i did... either nothing happens or the program crashes...

    ReplyDelete
  5. Nice apps...downloading it now :)
    Keep up the great work

    ReplyDelete

Note: Only a member of this blog may post a comment.