SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
replace.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_REPLACE_HPP
9 #define SIG_UTIL_REPLACE_HPP
10 
11 #include "../sigutil.hpp"
12 #include <unordered_map>
13 
14 
16 
17 namespace sig
18 {
19 
21 
27 {
28  std::unordered_map<wchar_t, wchar_t> alphabet_; // 全角アルファベット -> 半角アルファベット
29  std::unordered_map<wchar_t, wchar_t> number_; // 全角数字 -> 半角数字
30  std::unordered_map<wchar_t, wchar_t> katakana_; // 半角カタカナ -> 全角カタカナ
31  std::unordered_map<std::wstring, wchar_t> katakana_d_; // (濁音) 半角カタカナ -> 全角カタカナ
32 
33 private:
34  ZenHanReplace(){
35  wchar_t zen1[2] = L"A";
36  char han1 = 'A';
37 
38  for (uint i = 0; i < 26; ++i, ++zen1[0], ++han1){
39  alphabet_[zen1[0]] = han1;
40  }
41 
42  wchar_t zen2 = L'a';
43  wchar_t han2 = 'a';
44  for (uint i = 0; i < 26; ++i, ++zen2, ++han2){
45  alphabet_[zen2] = han2;
46  }
47 
48  number_[L'0'] = L'0';
49  number_[L'1'] = L'1';
50  number_[L'2'] = L'2';
51  number_[L'3'] = L'3';
52  number_[L'4'] = L'4';
53  number_[L'5'] = L'5';
54  number_[L'6'] = L'6';
55  number_[L'7'] = L'7';
56  number_[L'8'] = L'8';
57  number_[L'9'] = L'9';
58  katakana_[L'ー'] = L'ー';
59  katakana_[L'ァ'] = L'ァ';
60  katakana_[L'ィ'] = L'ィ';
61  katakana_[L'ゥ'] = L'ゥ';
62  katakana_[L'ェ'] = L'ェ';
63  katakana_[L'ォ'] = L'ォ';
64  katakana_[L'ア'] = L'ア';
65  katakana_[L'イ'] = L'イ';
66  katakana_[L'ウ'] = L'ウ';
67  katakana_[L'エ'] = L'エ';
68  katakana_[L'オ'] = L'オ';
69 
70  wchar_t han3 = L'カ';
71  wchar_t zen3 = L'カ';
72  for (uint i = 0; i < 12; ++i, ++han3, zen3 += 2){
73  katakana_[han3] = zen3;
74  }
75  katakana_[L'ッ'] = L'ッ';
76  katakana_[L'テ'] = L'テ';
77  katakana_[L'ト'] = L'ト';
78  han3 += 2; zen3 += 6;
79  for (uint i = 0; i < 6; ++i, ++han3, ++zen3){
80  katakana_[han3] = zen3;
81  }
82  for (uint i = 0; i < 5; ++i, ++han3, zen3 += 3){
83  katakana_[han3] = zen3;
84  }
85  for (uint i = 0; i < 5; ++i, ++han3, ++zen3){
86  katakana_[han3] = zen3;
87  }
88  katakana_[L'ヤ'] = L'ヤ';
89  katakana_[L'ャ'] = L'ャ';
90  katakana_[L'ユ'] = L'ユ';
91  katakana_[L'ュ'] = L'ュ';
92  katakana_[L'ヨ'] = L'ヨ';
93  katakana_[L'ョ'] = L'ョ';
94  han3 += 3; zen3 += 6;
95  for (uint i = 0; i < 5; ++i, ++han3, ++zen3){
96  katakana_[han3] = zen3;
97  }
98  katakana_[L'ワ'] = L'ワ';
99  katakana_[L'ヲ'] = L'ヲ';
100  katakana_[L'ン'] = L'ン';
101 
102  wchar_t han4[3] = L"ガ";
103  wchar_t zen4 = L'ガ';
104  for (uint i = 0; i < 12; ++i, ++han4[0], zen4 += 2){
105  katakana_d_[han4] = zen4;
106  }
107  katakana_d_[L"ヅ"] = L'ヅ';
108  katakana_d_[L"デ"] = L'デ';
109  katakana_d_[L"ド"] = L'ド';
110  han4[0] += 8; zen4 += 12;
111  for (uint i = 0; i < 5; ++i, ++han4[0], zen4 += 3){
112  katakana_d_[han4] = zen4;
113  }
114  wchar_t han5[3] = L"パ";
115  wchar_t zen5 = L'パ';
116  for (uint i = 0; i < 5; ++i, ++han5[0], zen5 += 3){
117  katakana_d_[han5] = zen5;
118  }
119  }
120 
121  ZenHanReplace(const ZenHanReplace&) = delete;
122 
123 public:
125 
131  static ZenHanReplace instance;
132  return instance;
133  }
134 
136  void alphabet_zen2han(std::wstring& sentence) const{
137  for (auto& c : sentence){
138  if (alphabet_.count(c)) c = alphabet_.at(c);
139  }
140  }
141 
143  void alphabet_han2zen(std::wstring& sentence) const{
144  for (auto& c : sentence){
145  for (auto const& v : alphabet_){
146  if (v.second == c) c = v.first;
147  }
148  }
149  }
150 
152  void number_zen2han(std::wstring& sentence) const{
153  for (auto& c : sentence){
154  if (number_.count(c)) c = number_.at(c);
155  }
156  }
157 
159  void number_han2zen(std::wstring& sentence) const{
160  for (auto& c : sentence){
161  for (auto const& v : number_){
162  if (v.second == c) c = v.first;
163  }
164  }
165  }
166 
168  void katakana_zen2han(std::wstring& sentence) const{
169  for (uint i = 0; i<sentence.size(); ++i){
170  for (auto const& v : katakana_d_){
171  if (v.second == sentence[i]){
172  sentence.replace(i, 1, v.first);
173  }
174  }
175  }
176  for (auto& c : sentence){
177  for (auto const& v : katakana_){
178  if (v.second == c) c = v.first;
179  }
180  }
181  }
182 
183 
185  void katakana_han2zen(std::wstring& sentence) const{
186  for (unsigned i = 1; i < sentence.size(); ++i){
187  if (sentence[i] == L'゙' || sentence[i] == L'゚'){
188  auto ttmp = std::wstring(sentence.substr(i - 1, 2));
189  if (katakana_d_.count(ttmp)){
190  sentence.replace(i - 1, 2, 1, katakana_d_.at(ttmp));
191  --i;
192  }
193  }
194  }
195  for (auto& c : sentence){
196  if (katakana_.count(c)) c = katakana_.at(c);
197  }
198  }
199 };
200 
201 }
202 #endif
void number_zen2han(std::wstring &sentence) const
数字 全角 -> 半角
Definition: replace.hpp:152
void alphabet_han2zen(std::wstring &sentence) const
アルファベット 半角 -> 全角
Definition: replace.hpp:143
void katakana_zen2han(std::wstring &sentence) const
カタカナ 全角 -> 半角
Definition: replace.hpp:168
void alphabet_zen2han(std::wstring &sentence) const
アルファベット 全角 -> 半角
Definition: replace.hpp:136
全角・半角文字の置換処理を行う
Definition: replace.hpp:26
void katakana_han2zen(std::wstring &sentence) const
カタカナ 半角 -> 全角
Definition: replace.hpp:185
static ZenHanReplace & get_instance()
インスタンスの参照を取得
Definition: replace.hpp:130
void number_han2zen(std::wstring &sentence) const
数字 半角 -> 全角
Definition: replace.hpp:159
Definition: array.hpp:15