Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce all duplicated spaces #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/model/UserphraseModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,21 @@ void UserphraseModel::exportUserphrase(std::shared_ptr<UserphraseExporter> expor
emit exportCompleted(result, exporter.get()->getPath(), exported);
}

QString UserphraseModel::checkBopomofo(const QString &bopomofo) const
{
QString replaceBopomofo = bopomofo;
replaceBopomofo.replace(QString::fromUtf8("一"),QString::fromUtf8("ㄧ"));
replaceBopomofo.replace(QString::fromUtf8("丫"),QString::fromUtf8("ㄚ"));

return replaceBopomofo;
}

void UserphraseModel::add(const QString &phrase, const QString &bopomofo)
{
QString replaceBopomofo = checkBopomofo(bopomofo);
QString normalizedBopomofo = BopomofoUtil::normalize(bopomofo);
auto ret = chewing_userphrase_add(
ctx_.get(),
phrase.toUtf8().constData(),
replaceBopomofo.toUtf8().constData());
normalizedBopomofo.toUtf8().constData());

addresult_ = ret;

if (ret > 0) {
emit beginResetModel();
userphrase_.insert(Userphrase{
phrase,
bopomofo
normalizedBopomofo
});
emit endResetModel();
emit addNewPhraseCompleted(userphrase_[userphrase_.size()-1]);
Expand Down
3 changes: 1 addition & 2 deletions src/model/UserphraseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "UserphraseExporter.h"
#include "UserphraseImporter.h"
#include "UserphraseSet.h"
#include "BopomofoUtil.hpp"

class UserphraseModel final: public QAbstractListModel {
Q_OBJECT
Expand Down Expand Up @@ -71,8 +72,6 @@ public slots:
void undo();

private:
QString checkBopomofo(const QString &bopomofo) const;

std::unique_ptr<ChewingContext, void (*)(ChewingContext*)> ctx_;
UserphraseSet userphrase_;
std::vector<Userphrase> removerecord_;
Expand Down
38 changes: 38 additions & 0 deletions src/util/BopomofoUtil.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* chewing-editor: Chewing userphrase editor
* Copyright (C) 2014 Chewing Development Team
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <QString>

namespace BopomofoUtil {
static QString normalize(const QString &bopomofo)
{
QString normalized = bopomofo;

// Issue #106: replace ambiguity chars
normalized.replace(QString::fromUtf8(""), QString::fromUtf8(""));
normalized.replace(QString::fromUtf8(""), QString::fromUtf8(""));

// Issue #204: reduce spaces into one space
normalized.simplified();

return normalized;
}
};
38 changes: 38 additions & 0 deletions test/testBopomofoUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* chewing-editor: Chewing userphrase editor
* Copyright (C) 2014 Chewing Development Team

* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "gtest/gtest.h"

#include "BopomofoUtil.hpp"

#if (defined _MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif

class TestBopomofoUtil : public ::testing::Test {
protected:
TestBopomofoUtil() = default;
virtual ~TestBopomofoUtil() = default;
};

TEST_F(TestBopomofoUtil, ReduceDuplicatedSpacesInsideBopomofo)
{
ASSERT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", BopomofoUtil::normalize("ㄘㄜˋ ㄕˋ")));
ASSERT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", BopomofoUtil::normalize("ㄘㄜˋ ㄕˋ")));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this test also including the test of replacing "一" to "ㄧ" and "丫" to "ㄚ"?
This commit cover this feature, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nop, this PR is just moving the original part of replacing ambugity words into a sperated file and adding the reducing duplicated spaces function.

But, yes, we still need tests for replacing ambugity words, that's why I move it. After this PR, I'll submit another PR for the tests.

}