MySQL
  Home arrow MySQL arrow Page 4 - Lord Of The Strings Part 2
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Lord Of The Strings Part 2
By: Simon White
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 26
    2004-03-17

    Table of Contents:
  • Lord Of The Strings Part 2
  • The Size of the Problem
  • Discovering String Similarities
  • A Class Called Compare
  • Analyzing the Results
  • The Finnish Line
  • Differences on the Table

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Lord Of The Strings Part 2 - A Class Called Compare


    (Page 4 of 7 )

    Now I return to the main logic of the program, which is implemented in five methods of a class called 'Compare'. The top-level structure of the class is as follows (and you can see the full program listing here):


    public class Compare {
        
    protected QueryRunner queryRunner;
        
        
    public Compare() {}
        
    protected Word findBestMatch(Word baseWordCollection collection) {}   
        
    protected void storeBestMatch(Word wWord bestMatchdouble bestScore) {}
        
    protected Word getWord(String langthrows SQLException {}  
        
    protected Collection getCandidateStrings(String strthrows SQLException {}
    }

    As you can see, the class uses an instance of a QueryRunner to access the database, and also makes use of the Word class, both as a method parameter and a result. The following pseudo-code explains the algorithm and the interaction between the methods:


    OpenDatabaseConnection;
    // Get a Tolkien word that has not been considered
    While (w = getWord("Tolkien") {
      // Run a first pass query to return promising strings
      Collection candidates = getCandidateStrings(w);
      // Find the best match of those candidates
      findBestMatch(w, candidates);
    }
    CloseDatabaseConnection;

    First, I open a database connection, and then repeatedly retrieve Tolkien words that have not yet been considered. (The getWord() method runs an SQL query, which returns a Tolkien word that does not yet appear in the comparison results table.) For each Tolkien word, I run a 'first pass' query to retrieve a subset of promising 'candidate' words from all those in the database. I used the database's pattern matching ability to return all those (non-Tolkien) words that start with the same two letters as the Tolkien word. The program then calls findBestMatch() on the Tolkien word and the candidate words to find and store the best matching word. When this has been done for all the Tolkien words, the database connection is closed.


    findBestMatch(Word wCollection candidates):
      bestScore 
    = -1;
      
    for each c in candidates {
        similarity 
    compareStrings(cw);
        
    if (similarity bestScore) {
                    bestScore 
    similarity;
                    bestMatch 
    c;
                
    }
      
    }
      storeBestMatch
    (wbestMatchbestScore);



    Candidate Strings

    In the findBestMatch() method, the program iterates through each candidate word and computes the similarity metric. If the similarity metric is better than the previous best, then the program stores the new value as its current best. Once all the candidate strings have been considered, the best match is saved to the database using an SQL update query.

    More MySQL Articles
    More By Simon White


     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT