1 /* Copyright (C) 2013, 2017 Olga Yakovleva <yakovleva.o.v@gmail.com> */ 2 3 /* This program is free software: you can redistribute it and/or modify */ 4 /* it under the terms of the GNU Lesser General Public License as published by */ 5 /* the Free Software Foundation, either version 2.1 of the License, or */ 6 /* (at your option) any later version. */ 7 8 /* This program is distributed in the hope that it will be useful, */ 9 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 10 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 11 /* GNU Lesser General Public License for more details. */ 12 13 /* You should have received a copy of the GNU Lesser General Public License */ 14 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #ifndef RHVOICE_VOICE_PROFILE_HPP 17 #define RHVOICE_VOICE_PROFILE_HPP 18 19 #include <string> 20 #include <vector> 21 #include "voice.hpp" 22 23 namespace RHVoice 24 { 25 class voice_profile 26 { 27 private: 28 typedef std::vector<voice_list::const_iterator> list; 29 30 list voices; 31 std::string name; 32 33 public: 34 typedef std::vector<voice_list::const_iterator>::const_iterator iterator; 35 36 voice_profile() 37 { 38 } 39 40 explicit voice_profile(voice_list::const_iterator only_voice) 41 { 42 voices.push_back(only_voice); 43 name=only_voice->get_name(); 44 } 45 46 bool operator<(const voice_profile& other) const 47 { 48 return (name<other.name); 49 } 50 51 const std::string& get_name() const 52 { 53 return name; 54 } 55 56 iterator begin() const 57 { 58 return voices.begin(); 59 } 60 61 iterator end() const 62 { 63 return voices.end(); 64 } 65 66 bool add(voice_list::const_iterator v) 67 { 68 language_list::const_iterator lang=v->get_language(); 69 for(iterator it=begin();it!=end();++it) 70 { 71 if((*it)->get_language()==lang) 72 return false; 73 } 74 voices.push_back(v); 75 if(name.empty()) 76 name=v->get_name(); 77 else 78 { 79 name+="+"; 80 name+=v->get_name(); 81 } 82 return true; 83 } 84 85 bool empty() const 86 { 87 return voices.empty(); 88 } 89 90 std::size_t voice_count() const 91 { 92 return voices.size(); 93 } 94 95 voice_list::const_iterator primary() const 96 { 97 return voices.at(0); 98 } 99 100 iterator voice_for_language(language_list::const_iterator lang) const 101 { 102 for(iterator it=begin();it!=end();++it) 103 { 104 voice_list::const_iterator v=*it; 105 if(v->get_language()==lang) 106 return it; 107 } 108 return end(); 109 } 110 111 template<typename text_iterator> iterator voice_for_text(text_iterator text_start,text_iterator text_end) const; 112 }; 113 114 template<typename text_iterator> 115 voice_profile::iterator voice_profile::voice_for_text(text_iterator text_start,text_iterator text_end) const 116 { 117 iterator best=end(); 118 voice_list::const_iterator v; 119 language_list::const_iterator lang; 120 std::size_t max_count=0; 121 std::size_t count; 122 for(iterator it=begin();it!=end();++it) 123 { 124 v=*it; 125 lang=v->get_language(); 126 if((best!=end())&&lang->has_common_letters(*((*best)->get_language()))) 127 continue; 128 count=lang->count_letters_in_text(text_start,text_end); 129 if(count>max_count) 130 { 131 best=it; 132 max_count=count; 133 if(it==begin()) 134 return best; 135 } 136 } 137 return best; 138 } 139 } 140 #endif 141