最短摘要


[cpp]
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define KeyWordNumber 3

bool isAllExisted(string s1, string s2[])
{
    for (int i = 0; i < KeyWordNumber; ++i)
    {
        if (s1.find(s2[i]) == string::npos) return 0;
    }
    return 1;
}

string ExtractSummary(string description, string keywords[])
{
    int nTargetLen = description.length();
    //设置最大目标长度
    int pBegin = 0;
    //初始指针
    int pEnd = 0;
    //结束指针
    int nAbstractBegin = 0;
    //目标摘要的起始地址
    int nAbstractEnd = 0;
    //目标摘要的结束地址
    while (1)
    {
        //假设没有包含所有的关键词,并且后面的指针没有越界,往后移动指针
        while (pEnd < description.length()-1 && !isAllExisted(description.substr(pBegin, pEnd - pBegin + 1), keywords))
        {
            pEnd++;
        }
        //假设找到一段包含所有关键信息的字符串
        while (isAllExisted(description.substr(pBegin, pEnd - pBegin + 1), keywords))
        {
            if (pEnd - pBegin + 1 <= nTargetLen)
            {
                nTargetLen = pEnd - pBegin + 1;
                nAbstractBegin = pBegin;
                nAbstractEnd = pEnd;
            }
            pBegin++;
        }
        if (pEnd >= description.length()-1) break;
    }
    return description.substr(nAbstractBegin, nAbstractEnd - nAbstractBegin + 1);
}

int main()
{
    string text = "money is important than knife and money is can buy garden";
    string abstract[3];
    abstract[0] = "money";
    abstract[1] = "and";
    abstract[2] = "garden";
    string res = ExtractSummary(text, abstract);
    cout << res << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值