55#include < QDebug>
66#include < QLineEdit>
77#include < QRegularExpression>
8- #include < QTextCodec>
98
109#include < cpp-kana/Kana.h>
1110
1211TextWidget::TextWidget (QWidget *parent)
13- : QWidget(parent), g2p_man(new Pinyin::Pinyin()), g2p_canton(new Pinyin::Jyutping()),
14- mecabYomi(mecabInit(" mecabDict" , " yomi" )), mecabWakati(mecabInit(" mecabDict" , " wakati" )) {
12+ : QWidget(parent), g2p_man(new Pinyin::Pinyin()), g2p_canton(new Pinyin::Jyutping()) {
1513 wordsText = new QLineEdit ();
1614 wordsText->setPlaceholderText (" Enter mandarin here..." );
1715
@@ -88,19 +86,18 @@ TextWidget::TextWidget(QWidget *parent)
8886 connect (replaceAction, &QAction::triggered, this , &TextWidget::_q_replaceButtonClicked);
8987}
9088
91- TextWidget::~TextWidget (){} ;
89+ TextWidget::~TextWidget () = default ;
9290
9391QString TextWidget::sentence () const {
9492 QString words = wordsText->text ();
95- words.replace (" \r\n " , " " );
96- words. replace ( " \n " , " " );
93+ words.replace (QRegularExpression ( R"( [ \r\n]+ )" ) , " " );
94+ words = words. simplified ( );
9795 return words;
9896}
9997
10098void TextWidget::_q_pasteButtonClicked () const {
10199 const auto board = QApplication::clipboard ();
102- const QString text = board->text ();
103- if (!text.isEmpty ()) {
100+ if (const QString text = board->text (); !text.isEmpty ()) {
104101 wordsText->setText (text);
105102 }
106103}
@@ -124,7 +121,7 @@ void TextWidget::_q_replaceButtonClicked() const {
124121 break ;
125122 }
126123 case 1 : {
127- str = QString::fromUtf8 (Kana::kanaToRomaji (mecabConvert ( jpInput) .toUtf8 ().toStdString (),
124+ str = QString::fromUtf8 (Kana::kanaToRomaji (jpInput.toUtf8 ().toStdString (),
128125 Kana::Error::Default, doubleConsonant->isChecked ())
129126 .toStdStr ()
130127 .c_str ());
@@ -157,7 +154,7 @@ void TextWidget::_q_appendButtonClicked() const {
157154 break ;
158155 }
159156 case 1 : {
160- str = QString::fromUtf8 (Kana::kanaToRomaji (mecabConvert ( jpInput) .toUtf8 ().toStdString (),
157+ str = QString::fromUtf8 (Kana::kanaToRomaji (jpInput.toUtf8 ().toStdString (),
161158 Kana::Error::Default, doubleConsonant->isChecked ())
162159 .toStdStr ()
163160 .c_str ());
@@ -197,26 +194,4 @@ void TextWidget::_q_onLanguageComboIndexChanged() {
197194 for (QCheckBox *control : optionMap[selectedLanguage]) {
198195 control->show ();
199196 }
200- }
201-
202- MeCab::Tagger *TextWidget::mecabInit (const QString &path, const QString &format) {
203- const QString args = " -O" + format + " -d " + path + " -r" + path + " /mecabrc" ;
204- return MeCab::createTagger (args.toUtf8 ());
205- }
206-
207- QString TextWidget::mecabConvert (const QString &input) const {
208- const QTextCodec *codec = QTextCodec::codecForName (" GBK" );
209- const QByteArray mecabRes = mecabWakati->parse (codec->fromUnicode (input));
210-
211- QStringList out;
212- foreach (auto &it, mecabRes.split (' ' )) {
213- QString res = codec->toUnicode (mecabYomi->parse (it));
214- const QStringList item = res.split (" \t " );
215- if (item.size () > 1 ) {
216- out.append (item[1 ]);
217- } else if (!item.empty () && item[0 ] != " " ) {
218- out.append (item[0 ]);
219- }
220- }
221- return out.join (" " );
222197}
0 commit comments