- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含多个段落的文件 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; });
}
}
编辑:
作为解释,我使用的是 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/
我有以下数据框 (df_hvl),列名“FzListe”和以下数据: FzListe 7MA1, 7OS1 7MA1, 7ZJB 7MA2, 7MA3, 7OS1 76G1, 7MA1, 7OS1 7
我有点小问题。仅当尝试写入的相同字符串/单词不存在时,我才想写入文件。在我的例子中,它是一个 IP 地址和端口,用“:”分隔。如果我手动写入文件,例如 193...:80 和 193...:22,它指
如何返回结果列中的单词示例? 我得到的最接近的是 [\W]{2,}[^,\W].+[?=,] ID 文本 我的结果(完全匹配) 预期(完全匹配) 1 词A,世界B,词C , 世界 B, 字B 2 wo
我想在引号之间得到一个字符串 我知道一个解决方案是: /'.*?'/ 但问题是它不适用于英语中的所有格或收缩格 例如: What is the name of Mario's brother in t
我应该在句子中找到出现最多的单词。 这是我尝试过的,但不起作用。 '); $max = -1; $resultWords = array(); $resultCount = array(); $i =
我是vim的新手。我正在尝试练习(最近阅读了一些教程),但是我发现我不能不突出显示“复制粘贴”中的字符/单词/行。 在Textmate中,我通常使用SHIFT + CTRL + LeftArrowKe
有谁知道一个JSON格式的英语词典,该词典具有(单词,定义和单词类型,例如名词/形容词/动词/副词) 这种格式: [ {"Word" : "Chair", "Definition" : "A
我正在做一些 javascript,同时我注意到我无法替换 html 标记内的“ document.getElementById('label').innerHTML = document.get
您好,我正在使用 groovy 2.1.5,我必须编写一个代码来显示具有给定路径的目录的内容/文件,然后它会备份文件并替换文件中的单词/字符串。 这是我用来尝试替换所选文件中的单词的代码 String
我正在准备一个实验,我想使用python编写程序以识别参与者说出的某些单词。 我在python中搜索了很多有关语音识别的内容,但结果却很复杂(例如CMUSphinx)。 我要实现的是一个程序,该程序接
假设我有以下代码: $size = 23.9 $size = "$size GB" write $size 我想在其他事情上使用相同的变量,即 if ($size -lt 20) {...} 这显然是
我想替换字符串中单词 Date 的所有情况,除非它是 Date()(即 Date 后跟括号)。这是一个字符串示例以及我最初尝试的内容: x gsub("Date", paste("Date:", S
我对 Java 和编程都很陌生,请记住这一点,请不要对我严厉 ^^。接下来,我最近用 Java 进行了一些培训,我喜欢这个挑战,但现在我只是陷入困境。我做了一些示例来查找用户输入的最大字符串,一切都很
我必须给一个数字x,并写x个字符串(单词)。我必须找到写得最多的那一篇。它可以工作,但是当我尝试从文件中读取它时,却没有。例如,如果我执行 a.out'' #include #include in
这里是学习者,如果这个问题看起来很荒谬,请多多包涵。假设我试图引用字符串中的字符而不是字符串本身,我该怎么做呢?我的意思是; 给定:var str = "我想知道一个大脑分散的计算机如何保持理智" 我
这是阿克沙塔。我一直在解析以下数据。我想单独获取每个单词。我可以有一个示例代码以便我可以继续吗 RTRV-HDR RH01 SIMULATOR 09-11-18 16 13 19 M R
我有一个任意字符串,它总是包含至少一个英文单词后跟一系列数字:"Hello World 1234" 我如何只提取 "Hello World" 来自字符串? 最佳答案 在我看来你需要反正则表达式: St
我正在尝试输入一个四个单词的句子,然后能够使用indexOf和子字符串单独打印出每个单词。有什么想法我做错了吗? 已编辑 那么这就是它应该的样子吗?我已经运行了两次,得到了两个不同的答案,所以我不确定
如何在文本开头查找短语(单词) 我需要非常快速的解决方案来查明文本是否以某些已知短语开头 我在 Mysql (innodb) 表中的短语如下: CREATE TABLE IF NOT EXISTS `
我在 MYSQL 表中有一本字典,该表由 240 000 个单词组成。例如,如果我有字母 G、I、G、S、N> 和 O 我想选择表中包含所有或部分这些字母(并且没有其他字母)的所有单词。 可接受的词语
我是一名优秀的程序员,十分优秀!