Development Cycles
  Home arrow Development Cycles arrow Page 5 - How to Strike a Match
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? 
DEVELOPMENT CYCLES

How to Strike a Match
By: Simon White
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 35
    2004-04-07

    Table of Contents:
  • How to Strike a Match
  • The New Metric
  • A Real World Example
  • A Java Implementation
  • Finishing the Java Implementation
  • Summary

  • 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


    How to Strike a Match - Finishing the Java Implementation


    (Page 5 of 6 )

    Taking Account of White Space

    This method uses the split() method of the String class to split the input string into separate words, or tokens. It then iterates through each of the words, computing the character pairs for each word. The character pairs are added to an ArrayList, which is returned from the method. An ArrayList is used, rather than an array, because we do not know in advance how many character pairs will be returned. (At this point, the program doesn’t know how much white space the input string contains.)


    /** @return an ArrayList of 2-character Strings. */
       
    private static ArrayList wordLetterPairs(String str) {
           ArrayList allPairs 
    = new ArrayList();
           
    // Tokenize the string and put the tokens/words into an array 
           String[] words = str.split("s");
           // For each word
           for (int w=0; w < words.length; w++) {
               // Find the pairs of characters
               String[] pairsInWord = letterPairs(words[w]);
               for (int p=0; p < pairsInWord.length; p++) {
                   allPairs.add(pairsInWord[p]);
               }
           }
           return allPairs;
       }

    Computing the Metric

    This public method computes the character pairs from the words of each of the two input strings, then iterates through the ArrayLists to find the size of the intersection. Note that whenever a match is found, that character pair is removed from the second array list to prevent us from matching against the same character pair multiple times. (Otherwise, ‘GGGGG’ would score a perfect match against ‘GG’.)


    /** @return lexical similarity value in the range [0,1] */
       
    public static double compareStrings(String str1String str2) {
           ArrayList pairs1 
    wordLetterPairs(str1.toUpperCase());
           ArrayList pairs2 
    wordLetterPairs(str2.toUpperCase());
           int intersection 
    0;
           int union 
    pairs1.size() + pairs2.size();
           
    for (int i=0i<pairs1.size(); i++) {
               Object pair1
    =pairs1.get(i);
               
    for(int j=0j<pairs2.size(); j++) {
                   Object pair2
    =pairs2.get(j);
                   
    if (pair1.equals(pair2)) {
                       intersection
    ++;
                       pairs2
    .remove(j);
                       
    break;
                   
    }
               
    }
           
    }
           
    return (2.0*intersection)/union;
       
    }

    More Development Cycles Articles
    More By Simon White


     

    DEVELOPMENT CYCLES ARTICLES

    - Branch and Bound Algorithm Technique
    - Dynamic Programming Algorithm Technique
    - Genetic Algorithm Techniques
    - Greedy Strategy as an Algorithm Technique
    - Divide and Conquer Algorithm Technique
    - The Backtracking Algorithm Technique
    - More Pattern Matching Algorithms: B-M
    - Pattern Matching Algorithms Demystified: KMP
    - Coding Standards
    - A Peek into the Future: Transactional Memory
    - Learning About the Graph Construct using Gam...
    - Learning About the Graph Construct using Gam...
    - Learning About the Graph Construct using Gam...
    - How to Strike a Match
    - Entity Relationship Modeling






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