我怎样才能找到一个句子的n元语法
How can I find the n-gram of a sentence?
我得到了helloworld
的测试字符串,我必须在其中找到n-gram,具体说明它是3
。因此,我的代码应该给我以下输出: hel, ell, llo, low, owo, wor, orl, rld,
我编写的代码如下:
vector<string> generate_ngrams(string w, size_t n) {
vector<string> ngrams;
for (auto i = 0; i < n; i++) {
ngrams.push_back(w.substr(i * n, n));
}
return ngrams;
我用我的代码得到的输出: hel, low, orl,
例如,我如何操作到目前为止的代码,以便 for 循环调用某种类型的推送回"e",然后找到它后面的两个字母,并继续这样做,直到它不能再这样做了?
这是主要的调用,也是为了看看被扔进去了什么:
case 2:{
string s;
int n;
cin >> n;
cin.ignore(100, 'n');
getline(cin, s);
auto v = generate_ngrams(s, n);
copy (v.begin(), v.end(), oss);
cout << endl;
break;
}
n 是子集的大小,而不是 w 的大小。
for (int i = 0; i <= w.length() - n; ++i)
{
ngrams.push_back(w.substr(i, n));
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 创建一个程序,其中句子在C++中全部大写,全部小写
- 我需要编写一个代码,从用户那里获取输入(句子)并停止在@处
- 我怎样才能找到一个句子的n元语法
- 在C++中输入一个句子
- 我如何将句子分成一个单词
- 如何在C++中迭代一个句子的单词
- 将三个文本文件组合成一个文本文件,形成一个句子C++
- 句子中的最后一个单词在句子颠倒后不打印
- 使用QRegularExpression从句子中提取一个十进制数
- 使用QRegularExpression提取一个完整的句子
- 把一个句子分开,然后找回一个特定的单词
- 反转每个单词在一个句子中使用c++需要代码优化我的代码片段
- 在Google Code jam上问的一个c++句子的倒序
- 要求在循环中写一个句子来计算元音,即使循环进行也不会再问
- 如何存储一个完整的句子与空白的字符串在cpp