在不使用正则表达式之前和之后输出字符串向量'word'。制定相关程序

Output a vector of strings before and after 'word' NOT using regex. Making concordance program

本文关键字:word 程序 向量 字符串 正则表达式 输出 之后      更新时间:2023-10-16

我正在开发一个索引程序,目前正在研究getContext函数。我需要这个函数的工作方式有点像正则表达式,但我希望它在指定单词之前和之后返回字符串向量。我不知道我的想法是否正确,但这是我能想到的。

所以这就是我想到的:它接受一个单词并创建两个向量,并在指定单词的左侧和右侧返回一个向量。

谢谢:D

我认为我不需要包含整个代码文件,但如果有人需要它,我也可以把它放上去。

/* Get context for input parameter word (case-insensitive comparison)
* Return a dynamically allocated vector of strings, each string
* consisting of contextSize number of words before word and contextSize
* number of words after word, with word in the middle (set off with "<<"
* before the word and ">>" after the word). ContextSize defaults to 5.
* It is user's responsibility to delete the vector.
*/
vector<string>*Concordance::getContext(string word, int contextSize = 5){
    vector<string> before;
    vector<string> after;

    return 0;
}

如果您只是在std::vector<std::string>中寻找std::string,那么您可以使用std::find

bool IsWordInArrayOfWords(const std::vector<string>& arrayOfWords, const std::string& word)
{
    auto found = std::find(arrayOfWords.cbegin(), arrayOfWords.cend(), word);
    return found != arrayOfWords.cend();
}

如果您正在寻找一种方法来搜索单词的部分匹配和基于百分比或其他更复杂的上下文的最佳匹配,并且正则表达式不是一种选择,那么我认为我们需要更好地描述您要解决的内容以及您要解决的真正问题是什么。