gpt4 book ai didi

c++ - 在字符串中定位匹配的单词

转载 作者:行者123 更新时间:2023-11-30 03:36:33 25 4
gpt4 key购买 nike

我有一个包含多个段落的文件 A。我需要确定我在哪里匹配了另一个文件 B 中的单词。我需要告诉每个单词的段落、行号和单词编号,包括那些与文件 B 中的单词匹配的单词。我终于到此为止了,放弃了在 vector 、数组和字符串拆分上。我学会了(我认为)stringstream。目前,我在行中阅读,然后将其拆分为“。”成句子,然后再把这些句子读回去,在“”上分开。我计算行号,计算和匹配单词,但我似乎无法获得段落编号(我已经意识到 p++ 实际上是在计算行数,而 l++ 也在计算单词数)。有人可以帮我吗? edit 每个段落用“\n”分隔,每个句子用“.”分隔。我仍然需要找出一种方法来忽略所有其他标点符号,以便单词 100% 匹配,并且不会被逗号、分号或其他标点符号丢弃。我猜那将是某个地方的正则表达式。

从带有文本的文件输入看起来像:

    My dog has fleas in his weak knees. This is a line.  The paragraph is ending.'\n'    Fleas is a word to be matched.  here is another line.  The paragraph is ending.'\n'

output should look something like:

    paragraph1 line 1 word 1  My    paragraph1 line 1 word 2  dog    paragraph1 line 1 word 3  has    paragraph1 line 1 word 4  MATCHED!  fleas
while (getline(fin, para)) { //get the paragraphs
pbuffer.clear();
pbuffer.str("."); //split on periods
pbuffer << para;
p++; //increase paragraph number

while (pbuffer >> line) { //feed back into a new buffer

lbuffer.clear();
lbuffer.str(" "); //splitting on spaces
lbuffer << line;
l++; //line counter

while (lbuffer >> word) { //feed back in
cout << "l " << l << " W: " << w << " " << word;
fmatch.open("match.txt");
while (fmatch >> strmatch) { //did I find a match?
if (strmatch.compare(word) == 0) {
cout << " Matched!\n";
}
else {
cout << "\n";
}

}

最佳答案

既然你说你可以在阅读时写下每个单词,我们就不会费心去收集了。我们只使用 istringstream istream_iterator 并对抗指数。
假设fin很好,我要简单地写信给cout您可以进行适当的调整以写入您的文件。

1st 您需要将“fmatch.txt”读入 vector<string>像这样:

const vector<string> strmatch{ istream_iterator<string>(fmatch), istream_iterator<string> }

然后你只想在嵌套循环中使用它:

string paragraph;
string sentence;

for(auto p = 1; getline(fin, paragraph, '\n'); ++p) {
istringstream sentences{ paragraph };

for(auto s = 1; getline(sentences, sentence, '.'); ++s) {
istringstream words{ sentence };

for_each(istream_iterator<string>(words), istream_iterator<string>(), [&, i = 1](const auto& word) mutable { cout << 'w' << i++ << ", p" << p << ", s" << s << (find(cbegin(strmatch), cend(strmatch), word) == cend(strmatch) ? ", word, " : ", namedEntity, ") << word << endl; });
}
}

Live Example

编辑:

作为解释,我使用的是 for_each 对句子中的每个单词调用 lambda。

让我们分解 lambda 并解释每个部分的作用:

  • [&这通过引用将 lambda 声明范围内的任何变量公开给 lambda 以供使用:http://en.cppreference.com/w/cpp/language/lambda#Lambda_capture因为我正在使用 strmatch , p , 和 s在 lamda 中,这些将通过引用捕获
  • , i = 1] C++14 允许我们在类型为 auto 的 lambda 捕获中声明一个变量所以i是一个 int每次租用声明 lambda 的范围时都会重新初始化,这里是嵌套 for 主体的每个条目。 -循环
  • (const auto& word)这是传递给 lambda 的参数列表:http://en.cppreference.com/w/cpp/language/lambda这里for_each只会传递string小号
  • mutable因为我正在修改 i ,这是 lambda 拥有的,我需要它是非 const所以我声明 lambda mutable

在 lambda 的主体中,我将只使用 find 使用标准插入运算符写入值。

编辑 2:

如果您仅限于使用 C++11,您将无法在 lambda 捕获中声明变量。您可以只在外部提供:

string paragraph;
string sentence;

for(auto p = 1; getline(fin, paragraph, '\n'); ++p) {
istringstream sentences{ paragraph };

for(auto s = 1; getline(sentences, sentence, '.'); ++s) {
istringstream words{ sentence };
auto i = 1;

for_each(istream_iterator<string>(words), istream_iterator<string>(), [&](const auto& word){ cout << 'w' << i++ << ", p" << p << ", s" << s << (find(cbegin(strmatch), cend(strmatch), word) == cend(strmatch) ? ", word, " : ", namedEntity, ") << word << endl; });
}
}

关于c++ - 在字符串中定位匹配的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40609907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com