我正在尝试读取文本文件,并按字符串将其输入到矢量字符串中。我需要它停在每个句子的结尾,然后在句子中挑出关键词。我理解如何找到关键词,但不知道如何让它最后停止输入字符串。我正在使用while循环来检查每一行,我正在考虑使用一系列if语句,例如
if(std::vector<string>::iterator i == ".") i == "\0"
到目前为止我执行向量填充的代码是:
std::string c;
ifstream infile;
infile.open("example.txt");
while(infile >> c){
a.push_back(c);
}
好的,所以我已经开始考虑将文本文件的每个单词加载到标记中的方法,考虑到“”作为分隔符,并且有一个特殊的单词列表:
const int MAX_PER_LINE = 512;
const int MAX_TOK = 20;
const char* const DELIMETER = " -";
const char* const SPECIAL ="!?.";
const char* const ignore[] = {"Mr.", "Ms.","Mrs.","sr.", "Ave.", "Rd."};
然后
if(!file.good()){
return 1;
}
//parsing algorithm paraphrased from cs.dvc.edu/HowTo_Parse.html
while(!file.eof()){
char line[MAX_PER_LINE];
file.getline(line, MAX_PER_LINE);
int n = 0;
const char* token[MAX_TOK] = {};
token[0] = strtok(line, DELIMETER);
if(token[0]){
for(n = 1; n < MAX_TOK; ++n){
token[n] = strtok(0, DELIMETER);
if(!token[n]) break;
}
}
//for(int i = 0; i < n; ++i){
for(int i = 0; i < n; ++i){
cout << "Token[" << i << "] =" << token[i] << endl;
cout << endl;
}
}
现在我正在寻找一个if语句中的内容,以便它检查每个标记的特殊情况,或者如果它们遵循具有特殊情况的标记,则将它们加载到新的集合标记中。我知道psuedo代码大部分,但我不知道要用什么样的语法,如果(token [i]包含特殊情况或令牌[i]之前没有任何东西(对于第一个令牌)或大写,并使用特殊情况下的令牌将其加载到新令牌中。
任何帮助将不胜感激。
答案 0 :(得分:2)
编写自己的句子分隔符对于没有国际化的小项目或项目是可以的。 对于文本边界的基于高级文本的解决方案,我建议ICU BreakIterator。基于unicode.org标准化,它们提供字符,单词,换行符和句子边界。他们有C ++的开源库(我认为也是Java)。 请参阅this page,它有链接到库的下载页面。
这样可以避免重新发明轮子并避免以后出现潜在问题。大多数领先的出版软件产品如QuarkXPress等都使用这个库。
编辑:
我试图在句子边界上找到ICU的BreakIterator用法的快速教程,但我找到了单词边界的例子 - (句子边界计算将非常相似,可能需要在下面用createWordInstance
替换createSentenceInstance
)
void listWordBoundaries(const UnicodeString& s) {
UErrorCode status = U_ZERO_ERROR;
BreakIterator* bi = BreakIterator::createWordInstance(Locale::getUS(), status);
bi->setText(s);
int32_t p = bi->first();
while (p != BreakIterator::DONE) {
printf("Boundary at position %d\n", p);
p = bi->next();
}
delete bi;
}
答案 1 :(得分:0)
查找以句点结尾的单词非常简单,只需检查word.back() == '.'
。您还需要首先检查word.empty()
,因为如果字符串为空,back()
是未定义的行为。如果您的编译器不支持C ++ 11,那么您也可以使用word[word.size() - 1] == '.'
来完成更长的工作。
这是一个基本的例子,它使用任何以“。”结尾的单词天真地分割句子:
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char** argv) {
if (argc == 1) {
std::cerr << "Usage: " << argv[0] << " [text to split]\n"
<< "Splits the input text into one sentence per line." << std::endl;
return 1;
}
std::vector<std::string> sentences;
std::string current_sentence;
for (int i = 1; i < argc; ++i) {
std::string word(argv[i]);
current_sentence.append(word);
current_sentence.push_back(' ');
/* use word.back() == '.' for C++11 */
if (!word.empty() && word[word.size() - 1] == '.') {
sentences.push_back(current_sentence);
current_sentence.clear();
}
}
if (!current_sentence.empty()) {
sentences.push_back(current_sentence);
}
for (size_t i = 0; i < sentences.size(); ++i) {
std::cout << sentences[i] << std::endl;
}
return 0;
}
运行如:
$ g++ test.cpp
$ ./a.out This is a test. And a second sentence. So we meet again Mr. Bond.
This is a test.
And a second sentence.
So we meet again Mr.
Bond.
注意它是怎么想的“先生”是句子的结尾。
我不确定一种聪明的处理方式,但是一个(脆弱的)选项是制作一个不是句子结尾的单词列表,然后检查单词是否在列表中,如这样:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
const std::string tmp[] = {
"dr.",
"mr.",
"mrs.",
"ms.",
"rd.",
"st."
};
const std::set<std::string> ABBREVIATIONS(tmp, tmp + sizeof(tmp) / sizeof(tmp[0]));
bool has_period(const std::string& word) {
return !word.empty() && word[word.size() - 1] == '.';
}
bool is_abbreviation(std::string word) {
/* Convert to lowercase, so we don't need to check every possible
* variation of each word. Remove this (and update the set initialization)
* if you don't care about handling poor grammar. */
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
/* Check if the word is an abbreviation. */
return ABBREVIATIONS.find(word) != ABBREVIATIONS.end();
}
int main(int argc, char** argv) {
if (argc == 1) {
std::cerr << "Usage: " << argv[0] << " [text to split]\n"
<< "Splits the input text into one sentence per line." << std::endl;
return 1;
}
std::vector<std::string> sentences;
std::string current_sentence;
for (int i = 1; i < argc; ++i) {
std::string word(argv[i]);
current_sentence.append(word);
current_sentence.push_back(' ');
if (has_period(word) && !is_abbreviation(word)) {
sentences.push_back(current_sentence);
current_sentence.clear();
}
}
if (!current_sentence.empty()) {
sentences.push_back(current_sentence);
}
for (size_t i = 0; i < sentences.size(); ++i) {
std::cout << sentences[i] << std::endl;
}
return 0;
}
在C ++ 11中,您可以使用unordered_set
提高效率,使用std::string::back
更简单,更简单的初始化(std::set<std::string> PERIOD_WORDS = { "dr.", "mr.", "mrs." /*etc.*/ }
)。
运行此版本:
$ g++ test.cpp
$ ./a.out This is a test. And a second sentence. So we meet again Mr. Bond.
This is a test.
And a second sentence.
So we meet again Mr. Bond.
但是,当然,它仍然没有发现我们没有明确编程的任何情况:
$ ./a.out Example Ave. is just north of here.
Example Ave.
is just north of here.
即使我们添加了这些内容,也很难检测到“我居住在例如Ave.”这样的句子,其中句子以缩写结尾。我希望这是一个有用的开始。
编辑:我刚刚阅读了评论中链接的sentence breaking Wikipedia article,并且合并规则相对容易:
(c)如果下一个标记大写,则结束一个句子。
类似的东西:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
const std::string tmp[] = {
"ave.",
"dr.",
"mr.",
"mrs.",
"ms.",
"rd.",
"st."
};
const std::set<std::string> PERIOD_WORDS(tmp, tmp + sizeof(tmp) / sizeof(tmp[0]));
bool has_period(const std::string& word) {
return !word.empty() && word[word.size() - 1] == '.';
}
bool is_abbreviation(std::string word) {
/* Convert to lowercase, so we don't need to check every possible
* variation of each word. Remove this (and update the set initialization)
* if you don't care about handling poor grammar. */
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
/* Check if the word is a word that ends with a period. */
return PERIOD_WORDS.find(word) != PERIOD_WORDS.end();
}
bool is_capitalized(const std::string& word) {
return !word.empty() && std::isupper(word[0]);
}
int main(int argc, char** argv) {
if (argc == 1) {
std::cerr << "Usage: " << argv[0] << " [text to split]\n"
<< "Splits the input text into one sentence per line." << std::endl;
return 1;
}
std::vector<std::string> sentences;
std::string current_sentence;
for (int i = 1; i < argc; ++i) {
std::string word(argv[i]);
std::string next_word(i + 1 < argc ? argv[i + 1] : "");
current_sentence.append(word);
current_sentence.push_back(' ');
if (next_word.empty()
|| has_period(word)
&& (!is_abbreviation(word) || is_capitalized(next_word))) {
sentences.push_back(current_sentence);
current_sentence.clear();
}
}
for (size_t i = 0; i < sentences.size(); ++i) {
std::cout << sentences[i] << std::endl;
}
return 0;
}
然后甚至像这样的工作:
$ ./a.out Example Ave. is just north of here. I live on Example Ave. Test test test.
Example Ave. is just north of here.
I live on Example Ave.
Test test test.
但它仍然无法处理某些情况:
$ ./a.out Mr. Adams lives on Example Ave. Example Ave. is just north of here. I live on Example Ave. Test test test.
Mr.
Adams lives on Example Ave.
Example Ave. is just north of here.
I live on Example Ave.
Test test test.