TF-IDF算法确定阅读主题词解答英语阅读Title题目
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <windows.h>
using namespace std;
#define N 1517 //文献数目
#define textN 6 //题目数目
#define ERROR 1
#define OK 0
const int WORD_LENGTH = 30;//定义单个单词最大长度
char temp[WORD_LENGTH];//定义用以临时存放单词的数组
typedef struct Node {
char word[WORD_LENGTH] = { '\0' };
int time = 0;
int textnum = 0;
double weight = 0;
}wordNode, wordLink;
char Libword[900][WORD_LENGTH] = { 0 }; //900条停用词库
int wordleng = 0; //词库中实际词条数目
wordNode sumWord[1000];//文章词表
int wordNum = 0;//文章中的非重单词数
int sumWordNum = 0;//文章总词数
double score[4] = { 0 };//选项分数
int DoLibStop(char *name, char memory[][WORD_LENGTH])
{
FILE *cp = fopen(name, "r");//词库位置
char ch;
while (!feof(cp)) //读取词库
{
ch = fgetc(cp);
for (int i = 0; ch != 13 && i<22 && ch != 10; i++)//回车区分词
{
Libword[wordleng][i] = ch;
ch = fgetc(cp);
}
// std::cout<<(word[wordleng]); //屏幕输出。临时
wordleng++;
}
fclose(cp); //关闭停用词库
return wordleng;
}
void wordDelSpe(char word[]) //去掉特殊字符
{
int i, k, j;
char *specialChar = ",.;:'“”?!><+=|*&^%$#@\"[](){}0123456789";//定义特殊字符集
for (i = 0; i<strlen(word); i++)
{
//筛选并去除字符串中的特殊字符
for (k = 0; k<strlen(specialChar); k++)
{
if (word[i] == specialChar[k])
{
j = i;
while (j<strlen(word))
{
word[j] = word[j + 1];
j++;
}
i--;
break;
}
}
}
}
bool wordCmpStop(char *word)//将人称代词及其他常用词去掉
{
int simNum = wordleng;
for (int i = 0; i<strlen(word); i++)//筛选并将字符串中的大写字母转化为小写字母
if (word[i] >= 'A'&& word[i] <= 'Z')
word[i] += 32;
for (int i = 0; i<simNum; i++)
if (strcmp(word, Libword[i]) == 0)
return true;
return false;
}
void wordSearch(char *word, int &wordnum) {
int i = 0;
while (i < wordnum && (strcmp(sumWord[i].word, word) != 0))
{
i++;
}
if (i < wordnum)
sumWord[i].time++;
if (i == wordnum)
{
strcpy(sumWord[i].word, word);
wordnum++;
sumWord[i].time = 1;
}
sumWordNum += 1;
}
void doArticle(char *file0)
{
FILE *file;
if ((file = fopen(file0, "r")) == NULL) {
//这里是绝对路径,基于XCode编译器查找方便的需求
printf("%s文件读取失败!", file0);
system("pause");
exit(1);
}
while ((fscanf(file, "%s", temp)) != EOF)
{
if (temp[0] == '*')//遇到题目了
break;
wordDelSpe(temp);
if (wordCmpStop(temp) == true)
{
sumWordNum += 1;
continue;
}
wordSearch(temp, wordNum);
}
fclose(file);//关闭文件
}
void copyNode(wordNode& node1, wordNode &node2)//node2复制到node1
{
strcpy(node1.word, node2.word);
node1.time = node2.time;
node1.textnum = node2.textnum;
node1.weight = node2.weight;
}
void sortWord()//直接插入排序
{
wordNode t;
int i, j;
/*cout << wordNum << endl;*/
for (i = 1; i < wordNum; i++)
{
copyNode(t, sumWord[i]);
for (j = i - 1; j >= 0 && sumWord[j].weight<t.weight; j--)
{
copyNode(sumWord[j + 1], sumWord[j]);
}
copyNode(sumWord[j + 1], t);
}
}
void fileCount(char file[N][50])
{
int i, j;
FILE *f;
for (i = 0; i <N; i++)
{
f = fopen(file[i], "r");
if (!f)
{
printf("%s文件读取失败!", file[i]);
/*system("pause");
exit(1);*/
continue;
}
while ((fscanf(f, "%s", temp)) != EOF)
{
wordDelSpe(temp);
j = 0;
while (j < wordNum && (strcmp(sumWord[j].word, temp) != 0))
{
j++;
}
if (j < wordNum)
sumWord[j].textnum++;//文章数++
}
fclose(f);//关闭文件
}
}
void calWeight(wordNode *sumWord, int wordNum)
{
int i;
for (i = 0; i < wordNum; i++)
sumWord[i].weight = (sumWord[i].time * 1.0 / sumWordNum)*log((N*1.0) / (sumWord[i].textnum + 1));
}
int numWei(int n)
{
if (n / 10 == 0)
return 1;
else if (n / 100 == 0)
return 2;
else if (n / 1000 == 0)
return 3;
else
return 4;
}
void fileNameMake(char file[][50], int n)
{
int i = 0, j = 0, i1, num;
/*strcpy(file[0], "txt\\txt1.txt");
strcpy(file[1], "txt\\txt2.txt");
strcpy(file[2], "txt\\txt3.txt");
strcpy(file[3], "txt\\txt4.txt");
strcpy(file[4], "txt\\txt5.txt");*/
for (i = 0; i < n; i++)
{
strcpy(file[i], "fileLib\\\\txt");
num = numWei(i + 1);
//cout << num << endl;
i1 = i + 1;
for (j = num - 1; j >= 0; j--)
{
*(file[i] + 12 + j) = i1 % 10 + 48;
i1 = i1 / 10;
}
strcpy(file[i] + 12 + num, ".txt");
/*cout << file[i] << endl;*/
/*if (strcmp(file[i] , "txt\\\\txt00.txt")==0)
cout << i << endl;*/
}
}
void saveNameMake(char savefile[][50], int n)
{
int i = 0, j = 0, i1, num;
for (i = 0; i < n; i++)
{
strcpy(savefile[i], "savefile\\\\save");
num = numWei(i + 1);
//cout << num << endl;
i1 = i + 1;
for (j = num - 1; j >= 0; j--)
{
*(savefile[i] + 14 + j) = i1 % 10 + 48;
i1 = i1 / 10;
}
strcpy(savefile[i] + 14 + num, ".txt");
//cout << savefile[i] << endl;
/*if (strcmp(file[i] , "txt\\\\txt00.txt")==0)
cout << i << endl;*/
}
}
void textNameMake(char textfile[][50], int n)
{
int i = 0, j = 0, i1, num;
for (i = 0; i < n; i++)
{
strcpy(textfile[i], "textfile\\\\text");
num = numWei(i + 1);
//cout << num << endl;
i1 = i + 1;
for (j = num - 1; j >= 0; j--)
{
*(textfile[i] + 14 + j) = i1 % 10 + 48;
i1 = i1 / 10;
}
strcpy(textfile[i] + 14 + num, ".txt");
//cout << textfile[i] << endl;
/*if (strcmp(file[i] , "txt\\\\txt00.txt")==0)
cout << i << endl;*/
}
}
void scoreArticle(char *file0,int k,char *answers, int &answerNum)
{
FILE *file;
int i;
if ((file = fopen(file0, "r")) == NULL) {
//这里是绝对路径,基于XCode编译器查找方便的需求
printf("%s文件读取失败!", file0);
system("pause");
exit(1);
}
int count = -1;
while ((fscanf(file, "%s", temp)) != EOF&&temp[0] != '*') {}//※号提示题目
while ((fscanf(file, "%s", temp)) != EOF)
{
if (temp[0] == '*')
break;
if (!strcmp(temp, "A.") || !strcmp(temp, "B.") || !strcmp(temp, "C.") || !strcmp(temp, "D."))
{
count++;
continue;
}
wordDelSpe(temp);
for (i = 0; i<strlen(temp); i++)//筛选并将字符串中的大写字母转化为小写字母
if (temp[i] >= 'A'&& temp[i] <= 'Z')
temp[i] += 32;
for (i = 0; i < wordNum; i++)
{
if (!strcmp(temp, sumWord[i].word))
score[count] += sumWord[i].weight;
}
}
fscanf(file, "%s", temp);
/*cout << temp[1] << "###"<<endl;*/
answers[k] = temp[1];
answerNum++;
fclose(file);//关闭文件
}
void doArticleLocal(char *file0)
{
FILE *file;
int i;
if ((file = fopen(file0, "r")) == NULL) {
//这里是绝对路径,基于XCode编译器查找方便的需求
printf("%s文件读取失败!",file0);
system("pause");
exit(1);
}
while ((fscanf(file, "%s", temp)) != EOF)
{
if (temp[strlen(temp) - 1] == '#')
{
/*cout << "遇到了#" << endl;*/
break;
}
wordDelSpe(temp);
for (i = 0; i<strlen(temp); i++)//筛选并将字符串中的大写字母转化为小写字母
if (temp[i] >= 'A'&& temp[i] <= 'Z')
temp[i] += 32;
for (i = 0; i < wordNum; i++)
{
if (!strcmp(temp, sumWord[i].word))
{
sumWord[i].weight *= 1.5;
/*cout << "改了" << endl;*/
}
}
}
while ((fscanf(file, "%s", temp)) != EOF&&temp[0] != '#') {}//再次遇到#号,最后一段
while ((fscanf(file, "%s", temp)) != EOF)
{
if (temp[0] == '*')
{
/*cout << "遇到了*" << endl;*/
break;
}
wordDelSpe(temp);
for (i = 0; i<strlen(temp); i++)//筛选并将字符串中的大写字母转化为小写字母
if (temp[i] >= 'A'&& temp[i] <= 'Z')
temp[i] += 32;
for (i = 0; i < wordNum; i++)
{
if (!strcmp(temp, sumWord[i].word))
sumWord[i].weight *= 1.5;
}
}
fclose(file);//关闭文件
}
void doArticleAll(char *file0,char file[][50],char *savefile,int id,char *answers,int &answerNum,int &correctNum)
{
/*cout << "correctNum" << correctNum << endl;*/
int i;
char ans; //答案
doArticle(file0); //处理题目文本
fileCount(file); //统计文件库中单词出现次数
calWeight(sumWord, wordNum); //计算权重
sortWord(); //排序
FILE *p = fopen(savefile, "w"); //文本输出
fprintf(p, " word \t词频\t文章数\t权重\n"); //输出到文档结果
fprintf(p, "本文共%d个词,%d个不重复词\n", sumWordNum, wordNum);
for (i = 0; i < wordNum; i++)
{
fprintf(p, "%-16s\t%d\t%d\t%f\n", sumWord[i].word, sumWord[i].time, sumWord[i].textnum, sumWord[i].weight);
}
fclose(p);
doArticleLocal(file0);//根据位置调整权值
scoreArticle(file0,id,answers,answerNum);
std::cout << "第"<<id+1<<"题结果成功输出到文件:" << savefile << endl;
std::cout << "成功得到结果:" << endl;
for (int h = 0; h < 4; h++)
{
cout << score[h] << endl;
}
int max = 0;
for (int k = 1; k < 4; k++)
{
if (score[k] > score[max])
max = k;
}
ans = 65 + max;
cout << "答案是:" << ans ;
if (answers[id] == ans)
{
cout <<" 正确"<<endl;
correctNum++;
}
else
cout << " 错误 (正确答案为:" << answers[id]<<")" << endl ;
cout << endl << "-----------------------------" << endl;
/*cout << "correctNum" << correctNum << endl;*/
}
void main(int n, char *arg[])
{
char answers[textN] = { '\0' };
int answerNum = 0,correctNum = 0, i = 0, j = 0;
double corretPersent; //正确率
char textfile[textN][50] = { '\0' }; //题目名字
char savefile[textN][50] = { '\0' }; //保存文件
char file[N][50] = { '\0' }; //需要检索的文献
//char *savefile1 = "savefile\\save.txt"; //结果存放文档
char *LibStop = "stopLib\\stop.txt"; //停用词库
//char *file0 = "textfile\\text1.txt";
//char *file0 = "text2.txt";
clock_t start0, finish0; //程序运行时间
double sftime0;
start0 = clock();
fileNameMake(file, N);
textNameMake(textfile, textN);
saveNameMake(savefile, textN);
cout << endl;
wordleng = DoLibStop(LibStop, Libword);//停用词处理
/*cout << file0 << "hah" << endl;*/
answerNum = 0;
correctNum = 0;
for(int k=0;k<textN;k++)
doArticleAll(textfile[k], file, savefile[k],k,answers,answerNum,correctNum);
/*cout << "correctNum" << correctNum << endl;
cout << "answerNum" << answerNum << endl;*/
corretPersent = correctNum*100.0 / textN;
printf("共%d篇文章,正确率为%.2f%%\n", textN, corretPersent);
finish0 = clock();
sftime0 = (double)(finish0 - start0) / CLOCKS_PER_SEC;//计算用时
std::cout << endl<< "共用时间:" << sftime0 << "秒." << endl;
system("pause");
}
TF-IDF算法确定阅读主题词解答英语阅读Title题目的更多相关文章
- 55.TF/IDF算法
主要知识点: TF/IDF算法介绍 查看es计算_source的过程及各词条的分数 查看一个document是如何被匹配到的 一.算法介绍 relevance score算法,简单来说 ...
- tf–idf算法解释及其python代码实现(下)
tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...
- tf–idf算法解释及其python代码实现(上)
tf–idf算法解释 tf–idf, 是term frequency–inverse document frequency的缩写,它通常用来衡量一个词对在一个语料库中对它所在的文档有多重要,常用在信息 ...
- Elasticsearch由浅入深(十)搜索引擎:相关度评分 TF&IDF算法、doc value正排索引、解密query、fetch phrase原理、Bouncing Results问题、基于scoll技术滚动搜索大量数据
相关度评分 TF&IDF算法 Elasticsearch的相关度评分(relevance score)算法采用的是term frequency/inverse document frequen ...
- tf–idf算法解释及其python代码
tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...
- 25.TF&IDF算法以及向量空间模型算法
主要知识点: boolean model IF/IDF vector space model 一.boolean model 在es做各种搜索进行打分排序时,会先用boolean mo ...
- Elasticsearch学习之相关度评分TF&IDF
relevance score算法,简单来说,就是计算出,一个索引中的文本,与搜索文本,他们之间的关联匹配程度 Elasticsearch使用的是 term frequency/inverse doc ...
- 基于TF/IDF的聚类算法原理
一.TF/IDF描述单个term与特定document的相关性TF(Term Frequency): 表示一个term与某个document的相关性. 公式为这个term在document中出 ...
- 信息检索中的TF/IDF概念与算法的解释
https://blog.csdn.net/class_brick/article/details/79135909 概念 TF-IDF(term frequency–inverse document ...
随机推荐
- WebService初学
作为一个初学者,在遇到新的知识点的时候,搞清这个知识点的名称含义,是有必要的.那什么是WebService呢? 顾名思义,它是一个运行在web上的服务.这个服务通过网络为我们的程序提供服务方法.类似一 ...
- XML基础
什么是XML? XML(Extensible Markup Language)可扩展标记语言,是一种以简单文本格式存储数据的方式. 它最重要的组成部分是XML元素,包含了文档的实际数据. XML的几个 ...
- Python - 类与对象的方法
类与对象的方法
- MySQL 锁问题
一.MySQL中不同的存储引擎支持不同的锁机制 (A) MyISAM 和 MEMORY 支持表级锁 (B) BDB 支持页面锁,也支持表级锁 (C) InnoDB 支持行级锁,也支持表级锁,默认是行级 ...
- 第三篇 基于.net搭建热插拔式web框架(重造Controller)
由于.net MVC 的controller 依赖于HttpContext,而我们在上一篇中的沙箱模式已经把一次http请求转换为反射调用,并且http上下文不支持跨域,所以我们要重造一个contro ...
- sql 取新的列名含义
SELECT a.*, 1 DELETABLE, '' YEAR_ON, '' MONTH_ON, TOOL_STATUS status0 FROM TOOL a 说明:其中tool字段有tool_s ...
- java常用工具
/** * 将字节数组转换成字符串 * @param array 字节数组 * @return String */ public static String byte2str(byte[] array ...
- Javascript模块化编程(一):模块的写法
Javascript模块化编程(一):模块的写法 作者: 阮一峰 原文链接:http://www.ruanyifeng.com/blog/2012/10/javascript_module.html ...
- Linux文件与目录管理
. 代表此层目录 . . 代表上一层目录 - 代表前一个工作目录 ~ 代表"目前用户身份"所在的中文件夹 ~account 代表accoun ...
- android 移动网络实时抓包
2G.3G环境,那就必须root进去tcpdump 方式抓. 准备: 一.root CF-auto-root: http://autoroot.chainfire.eu/ 需要清理全部数据,注意备份 ...