Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef LATINIME_DAEMARU_LEVENSHTEIN_EDIT_DISTANCE_POLICY_H
     18 #define LATINIME_DAEMARU_LEVENSHTEIN_EDIT_DISTANCE_POLICY_H
     19 
     20 #include "suggest/policyimpl/utils/edit_distance_policy.h"
     21 #include "utils/char_utils.h"
     22 
     23 namespace latinime {
     24 
     25 class DamerauLevenshteinEditDistancePolicy : public EditDistancePolicy {
     26  public:
     27     DamerauLevenshteinEditDistancePolicy(const int *const string0, const int length0,
     28             const int *const string1, const int length1)
     29             : mString0(string0), mString0Length(length0), mString1(string1),
     30               mString1Length(length1) {}
     31     ~DamerauLevenshteinEditDistancePolicy() {}
     32 
     33     AK_FORCE_INLINE float getSubstitutionCost(const int index0, const int index1) const {
     34         const int c0 = CharUtils::toBaseLowerCase(mString0[index0]);
     35         const int c1 = CharUtils::toBaseLowerCase(mString1[index1]);
     36         return (c0 == c1) ? 0.0f : 1.0f;
     37     }
     38 
     39     AK_FORCE_INLINE float getDeletionCost(const int index0, const int index1) const {
     40         return 1.0f;
     41     }
     42 
     43     AK_FORCE_INLINE float getInsertionCost(const int index0, const int index1) const {
     44         return 1.0f;
     45     }
     46 
     47     AK_FORCE_INLINE bool allowTransposition(const int index0, const int index1) const {
     48         const int c0 = CharUtils::toBaseLowerCase(mString0[index0]);
     49         const int c1 = CharUtils::toBaseLowerCase(mString1[index1]);
     50         if (index0 > 0 && index1 > 0 && c0 == CharUtils::toBaseLowerCase(mString1[index1 - 1])
     51                 && c1 == CharUtils::toBaseLowerCase(mString0[index0 - 1])) {
     52             return true;
     53         }
     54         return false;
     55     }
     56 
     57     AK_FORCE_INLINE float getTranspositionCost(const int index0, const int index1) const {
     58         return getSubstitutionCost(index0, index1);
     59     }
     60 
     61     AK_FORCE_INLINE int getString0Length() const {
     62         return mString0Length;
     63     }
     64 
     65     AK_FORCE_INLINE int getString1Length() const {
     66         return mString1Length;
     67     }
     68 
     69  private:
     70     DISALLOW_COPY_AND_ASSIGN (DamerauLevenshteinEditDistancePolicy);
     71 
     72     const int *const mString0;
     73     const int mString0Length;
     74     const int *const mString1;
     75     const int mString1Length;
     76 };
     77 } // namespace latinime
     78 
     79 #endif  // LATINIME_DAEMARU_LEVENSHTEIN_EDIT_DISTANCE_POLICY_H
     80