ukui-search/libsearch/index/file-content-indexer.cpp

67 lines
2.1 KiB
C++
Raw Normal View History

2022-10-26 18:01:40 +08:00
/*
* Copyright (C) 2022, 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: iaom <zhangpengfei@kylinos.cn>
*
*/
#include "file-content-indexer.h"
#include <QDateTime>
2022-10-26 18:01:40 +08:00
#include "file-reader.h"
#include "file-utils.h"
#include "chinese-segmentation.h"
using namespace UkuiSearch;
fileContentIndexer::fileContentIndexer(const QString &filePath): m_filePath(filePath)
{
}
bool fileContentIndexer::index()
{
QString content;
QFileInfo info(m_filePath);
2022-12-23 11:11:06 +08:00
if(!info.exists()) {
return false;
}
2022-10-26 18:01:40 +08:00
QString suffix = info.suffix();
FileReader::getTextContent(m_filePath, content, suffix);
if(content.isEmpty()) {
return false;
}
m_document.setData(content);
2023-03-20 15:21:58 +08:00
std::vector<KeyWord> term = ChineseSegmentation::getInstance()->callSegment(content);
2022-10-26 18:01:40 +08:00
content.clear();
content.squeeze();
for(size_t i = 0; i < term.size(); ++i) {
m_document.addPosting(term.at(i).word, term.at(i).offsets, static_cast<int>(term.at(i).weight));
}
term.clear();
term.shrink_to_fit();
m_document.setUniqueTerm(FileUtils::makeDocUterm(m_filePath));
m_document.addTerm("PARENTTERM" + FileUtils::makeDocUterm(m_filePath.section("/", 0, -2, QString::SectionIncludeLeadingSep)));
2022-10-26 18:01:40 +08:00
m_document.addValue(1, m_filePath);
m_document.addValue(2, suffix);
QString time = info.lastModified().toString("yyyyMMddHHmmsszzz");
m_document.addSortableSerialiseValue(3, time);
m_document.setIndexTime(time);
2022-10-26 18:01:40 +08:00
return true;
}