4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SoundexMethod.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Subproject: Position of fragment of text in XML document
4  * Authors: Michael Angelov
5  * File: SoundexMethod.java
6  * Description: Compare class using Soundex approximate string matching method
7  */
8 
9 /**
10  * @file SoundexMethod.java
11  *
12  * @brief Compare class using Soundex approximate string matching method
13  */
14 
15 package cz.vutbr.fit.knot.annotations.fragmentUpdater.compareMethods;
16 
17 import org.apache.commons.codec.language.Soundex;
18 
19 /**
20  * Compare class using Soundex approximate string matching method
21  *
22  * @brief Compare class using Soundex approximate string matching method
23  * @author Michael Angelov
24  */
25 public class SoundexMethod extends CompareMethod {
26 
27  /**
28  * String comparison method using Soundex encoding of strings
29  *
30  * @param first first string to compare
31  * @param second second string to compare
32  * @return true if strings match, false if don't
33  */
34  @Override
35  public boolean compare(String first, String second) {
36  if (second == null) {
37  return false;
38  }
39  Soundex soundex = new Soundex();
40 
41  String[] firstWords = first.split("\\s+");
42  String[] secondWords = second.split("\\s+");
43 
44  // iterate through all the words of first and second string
45  int i = 0;
46  while (i < firstWords.length && i < secondWords.length) {
47  // if one pair of words doesn't match, strings don't match, return false
48  if (!soundex.encode(firstWords[i]).equals(soundex.encode(secondWords[i]))) {
49  return false;
50  }
51  i++;
52  }
53 
54  // everything went ok, words of string match
55  return true;
56  }
57 
58  /**
59  * Overriden toString() method
60  *
61  * @return string representation of class
62  */
63  @Override
64  public String toString() {
65  return "Soundex";
66  }
67 } // class SoundexMethod
Compare class using Soundex approximate string matching method.