- /**
- * filename: HammingEDRule.java
- * package:
- * author: Nick Ma
- * email: nick.ma85@yahoo.com
- * date: 2008-11-28
- * description: a simple algorithm to get distance of the difference
- * between two strings, just measuring the substitution distance
- * like Hamming Distance.
- */
- public class HammingEDRule implements EditDistanceRule {
- /* (non-Javadoc)
- * @see spellChecker.EditDistanceRule#getDistance(java.lang.String, java.lang.String)
- */
- @Override
- public int getDistance(String s, String t) {
- // TODO Auto-generated method stub
- int distance = 0; // the edit distance to return
- // check the validity of the two strings
- if(s == null || t == null) {
- return 0;
- }
- // let t be the longer string
- if(s.length() > t.length()) {
- String temp = s;
- s = t;
- t = temp;
- }
- // calculate the distance and then return it
- for(int i = 0; i < s.length(); i++) {
- if(s.charAt(i) != t.charAt(i)) {
- distance++;
- }
- }
- distance += t.length() - s.length();
- return distance;
- }
- }
Hamming Distance
最新推荐文章于 2023-06-28 09:18:16 发布