Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2014, 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_NGRAM_UTILS_H
     18 #define LATINIME_NGRAM_UTILS_H
     19 
     20 #include "defines.h"
     21 
     22 namespace latinime {
     23 
     24 enum class NgramType : int {
     25     Unigram = 0,
     26     Bigram = 1,
     27     Trigram = 2,
     28     Quadgram = 3,
     29     NotANgramType = -1,
     30 };
     31 
     32 namespace AllNgramTypes {
     33 // Use anonymous namespace to avoid ODR (One Definition Rule) violation.
     34 namespace {
     35 
     36 const NgramType ASCENDING[] = {
     37    NgramType::Unigram, NgramType::Bigram, NgramType::Trigram
     38 };
     39 
     40 const NgramType DESCENDING[] = {
     41    NgramType::Trigram, NgramType::Bigram, NgramType::Unigram
     42 };
     43 
     44 }  // namespace
     45 }  // namespace AllNgramTypes
     46 
     47 class NgramUtils final {
     48  public:
     49     static AK_FORCE_INLINE NgramType getNgramTypeFromWordCount(const int wordCount) {
     50         // Max supported ngram is (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram.
     51         if (wordCount <= 0 || wordCount > MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1) {
     52             return NgramType::NotANgramType;
     53         }
     54         // Convert word count to 0-origin enum value.
     55         return static_cast<NgramType>(wordCount - 1);
     56     }
     57 
     58  private:
     59     DISALLOW_IMPLICIT_CONSTRUCTORS(NgramUtils);
     60 
     61 };
     62 }
     63 #endif /* LATINIME_NGRAM_UTILS_H */
     64