add chinesecharactertopinyin to file-ulils

This commit is contained in:
zhangzihao 2020-12-26 12:45:28 +08:00
parent f9963f178e
commit b4d3ee033d
8 changed files with 64 additions and 8 deletions

View File

@ -2,6 +2,8 @@
#include <QDebug>
#include <QFile>
#include <QUrl>
#include <QMap>
QMap<QString, QStringList> FileUtils::map_chinese2pinyin = QMap<QString, QStringList>();
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<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 <QCryptographicHash>
#include <QIcon>
#include <QMap>
class FileUtils
{
@ -17,6 +21,13 @@ public:
static QString getFileName(const QString &);
static QString getAppName(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:
FileUtils();
};

View File

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

View File

@ -12,8 +12,8 @@ public:
explicit chineseCharactersToPinyin(QObject *parent = nullptr);
static QString find(const QString &hanzi)
{
// static QMap<QString, QStringList> map = loadHanziTable("://index/pinyinWithoutTone.txt");
static QMap<QString, QStringList> map;
static QMap<QString, QStringList> map = loadHanziTable("://index/pinyinWithoutTone.txt");
// static QMap<QString, QStringList> map;
QString output;
QStringList stringList = hanzi.split("");
@ -30,6 +30,7 @@ public:
Q_SIGNALS:
private:
static QMap<QString, QStringList> map;
/* 加载汉字对照表 */
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);
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<QString> &list)
doc.setData(sourcePath);
doc.setUniqueTerm(uniqueterm);
doc.addValue(list.at(2));
// doc.setIndexText(QStringList()<<index_text<<pinyin_text);
doc.setIndexText(QStringList()<<index_text<<pinyin_text);
doc.setIndexText(QStringList()<<index_text);
return doc;

View File

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

View File

@ -45,6 +45,9 @@ void centerToScreen(QWidget* widget) {
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>");
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

View File

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