一个检索系统,在归并拉链并得到摘要数据以后,必不可少的环节是飘红截断。php
对于整个架构来讲,检索索引以及rank等后端通常使用c/c++来实现,真正的展示ui可使用php/python等脚本语言来实现。对于飘红而言,能够放在ui端使用php截断飘红,也能够放在后端经过c/c++来飘红截断。文本编码能够用gbk也能够用utf-8。对于存储而言,若是使用gbk编码能够比utf-8python
使用php的优势:操做简单,有大量现成的库,不须要关注gbk等具体实现,经过mb_str库能够搞定全部事情,缺点在文本较长的时候性能及其低下。显而易见,使用C/C++的有点是性能极高,可是肯定是须要本身去关注字符编码。ios
由于排期较紧,我起初使用了php来进行飘红截断,一个文本(字数大概在5k左右)飘红大概花了200+ms,这个性能是不能忍受的,以后我只好改为了c/c++来进行飘红截断,性能获得显著提高,耗时只有0.01ms。性能提高达到万倍。c++
如今描述下怎么来使用c/c++来飘红截断文本。后端
首先先简要介绍一下gbk(gb2312编码)。安全
GBK是在国家标准GB2312基础上扩容后兼容GB2312的标准,包含全部的中文字符。一个中文须要3个字节,最高位为1,因此第一个字节大于0x80. 此外字符编码还有utf-8。gbk和utf-8之间能够经过Unicode编码进行转换。gbk,utf-8,Unicode之间的关系若是有不了解的请本身Google或者百度.架构
须要截断文本content
须要飘红的词组hi_words,用"|"进行进行分割
须要截断的字数 len函数
优先截断字节数目必定;优先截取飘红词右边的整句;若是整句不到截断字数,在拿飘红词左边的句子进行填充。性能
这个使用strtok_r轻轻松松搞定(注意不要使用非线程安全的strtok).测试
int getHighWord(char* high_words, vector<string>& words) { char keyword[1024]; char *ptok = NULL; snprintf(keyword, sizeof(keyword), "%s", high_words); char *part = strtok_r(keyword, "|", &ptok); while( part != NULL) { string tmp(part); words.push_back(tmp); part = strtok_r(NULL, "|", &ptok); } return 0; }
sep_china是中文句子分隔符,sep_uni是英文分割符,他们都是一个整句。
vector<string> sep_china; vector<string> sep_uni; sep_china.push_back(","); sep_china.push_back("。"); sep_china.push_back(";"); sep_china.push_back(":"); sep_china.push_back("!"); sep_uni.push_back(","); sep_uni.push_back("."); sep_uni.push_back("?"); sep_uni.push_back(";"); sep_uni.push_back(":");
string str_cnt(content); //首先判断有没有这个word int pos_word = -1; string min_word; int i; for(i=0; i<words.size(); i++) { pos_word = str_cnt.find(words[i]); if(pos_word > 0) { min_word = words[i]; break; } } //若是没有找到,直接截断返回 if( pos_word < 0) { if( num <= getExtraWord(content, hi_word, 0, num) ) { strcat(hi_word,"..."); return 0; } }
//还须要多少字节 int left = num - min_word.size(); //前面有多少字节 int before = pos_word; //后面还有多少字节 int after = str_cnt.size() - min_word.size() - pos_word; //得到前一个标点的位置 int pos_quoto_china = -1; int pos_quoto_uni = -1; string quoto_str = str_cnt.substr(0, pos_word); pos_quoto_china = findMaxPos(quoto_str, sep_china) + 2; //一个标点2个字符 pos_quoto_uni = findMaxPos(quoto_str,sep_uni) +1 ; //一个标点一个字符 int quoto_pos = pos_quoto_uni > pos_quoto_china ? pos_quoto_uni : pos_quoto_china; //得到前一个标点有多少字节 int quoto = quoto_pos > 0 ? pos_word - quoto_pos : 0;
int getStatus(int before, int after, int quoto, int left) { int status = 0; if(quoto >= left) { //直接截断quoto status = 0; } else if( quoto + after > left) { //返回截断quoto+left status =1 ; } else { //返回截断after和left-after的前面 status =2; } return status; }
char before_word[1024]; char after_word[1024]; int left_cnt; int right_cnt; before_word[0] = after_word[0] = '0'; switch(status) { case 0: getExtraWord(content, hi_word, pos_word, left); strcat(hi_word, "..."); break; case 1: left_cnt = getExtraWord(content, before_word, pos_word -1, -1 * quoto); right_cnt = getExtraWord(content, after_word, pos_word + min_word.size(), left- quoto); if( left_cnt < pos_word) { strcpy(hi_word, "..."); } strcat(hi_word, before_word); strcat(hi_word, min_word.c_str() ); strcat(hi_word, after_word); strcat(hi_word, "..."); break; case 2: right_cnt = getExtraWord(content, before_word, pos_word + min_word.size() , after); left_cnt = getExtraWord(content, after_word, pos_word-1 , after- left); if( left_cnt < pos_word) { strcpy(hi_word, "..."); } strcat(hi_word, before_word); strcat(hi_word, min_word.c_str() ); strcat(hi_word, after_word); strcat(hi_word, "..."); break; default: getExtraWord(content, hi_word, 0, num); break; }
其中最为核心的为截断函数,以下所示,从word的begin位置截取length个字符(length小于0表示从左截断,0x80是判断标准。
int getExtraWord(const char* word, char* res, int begin, int length) { if(length == 0 ) { res[0] = '\0'; return 0; } int i; int flag; const char *str = word + begin; const char *chech_vaild = (length >0 )? str : str+1; if( false == checkVaild(chech_vaild) ) { cout << "not vaild\n"; //尽力修复下吧 begin ++; str++; } //若是从后截断 word没有 length,直接返回 if( (int)strlen(str) <= length ) { strcpy(res, str); return strlen(str); } //若是从前截断 word没有lenght长 if( begin <= -1 * length) { strncpy(res, word, begin); res[begin ] = '\0'; return begin; } flag = length > 0 ? 1 : -1; int num = 0; while( *str ) { //若是是中文 if((unsigned char)*str > 0x80) { num += 2; str += flag * 2; } else { num ++; str += flag; } if( num >= flag * length) { break; } } res[0] = '\0'; i = 0; str = (length > 0) ? word + begin : word + begin - num + 1; while(*str) { res[i++] = *(str++); if(i==num) { break; } } res[i] = '\0'; return i; }
int main() { const char* content = "啊。鸡 从到信阳,而后在火车站有直达鸡公山的汽车,只收10元。约一个小时就到鸡公山脚下,之前票价是63,自从港中旅进驻鸡公山后,如今好像改为123了,固然了,港中旅把鸡公山搞得更加漂亮和特点了。 在鸡公山门口有去山顶的旅游大巴,单程15元,往返的20元,不过建议你们作单程上去,而后步行从爬山古道或者长生谷下山,会更加有趣味。和朋友在爬山古道的入口处景区处于全监控状态,因此至关安全。美龄舞厅外面的招牌很显眼啊。颐庐是鸡公山上最有名的建筑,尽管世界各国都曾有在山上建房子,但唯独中国人靳"; vector<string> words; char* hi = "信阳"; getHighWord(hi, words); for(int i=0; i<words.size(); i++) { cout << words[i] << endl; } char hi_word[1024]; hi_word[0] = '\0'; getHilight(content, hi_word, words, 200); cout << "hi_word is " << hi_word << endl; }
#include <stdio.h> #include <iostream> #include <string> #include <vector> using namespace std; /** 查看一句话是不是完整的gbk语句 */ bool checkVaild(const char* word) { bool vaild = true; while(*word) { if( (unsigned char)*word++ > 0x80 ) { vaild = !vaild; } } return vaild; } int getExtraWord(const char* word, char* res, int begin, int length) { if(length == 0 ) { res[0] = '\0'; return 0; } int i; int flag; const char *str = word + begin; const char *chech_vaild = (length >0 )? str : str+1; if( false == checkVaild(chech_vaild) ) { cout << "not vaild\n"; //尽力修复下吧 begin ++; str++; } //若是从后截断 word没有 length,直接返回 if( (int)strlen(str) <= length ) { strcpy(res, str); return strlen(str); } //若是从前截断 word没有lenght长 if( begin <= -1 * length) { strncpy(res, word, begin); res[begin ] = '\0'; return begin; } flag = length > 0 ? 1 : -1; int num = 0; while( *str ) { //若是是中文 if((unsigned char)*str > 0x80) { num += 2; str += flag * 2; } else { num ++; str += flag; } if( num >= flag * length) { break; } } res[0] = '\0'; i = 0; str = (length > 0) ? word + begin : word + begin - num + 1; while(*str) { res[i++] = *(str++); if(i==num) { break; } } res[i] = '\0'; return i; } int getStatus(int before, int after, int quoto, int left) { int status = 0; if(quoto >= left) { //直接截断quoto status = 0; } else if( quoto + after > left) { //返回截断quoto+left status =1 ; } else { //返回截断after和left-after的前面 status =2; } return status; } int findMaxPos(string content, vector<string>quotos) { int i; int pos = string::npos; int tmp_pos; for(i=0; i<quotos.size(); i++) { tmp_pos = content.rfind(quotos[i]); if( tmp_pos > pos) { pos = tmp_pos; } } return pos; } int getHilight(const char* content, char* hi_word, vector<string> words, int num) { //若是不够长 if( strlen(content) <= num ) { strcpy(hi_word, content); return 0; } //首先分词 vector<string> sep_china; vector<string> sep_uni; sep_china.push_back(","); sep_china.push_back("。"); sep_china.push_back("?"); sep_china.push_back(";"); sep_china.push_back(":"); sep_china.push_back("!"); sep_uni.push_back(","); sep_uni.push_back("."); sep_uni.push_back("?"); sep_uni.push_back(";"); sep_uni.push_back(":"); sep_uni.push_back(";"); //sep_uni.push_back(" "); //内容 string str_cnt(content); //首先判断有没有这个word int pos_word = -1; string min_word; int i; for(i=0; i<words.size(); i++) { pos_word = str_cnt.find(words[i]); if(pos_word > 0) { min_word = words[i]; break; } } //若是没有找到,直接截断返回 if( pos_word < 0) { if( num <= getExtraWord(content, hi_word, 0, num) ) { strcat(hi_word,"..."); return 0; } } //还须要多少字节 int left = num - min_word.size(); //前面有多少字节 int before = pos_word; //后面还有多少字节 int after = str_cnt.size() - min_word.size() - pos_word; //得到前一个标点的位置 int pos_quoto_china = -1; int pos_quoto_uni = -1; string quoto_str = str_cnt.substr(0, pos_word); pos_quoto_china = findMaxPos(quoto_str, sep_china) + 2; //一个标点2个字符 pos_quoto_uni = findMaxPos(quoto_str,sep_uni) +1 ; //一个标点一个字符 int quoto_pos = pos_quoto_uni > pos_quoto_china ? pos_quoto_uni : pos_quoto_china; //得到前一个标点有多少字节 int quoto = quoto_pos > 0 ? pos_word - quoto_pos : 0; int status = getStatus(before, after, quoto, left); char before_word[1024]; char after_word[1024]; int left_cnt; int right_cnt; before_word[0] = after_word[0] = '0'; switch(status) { case 0: getExtraWord(content, hi_word, pos_word, left); strcat(hi_word, "..."); break; case 1: left_cnt = getExtraWord(content, before_word, pos_word -1, -1 * quoto); right_cnt = getExtraWord(content, after_word, pos_word + min_word.size(), left- quoto); if( left_cnt < pos_word) { strcpy(hi_word, "..."); } strcat(hi_word, before_word); strcat(hi_word, min_word.c_str() ); strcat(hi_word, after_word); strcat(hi_word, "..."); break; case 2: right_cnt = getExtraWord(content, before_word, pos_word + min_word.size() , after); left_cnt = getExtraWord(content, after_word, pos_word-1 , after- left); if( left_cnt < pos_word) { strcpy(hi_word, "..."); } strcat(hi_word, before_word); strcat(hi_word, min_word.c_str() ); strcat(hi_word, after_word); strcat(hi_word, "..."); break; default: getExtraWord(content, hi_word, 0, num); break; } return 0; } int getHighWord(char* high_words, vector<string>& words) { char keyword[1024]; char *ptok = NULL; snprintf(keyword, sizeof(keyword), "%s", high_words); char *part = strtok_r(keyword, "|", &ptok); while( part != NULL) { string tmp(part); words.push_back(tmp); part = strtok_r(NULL, "|", &ptok); } return 0; } int main() { const char* content = "啊。鸡 从到信阳,而后在火车站有直达鸡公山的汽车,只收10元。约一个小时就到鸡公山脚下,之前票价是63,自从港中旅进驻鸡公山后,如今好像改为123了,固然了,港中旅把鸡公山搞得更加漂亮和特点了。 在鸡公山门口有去山顶的旅游大巴,单程15元,往返的20元,不过建议你们作单程上去,而后步行从爬山古道或者长生谷下山,会更加有趣味。和朋友在爬山古道的入口处景区处于全监控状态,因此至关安全。美龄舞厅外面的招牌很显眼啊。颐庐是鸡公山上最有名的建筑,尽管世界各国都曾有在山上建房子,但唯独中国人靳"; vector<string> words; char* hi = "信阳"; getHighWord(hi, words); for(int i=0; i<words.size(); i++) { cout << words[i] << endl; } char hi_word[1024]; hi_word[0] = '\0'; getHilight(content, hi_word, words, 100); cout << "hi_word is " << hi_word << endl; }