Merge pull request #14 from MouseZhangZh/zzh-search

 add chinesecharactertopinyin to file-ulils
This commit is contained in:
iaom 2020-12-26 13:46:31 +08:00 committed by GitHub
commit e86a16969b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 8 deletions

View File

@ -2,6 +2,8 @@
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QUrl> #include <QUrl>
#include <QMap>
QMap<QString, QStringList> FileUtils::map_chinese2pinyin = QMap<QString, QStringList>();
FileUtils::FileUtils() FileUtils::FileUtils()
{ {
@ -144,3 +146,41 @@ QString FileUtils::getAppName(const QString& path) {
QString FileUtils::getSettingName(const QString& setting) { QString FileUtils::getSettingName(const QString& setting) {
return setting.right(setting.length() - setting.lastIndexOf("/") - 1); 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<QString, QStringList> map = loadHanziTable("://index/pinyinWithoutTone.txt");
// static QMap<QString, QStringList> 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;
}

View File

@ -4,6 +4,10 @@
#include <QString> #include <QString>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QIcon> #include <QIcon>
#include <QMap>
class FileUtils class FileUtils
{ {
@ -17,6 +21,13 @@ public:
static QString getFileName(const QString &); static QString getFileName(const QString &);
static QString getAppName(const QString &); static QString getAppName(const QString &);
static QString getSettingName(const QString &); static QString getSettingName(const QString &);
static QMap<QString, QStringList> map_chinese2pinyin;
//chinese character to pinyin
static QString find(const QString&);
static void loadHanziTable(const QString&);
private: private:
FileUtils(); FileUtils();
}; };

View File

@ -2,7 +2,7 @@
chineseCharactersToPinyin::chineseCharactersToPinyin(QObject *parent) : QObject(parent) chineseCharactersToPinyin::chineseCharactersToPinyin(QObject *parent) : QObject(parent)
{ {
map = loadHanziTable("://index/pinyinWithoutTone.txt");
} }

View File

@ -12,8 +12,8 @@ public:
explicit chineseCharactersToPinyin(QObject *parent = nullptr); explicit chineseCharactersToPinyin(QObject *parent = nullptr);
static QString find(const QString &hanzi) static QString find(const QString &hanzi)
{ {
// static QMap<QString, QStringList> map = loadHanziTable("://index/pinyinWithoutTone.txt"); static QMap<QString, QStringList> map = loadHanziTable("://index/pinyinWithoutTone.txt");
static QMap<QString, QStringList> map; // static QMap<QString, QStringList> map;
QString output; QString output;
QStringList stringList = hanzi.split(""); QStringList stringList = hanzi.split("");
@ -30,6 +30,7 @@ public:
Q_SIGNALS: Q_SIGNALS:
private: private:
static QMap<QString, QStringList> map;
/* 加载汉字对照表 */ /* 加载汉字对照表 */
static QMap<QString, QStringList> loadHanziTable(const QString &fileName) static QMap<QString, QStringList> loadHanziTable(const QString &fileName)
{ {

View File

@ -116,8 +116,8 @@ Document IndexGenerator::GenerateDocument(const QVector<QString> &list)
QString sourcePath = list.at(1); QString sourcePath = list.at(1);
index_text = index_text.replace(".","").replace(""," "); index_text = index_text.replace(".","").replace(""," ");
index_text = index_text.simplified(); index_text = index_text.simplified();
// QString pinyin_text = chineseCharactersToPinyin::find(filename.replace(".", "")).replace("", " "); QString pinyin_text = FileUtils::find(index_text.replace(".", "")).replace("", " ");
// pinyin_text = pinyin_text.simplified(); pinyin_text = pinyin_text.simplified();
QString uniqueterm = QString::fromStdString(FileUtils::makeDocUterm(sourcePath)); QString uniqueterm = QString::fromStdString(FileUtils::makeDocUterm(sourcePath));
// QString uniqueterm1 = QString::fromStdString(QCryptographicHash::hash(sourcePath.toUtf8(),QCryptographicHash::Md5).toStdString()); // QString uniqueterm1 = QString::fromStdString(QCryptographicHash::hash(sourcePath.toUtf8(),QCryptographicHash::Md5).toStdString());
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
@ -133,7 +133,7 @@ Document IndexGenerator::GenerateDocument(const QVector<QString> &list)
doc.setData(sourcePath); doc.setData(sourcePath);
doc.setUniqueTerm(uniqueterm); doc.setUniqueTerm(uniqueterm);
doc.addValue(list.at(2)); doc.addValue(list.at(2));
// doc.setIndexText(QStringList()<<index_text<<pinyin_text); doc.setIndexText(QStringList()<<index_text<<pinyin_text);
doc.setIndexText(QStringList()<<index_text); doc.setIndexText(QStringList()<<index_text);
return doc; return doc;

View File

@ -1,7 +1,7 @@
INCLUDEPATH += $$PWD INCLUDEPATH += $$PWD
HEADERS += \ HEADERS += \
$$PWD/chinesecharacterstopinyin.h \ # $$PWD/chinesecharacterstopinyin.h \
$$PWD/document.h \ $$PWD/document.h \
$$PWD/index-generator.h \ $$PWD/index-generator.h \
# $$PWD/inotify-manager.h \ # $$PWD/inotify-manager.h \
@ -12,7 +12,7 @@ HEADERS += \
$$PWD/text-content-indexer.h $$PWD/text-content-indexer.h
SOURCES += \ SOURCES += \
$$PWD/chinesecharacterstopinyin.cpp \ # $$PWD/chinesecharacterstopinyin.cpp \
$$PWD/document.cpp \ $$PWD/document.cpp \
$$PWD/index-generator.cpp \ $$PWD/index-generator.cpp \
# $$PWD/inotify-manager.cpp \ # $$PWD/inotify-manager.cpp \

View File

@ -45,6 +45,9 @@ void centerToScreen(QWidget* widget) {
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
//load chinese character and pinyin file to a Map
FileUtils::loadHanziTable("://index/pinyinWithoutTone.txt");
qRegisterMetaType<QVector<QStringList>>("QVector<QStringList>"); qRegisterMetaType<QVector<QStringList>>("QVector<QStringList>");
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

View File

@ -47,6 +47,7 @@ MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent) QMainWindow(parent)
{ {
/*-------------InotyifyRefact Test Start---------------*/ /*-------------InotyifyRefact Test Start---------------*/
QTime t1 = QTime::currentTime(); QTime t1 = QTime::currentTime();
InotifyManagerRefact* imr = new InotifyManagerRefact("/home"); InotifyManagerRefact* imr = new InotifyManagerRefact("/home");