• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 LIBTEXTCLASSIFIER_LANG_ID_LIGHT_SENTENCE_H_
18 #define LIBTEXTCLASSIFIER_LANG_ID_LIGHT_SENTENCE_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include "util/base/logging.h"
24 #include "util/base/macros.h"
25 
26 namespace libtextclassifier {
27 namespace nlp_core {
28 namespace lang_id {
29 
30 // Simplified replacement for the Sentence proto, for internal use in the
31 // language identification code.
32 //
33 // In this simplified form, a sentence is a vector of words, each word being a
34 // string.
35 class LightSentence {
36  public:
LightSentence()37   LightSentence() {}
38 
39   // Adds a new word after all existing ones, and returns a pointer to it.  The
40   // new word is initialized to the empty string.
add_word()41   std::string *add_word() {
42     words_.emplace_back();
43     return &(words_.back());
44   }
45 
46   // Returns number of words from this LightSentence.
num_words()47   int num_words() const { return words_.size(); }
48 
49   // Returns the ith word from this LightSentence.  Note: undefined behavior if
50   // i is out of bounds.
word(int i)51   const std::string &word(int i) const {
52     TC_DCHECK((i >= 0) && (i < num_words()));
53     return words_[i];
54   }
55 
56  private:
57   std::vector<std::string> words_;
58 
59   TC_DISALLOW_COPY_AND_ASSIGN(LightSentence);
60 };
61 
62 }  // namespace lang_id
63 }  // namespace nlp_core
64 }  // namespace libtextclassifier
65 
66 #endif  // LIBTEXTCLASSIFIER_LANG_ID_LIGHT_SENTENCE_H_
67