2021-01-29 11:43:07 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2020, KylinSoft Co., Ltd.
|
|
|
|
|
*
|
|
|
|
|
* 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 3 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, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
* Authors: zhangpengfei <zhangpengfei@kylinos.cn>
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-01-19 10:44:28 +08:00
|
|
|
|
#include "construct-document.h"
|
|
|
|
|
#include "file-utils.h"
|
2021-05-07 20:48:15 +08:00
|
|
|
|
#include "chinese-segmentation.h"
|
2021-01-19 10:44:28 +08:00
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QThread>
|
2021-01-27 18:22:50 +08:00
|
|
|
|
#include <QUrl>
|
2021-01-19 10:44:28 +08:00
|
|
|
|
|
|
|
|
|
//extern QList<Document> *_doc_list_path;
|
|
|
|
|
//extern QMutex _mutex_doc_list_path;
|
2021-04-30 16:28:50 +08:00
|
|
|
|
using namespace Zeeker;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
ConstructDocumentForPath::ConstructDocumentForPath(QVector<QString> list) {
|
2021-01-19 10:44:28 +08:00
|
|
|
|
this->setAutoDelete(true);
|
|
|
|
|
m_list = std::move(list);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void ConstructDocumentForPath::run() {
|
2021-01-19 10:44:28 +08:00
|
|
|
|
// qDebug()<<"ConstructDocumentForPath";
|
2021-06-10 20:43:57 +08:00
|
|
|
|
// if(!Zeeker::_doc_list_path)
|
|
|
|
|
// Zeeker::_doc_list_path = new QVector<Document>;
|
2021-01-19 10:44:28 +08:00
|
|
|
|
// qDebug()<<_doc_list_path->size();
|
2021-03-01 09:07:33 +08:00
|
|
|
|
QString index_text = m_list.at(0).toLower();
|
2021-01-19 10:44:28 +08:00
|
|
|
|
QString sourcePath = m_list.at(1);
|
2021-02-02 16:04:14 +08:00
|
|
|
|
Document doc;
|
2021-01-19 10:44:28 +08:00
|
|
|
|
|
|
|
|
|
//多音字版
|
|
|
|
|
//现加入首字母
|
2021-04-26 15:06:47 +08:00
|
|
|
|
QStringList pinyin_text_list = FileUtils::findMultiToneWords(QString(m_list.at(0)).replace(".", ""));
|
2021-02-20 16:56:30 +08:00
|
|
|
|
// if(!pinyin_text_list.isEmpty())
|
|
|
|
|
// {
|
|
|
|
|
// for (QString& i : pinyin_text_list){
|
|
|
|
|
// i.replace("", " ");
|
|
|
|
|
// i = i.simplified();
|
|
|
|
|
// }
|
|
|
|
|
// doc.setIndexText(pinyin_text_list);
|
|
|
|
|
// }
|
2021-01-19 10:44:28 +08:00
|
|
|
|
|
|
|
|
|
QString uniqueterm = QString::fromStdString(FileUtils::makeDocUterm(sourcePath));
|
2021-04-26 15:06:47 +08:00
|
|
|
|
QString upTerm = QString::fromStdString(FileUtils::makeDocUterm(sourcePath.section("/", 0, -2, QString::SectionIncludeLeadingSep)));
|
2021-02-02 14:05:32 +08:00
|
|
|
|
// qDebug()<<"sourcePath"<<sourcePath.section("/",0,-2,QString::SectionIncludeLeadingSep);
|
|
|
|
|
// qDebug() << "sourcePath ---------------------------: " << sourcePath;
|
|
|
|
|
// qDebug() << "sourcePath.section -------------------: " << sourcePath.section("/",0,-2,QString::SectionIncludeLeadingSep);
|
|
|
|
|
// qDebug() << "ConstructDocumentForPath -- uniqueterm: " << uniqueterm;
|
|
|
|
|
// qDebug() << "ConstructDocumentForPath -- upTerm: " << upTerm;
|
2021-01-19 10:44:28 +08:00
|
|
|
|
|
2021-02-02 16:04:14 +08:00
|
|
|
|
|
2021-01-19 10:44:28 +08:00
|
|
|
|
doc.setData(sourcePath);
|
|
|
|
|
doc.setUniqueTerm(uniqueterm);
|
|
|
|
|
doc.addTerm(upTerm);
|
|
|
|
|
doc.addValue(m_list.at(2));
|
2021-04-26 15:06:47 +08:00
|
|
|
|
/* QStringList temp;
|
|
|
|
|
// temp.append(index_text);
|
|
|
|
|
temp.append(pinyin_text_list)*/;
|
2021-01-27 18:22:50 +08:00
|
|
|
|
int postingCount = 0;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
while(postingCount < index_text.size()) {
|
2021-02-20 16:56:30 +08:00
|
|
|
|
// QVector<size_t> p;
|
|
|
|
|
// p.append(postingCount);
|
2021-04-26 15:06:47 +08:00
|
|
|
|
doc.addPosting(QUrl::toPercentEncoding(index_text.at(postingCount)).toStdString(), postingCount);
|
2021-01-27 18:22:50 +08:00
|
|
|
|
++postingCount;
|
|
|
|
|
}
|
2021-02-20 16:56:30 +08:00
|
|
|
|
int i = 0;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
for(QString& s : pinyin_text_list) {
|
2021-02-20 16:56:30 +08:00
|
|
|
|
i = 0;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
while(i < s.size()) {
|
|
|
|
|
doc.addPosting(QUrl::toPercentEncoding(s.at(i)).toStdString(), postingCount);
|
2021-02-20 16:56:30 +08:00
|
|
|
|
++postingCount;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-27 18:22:50 +08:00
|
|
|
|
|
2021-01-19 10:44:28 +08:00
|
|
|
|
// QMetaObject::invokeMethod(m_indexGenerator,"appendDocListPath",Q_ARG(Document,doc));
|
2021-06-10 20:43:57 +08:00
|
|
|
|
IndexGenerator::_mutex_doc_list_path.lock();
|
|
|
|
|
IndexGenerator::_doc_list_path.append(doc);
|
|
|
|
|
IndexGenerator::_mutex_doc_list_path.unlock();
|
2021-01-19 10:44:28 +08:00
|
|
|
|
// qDebug()<<"ConstructDocumentForPath finish";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
ConstructDocumentForContent::ConstructDocumentForContent(QString path) {
|
2021-01-19 10:44:28 +08:00
|
|
|
|
this->setAutoDelete(true);
|
|
|
|
|
m_path = std::move(path);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void ConstructDocumentForContent::run() {
|
2021-01-19 20:59:46 +08:00
|
|
|
|
// qDebug() << "ConstructDocumentForContent currentThreadId()" << QThread::currentThreadId();
|
2021-01-19 10:44:28 +08:00
|
|
|
|
// 构造文本索引的document
|
2021-06-10 20:43:57 +08:00
|
|
|
|
// if(!Zeeker::_doc_list_content)
|
|
|
|
|
// Zeeker::_doc_list_content = new QVector<Document>;
|
2021-01-19 10:44:28 +08:00
|
|
|
|
QString content;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
FileReader::getTextContent(m_path, content);
|
2021-03-02 15:19:10 +08:00
|
|
|
|
if(content.isEmpty())
|
|
|
|
|
return;
|
2021-06-23 15:50:19 +08:00
|
|
|
|
//QString uniqueterm = QString::fromStdString(FileUtils::makeDocUterm(m_path));
|
|
|
|
|
//QString upTerm = QString::fromStdString(FileUtils::makeDocUterm(m_path.section("/", 0, -2, QString::SectionIncludeLeadingSep)));
|
2021-01-19 10:44:28 +08:00
|
|
|
|
Document doc;
|
|
|
|
|
doc.setData(content);
|
2021-06-23 15:50:19 +08:00
|
|
|
|
//doc.setUniqueTerm(uniqueterm);
|
|
|
|
|
doc.setUniqueTerm(FileUtils::makeDocUterm(m_path));
|
|
|
|
|
//doc.addTerm(upTerm);
|
|
|
|
|
doc.addTerm(FileUtils::makeDocUterm(m_path.section("/", 0, -2, QString::SectionIncludeLeadingSep)));
|
2021-01-19 10:44:28 +08:00
|
|
|
|
doc.addValue(m_path);
|
2021-05-28 15:55:26 +08:00
|
|
|
|
|
2021-05-31 14:56:59 +08:00
|
|
|
|
//'\xEF\xBC\x8C' is "," "\xE3\x80\x82" is "。" use three " " to replace ,to ensure the offset info.
|
|
|
|
|
content = content.replace("\t", " ").replace("\xEF\xBC\x8C", " ").replace("\xE3\x80\x82", " ");
|
2021-05-28 15:55:26 +08:00
|
|
|
|
|
|
|
|
|
// QVector<SKeyWord> term = ChineseSegmentation::getInstance()->callSegement(content.left(20480000));
|
2021-06-07 15:37:06 +08:00
|
|
|
|
std::vector<cppjieba::KeyWord> term = ChineseSegmentation::getInstance()->callSegementStd(content.left(20480000).toStdString());
|
2021-05-28 15:55:26 +08:00
|
|
|
|
|
2021-05-22 09:18:35 +08:00
|
|
|
|
for(size_t i = 0; i < term.size(); ++i) {
|
2021-04-26 15:06:47 +08:00
|
|
|
|
doc.addPosting(term.at(i).word, term.at(i).offsets, static_cast<int>(term.at(i).weight));
|
2021-01-19 10:44:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:43:57 +08:00
|
|
|
|
IndexGenerator::_mutex_doc_list_content.lock();
|
|
|
|
|
IndexGenerator::_doc_list_content.append(doc);
|
|
|
|
|
IndexGenerator::_mutex_doc_list_content.unlock();
|
2021-01-19 10:44:28 +08:00
|
|
|
|
content.clear();
|
2021-03-16 17:21:10 +08:00
|
|
|
|
content.squeeze();
|
2021-06-23 15:50:19 +08:00
|
|
|
|
|
2021-01-19 10:44:28 +08:00
|
|
|
|
term.clear();
|
2021-06-10 20:43:57 +08:00
|
|
|
|
term.shrink_to_fit();
|
2021-01-19 10:44:28 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|