#include"stdafx.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"test.h"
#define MAX_LINE 1024
bool check_keyword(char* str,int inlen)
{
bool bRet = false;
for(int i=0; i<32;i++)
{
char *ret = strstr(str, c_keyword[i]);
if (ret != NULL)
{
int outlen = strlen(c_keyword[i]);
if(inlen == outlen)
{
bRet = true;
}
}
}
return bRet;
}
void mgr_keyword(char* str,int length)
{
char ch;
char word[30];
int j = 0;
for(int i=0; i<length;i++)
{
ch=str[i];
if(ch!=' '&&ch!=','&&ch!='.'&&ch!='\n'&&ch!='?'&&ch!='!')
{
word[j]=ch;
j++;
}
else if(ch==' '||ch=='\n'||ch=='\0')
{
if(check_keyword(word,j))
{
word[j]='\0';
if(j>2)
printf(" 关键字: %s\n", word);
memset(word,0,sizeof(word));
j=0;
}
else
{
memset(word,0,sizeof(word));
j=0;
}
}
else if(ch==','||ch=='.'||ch=='?'||ch=='!')
{
if(check_keyword(word,j))
{
word[j]=ch;
word[++j]='\0';
if(j>2)
printf(" 关键字带标点: %s\n", word);
memset(word,0,sizeof(word));
j=0;
}
else
{
memset(word,0,sizeof(word));
j=0;
}
}
}
}
int main()
{
char buf[MAX_LINE] = {0};
char str[MAX_LINE] = {0};
FILE *fp;
int i, j = 0;
if((fp = fopen("C:\\gt.txt","r")) == NULL)
{
perror("fail to read");
exit (1) ;
}
while(fgets(buf,MAX_LINE,fp) != NULL)
{
for (i = 0; i<sizeof(buf); i++)
{
if(buf[i]=='\n')
{
buf[i]=' ';
}
}
for (i = 0; i<sizeof(buf); i++)
{
if(buf[i]=='\0')
{//没有标点符号的一行,合并到下一行一起输出。
break;
}
if ((buf[i] == ',')||(buf[i] == '.')||(buf[i] == '?')||(buf[i] == '!'))
{
if((buf[i] == '.'))
{//assignment1.txt
if(buf[i+1]=='t'&& buf[i+2]=='x'&& buf[i+3]=='t')
{
str[j] = buf[i];
j++;
continue;
}
}
str[j] = buf[i];
int l=0;
//句子前的空格不输出
for(int k=0;i<sizeof(str); k++)
{
if(str[k]==' ')
{
l++;
}
else
{
break;
}
}
printf("句子: %s\n", str+l);
mgr_keyword((str+l),strlen(str+l));
memset(str,0,sizeof(str));
j = 0;
}
else
{
str[j] = buf[i];
j++;
}
}
memset(buf,0,sizeof(buf));
}
system("pause");
return 0;
}