// win_cDemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "tinyxml.h"
#include "NLPIR.h"
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include <fstream>
#include <iostream>
#include <string>
#include <hash_map>
#include <WinSock.h>
#include <mysql.h>
using namespace std;
#pragma comment(lib,"tinyxml.lib")
#pragma comment(lib, "NLPIR.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libmysql.lib")
//存放字典数据的hashMap
hash_map<string,float> hmDic;
//计算运行时间的变量
DWORD start_time,end_time;
//mysql文件
MYSQL mysqlData;
string user,passwd;
void processDictionary(string filename,float weight)
{
ifstream fin(filename.c_str());
if( !fin )
{
cout << "Error opening " << filename << " for input" << endl;
return;
}
string buf;
while(fin>>buf)
{
hmDic.insert(make_pair(buf,weight));
}
cout<<"Process Dictionary "<<filename<<" Success"<<endl;
}
void processConfigure()
{
//获取XML对象
TiXmlDocument doc;
//装载文件
doc.LoadFile("configure.xml");
//获取dics
TiXmlElement *dicsLevel = (doc.RootElement())->FirstChildElement();
//获取dic
TiXmlElement *dicLevel=dicsLevel->FirstChildElement();
TiXmlElement *pathLevel,*weightLevel;
start_time = GetTickCount64();//获取开始时间
cout<<"Process Begin, Please Wait Patient"<<endl;
while (dicLevel != NULL)//
{
pathLevel = dicLevel->FirstChildElement();//获取path
weightLevel = pathLevel->NextSiblingElement();//获取weight
//cout<<pathLevel->GetText()<<":"<<weightLevel->GetText()<<endl;
//处理文本
string filename =pathLevel->GetText();
float weight = atof(weightLevel->GetText());
//cout<<filename<<"|"<<weight+1<<endl;
processDictionary(filename,weight);
dicLevel=dicLevel->NextSiblingElement();
}
end_time = GetTickCount64();//获取结束时间
cout<<"Process All Dictionary Use "<<end_time-start_time<<" ms"<<endl;
//获取mysql
TiXmlElement *mysqlLevel = dicsLevel->NextSiblingElement();
TiXmlElement *userLevel,*passwdLevel;
userLevel = mysqlLevel->FirstChildElement();//获取user
passwdLevel = userLevel->NextSiblingElement();//获取passwd
user = userLevel->GetText();
passwd = passwdLevel->GetText();
}
int sentenceAnalysis(string sSentence)
{
try
{
const result_t *pVecResult;
int nCount;
//对句子进行分词
pVecResult=NLPIR_ParagraphProcessA(sSentence.c_str(),&nCount);
float totalNum = 0;//句子总的情感值
float tmpNum = 0;//每个字句的情感值
float tmpD = 1;//用来存放临时副词的变量
bool isTmpDSet = 0;//是否有副词被设置
for (int i=0;i<nCount;i++)
{
string ciXin = pVecResult[i].sPOS;//词性
//不存在词性则跳过
if(ciXin.empty())
{
continue;
}
//词性中包含'w'(即标点符号),表示当前的一个子句已经处理完毕
if(ciXin[0]=='w')
{
totalNum += tmpNum;
tmpNum = 0;
continue;
}
//如果不是形容词a,副词d,动词v,名词n,数词m,则不处理
if(!(ciXin=="a"||ciXin=="d"||ciXin=="v"||ciXin=="n"||ciXin=="m"))
continue;
string tmp = sSentence.substr(pVecResult[i].start,pVecResult[i].length);
auto itr=hmDic.find(tmp);
if(itr!=hmDic.end())
{
if(ciXin=="d")//如果副词后面接副词,进行多重复合
{
tmpD *= itr->second;
isTmpDSet = 1;
}
else if(isTmpDSet)//如果之前存在副词
{
tmpNum += tmpD * itr->second;
isTmpDSet = 0;
}
else
{
tmpNum += itr->second;//不存在副词的影响,直接加减
}
}
}
return totalNum;
}
catch(...)
{
//对出错的情况进行默认处理
return 0;
}
}
void initMysql()
{
try
{
mysql_init(&mysqlData);
// localhost:服务器 root为账号,123456为密码 test为数据库名 3306为端口
if(!mysql_real_connect(&mysqlData, "localhost","root","","fooddata",3306,NULL,0))
{
cout<<"database connect fail"<<endl;
exit(1);
}
else
cout<<"database connect success"<<endl;
}
catch (...)
{
exit(1);
}
}
int main()
{
//初始化分词器
if(!NLPIR_Init())
{
printf("Init fails\n");
return -1;
}
//首先读取配置文件,建立字典数据
processConfigure();
//初始化mysql数据库
initMysql();
//存储每一行数据
string text;
//查询所有结果
string sqlstr = "SELECT id,text,kind FROM comment";
MYSQL_RES *result = NULL;
if (0 == mysql_query(&mysqlData, sqlstr.c_str()))
{
//一次性取得数据集
result = mysql_store_result(&mysqlData);
//获取每一行
MYSQL_ROW row = NULL;
row = mysql_fetch_row(result);
start_time = GetTickCount64();
while (NULL != row) {
text = row[1];
//对每个句子进行分词
int answer = sentenceAnalysis(text);
if(answer>0)
cout<<"正面评价"<<endl;
else if(answer<0)
cout<<"负面评价"<<endl;
else
cout<<"中性评价"<<endl;
row = mysql_fetch_row(result);
}
end_time = GetTickCount64();
cout<<"Text all rows use "<<end_time-start_time<<" ms"<<endl;
}
//关闭数据库
// mysql_close(&mysqlData);
//释放分词器资源
NLPIR_Exit();
return 0;
}
求大神看看这个怎么修改???