From b4d3ee033ddb9d4f51069e9afe5b190f551094f0 Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Sat, 26 Dec 2020 12:45:28 +0800 Subject: [PATCH] :zap::zap::zap: add chinesecharactertopinyin to file-ulils --- file-utils.cpp | 40 +++++++++++++++++++++++++++++ file-utils.h | 11 ++++++++ index/chinesecharacterstopinyin.cpp | 2 +- index/chinesecharacterstopinyin.h | 5 ++-- index/index-generator.cpp | 6 ++--- index/index.pri | 4 +-- src/main.cpp | 3 +++ src/mainwindow.cpp | 1 + 8 files changed, 64 insertions(+), 8 deletions(-) diff --git a/file-utils.cpp b/file-utils.cpp index d636cbd..5a8aa5e 100644 --- a/file-utils.cpp +++ b/file-utils.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +QMap FileUtils::map_chinese2pinyin = QMap(); FileUtils::FileUtils() { @@ -144,3 +146,41 @@ QString FileUtils::getAppName(const QString& path) { QString FileUtils::getSettingName(const QString& setting) { return setting.right(setting.length() - setting.lastIndexOf("/") - 1); } + + +void FileUtils::loadHanziTable(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + qDebug("File: '%s' open failed!", file.fileName().toStdString().c_str()); + return; + } + + /* 读取汉字对照表文件并转换为QMap存储 */ + while(!file.atEnd()) { + QString content = QString::fromUtf8(file.readLine()); + FileUtils::map_chinese2pinyin[content.split(" ").last().trimmed()] = content.split(" ").first().split(","); + } + + file.close(); + + return; +} + +QString FileUtils::find(const QString &hanzi) +{ +// static QMap map = loadHanziTable("://index/pinyinWithoutTone.txt"); +// static QMap map; + QString output; + QStringList stringList = hanzi.split(""); + + /* 遍历查找汉字-拼音对照表的内容并将汉字替换为拼音 */ + for (const QString &str : stringList) { + if (FileUtils::map_chinese2pinyin.contains(str)) + output += FileUtils::map_chinese2pinyin[str].first(); + else + output += str; + } + + return output; +} diff --git a/file-utils.h b/file-utils.h index 55b7fa4..8b37500 100644 --- a/file-utils.h +++ b/file-utils.h @@ -4,6 +4,10 @@ #include #include #include +#include + + + class FileUtils { @@ -17,6 +21,13 @@ public: static QString getFileName(const QString &); static QString getAppName(const QString &); static QString getSettingName(const QString &); + + static QMap map_chinese2pinyin; + + //chinese character to pinyin + static QString find(const QString&); + static void loadHanziTable(const QString&); + private: FileUtils(); }; diff --git a/index/chinesecharacterstopinyin.cpp b/index/chinesecharacterstopinyin.cpp index c0a4850..abb7416 100644 --- a/index/chinesecharacterstopinyin.cpp +++ b/index/chinesecharacterstopinyin.cpp @@ -2,7 +2,7 @@ chineseCharactersToPinyin::chineseCharactersToPinyin(QObject *parent) : QObject(parent) { - + map = loadHanziTable("://index/pinyinWithoutTone.txt"); } diff --git a/index/chinesecharacterstopinyin.h b/index/chinesecharacterstopinyin.h index 7805fed..d3e2c89 100644 --- a/index/chinesecharacterstopinyin.h +++ b/index/chinesecharacterstopinyin.h @@ -12,8 +12,8 @@ public: explicit chineseCharactersToPinyin(QObject *parent = nullptr); static QString find(const QString &hanzi) { -// static QMap map = loadHanziTable("://index/pinyinWithoutTone.txt"); - static QMap map; + static QMap map = loadHanziTable("://index/pinyinWithoutTone.txt"); +// static QMap map; QString output; QStringList stringList = hanzi.split(""); @@ -30,6 +30,7 @@ public: Q_SIGNALS: private: + static QMap map; /* 加载汉字对照表 */ static QMap loadHanziTable(const QString &fileName) { diff --git a/index/index-generator.cpp b/index/index-generator.cpp index 9aa872d..d886cf8 100644 --- a/index/index-generator.cpp +++ b/index/index-generator.cpp @@ -116,8 +116,8 @@ Document IndexGenerator::GenerateDocument(const QVector &list) QString sourcePath = list.at(1); index_text = index_text.replace(".","").replace(""," "); index_text = index_text.simplified(); -// QString pinyin_text = chineseCharactersToPinyin::find(filename.replace(".", "")).replace("", " "); -// pinyin_text = pinyin_text.simplified(); + QString pinyin_text = FileUtils::find(index_text.replace(".", "")).replace("", " "); + pinyin_text = pinyin_text.simplified(); QString uniqueterm = QString::fromStdString(FileUtils::makeDocUterm(sourcePath)); // QString uniqueterm1 = QString::fromStdString(QCryptographicHash::hash(sourcePath.toUtf8(),QCryptographicHash::Md5).toStdString()); /*--------------------------------------------------------------------*/ @@ -133,7 +133,7 @@ Document IndexGenerator::GenerateDocument(const QVector &list) doc.setData(sourcePath); doc.setUniqueTerm(uniqueterm); doc.addValue(list.at(2)); -// doc.setIndexText(QStringList()<>("QVector"); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 1c62eef..43dabc8 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -47,6 +47,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { + /*-------------InotyifyRefact Test Start---------------*/ QTime t1 = QTime::currentTime(); InotifyManagerRefact* imr = new InotifyManagerRefact("/home");