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

StackOverflow https://stackoverflow.com/questions/5890483

سؤال

I'm working on a concordance program and currently working on the getContext function. I need this function work somewhat like regex, but I want it to return a vector of strings before and after the specified word. I don't know if what I'm thinking about is correct, but it's all that I can think of.

So this is what I had on mind: It takes in a word and creates two vectors and return one the left and right of the specify word.

Thanks. :D

I didn't think I would need to include the whole code file, but if anyone needs it, I can put it up too.

/* 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;
}
هل كانت مفيدة؟

المحلول

If you're just looking for a std::string in a std::vector<std::string> then you can use 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();
}

If you're looking for a way to search for a partial match of a word and best match based off a percentage or some other more complex context and regex is not an option then I think we'd need a much better description of what you're trying to accomplosh and what the real problem your looking to solve is.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top