C/C++ Qt实现文章小说人物关系分析
alicema1111 人气:0一、所需工具软件
1. Visual Stuido
2. C++
二、使用步骤
1.引入库
代码如下(示例):
#include "QtGuiApplication1.h" #include<qDebug> #include<QFile> #include <fstream> #include<iostream> using namespace std; #include <sstream> #include <unordered_map>
2.创建元素操作函数
代码如下(示例):
//读取人物名称 void QtGuiApplication1::readOnButton() { //打开关键词文件 ifstream ifs; ifs.open("name.txt", ios::in); if (!ifs.is_open()) { cout << "打开文件失败" << endl; return; } //一行一行读取到数组中 string buf; while (getline(ifs, buf)) { cout << buf << endl; QString name = QString::buf); gNameList << name; } ifs.close(); ui.textEdit->setPlainText(gNameList); } //出现次数 void QtGuiApplication1::timesOnButton() { qDebug() << QString::fromLocal8Bit("人名次数统计"); //统计归零 if (gNameList.size() == 0) { std::cout << "请倒入人名列表后再试!" << std::endl; } gNameCounter.clear(); for (auto name : gNameList) { std::string key = name.toLocal8Bit(); gNameCounter[key] = 0; } //一行行读入小说 ifstream ifs; ifs.open("天龙八部.txt", ios::in); if (!ifs.is_open()) { cout << "打开文件失败" << endl; return; } //清空统计用的TABLE的数据 gTable.clear(); //统计结果显示到界面 QStringList result; for (auto name : gNameList) { std::string keyword = name.toLocal8Bit(); QString tmp = name + ":" + QString::number(gNameCounter[keyword]); result << tmp; std::cout << keyword << " " << gNameCounter[keyword] << std::endl; } ui.textEdit_3->setText(result.join("\n")); } //篇幅跨度 void QtGuiApplication1::rangeOnButton() { ui.textEdit_3->clear(); for (auto name : gNameList) { std::string keyword = name.toLocal8Bit(); int firstLineNum = -1; int lastLineNum = -1; for (int i = 0; i < gTable.size(); i++) { auto& row = gTable.at(i); //std::cout << "行号:" << i << " "; int KeyWordCounts = row[keyword]; if (KeyWordCounts > 0) { if (firstLineNum == -1) { firstLineNum = i; } lastLineNum = i; } } ui.textEdit_3->append(QString::number(lastLineNum - firstLineNum)); } } //关系最紧密两人 void QtGuiApplication1::relationGoodOnButton() { ui.textEdit_3->clear(); qDebug() << "test"; //关系紧密算法,原理每个人名在第几行出现的行数集合和另一个人名行数集合求距离,取最小值为它的精密度 //值越小的那个为此人和另一个人的最精密度,然后同样的方法计算出此人与其它人的精密度,最终取得 //谁和这个人最紧密 std::map < std::string, std::vector<int>> DataContainer; //正在对比的两个选手 std::string player1, player2; int theMinDistance = 9999999999999999; for (auto name : gNameList) { std::string keyword = name.toLocal8Bit(); std::vector<int> rowNums; for (int i = 0; i < gTable.size(); i++) { auto& row = gTable.at(i); //std::cout << "行号:" << i << " "; int KeyWordCounts = row[keyword]; if (KeyWordCounts > 0) { rowNums.push_back(i); } } DataContainer[keyword] = rowNums; } ui.textEdit_3->append(QString::number(theMinDistance)+ QStringLiteral("行")); }
三、运行结果
加载全部内容