簡體   English   中英

如何使用STL C ++讀取文件並保存連字符

[英]How to read file and save hyphen using STL C++

我必須閱讀文本文件,將其轉換為小寫並刪除非字母字符,但還需要保存連字符,不要將其視為單詞。 這是我的編碼。 它將連字符作為UnknownWords單詞進行UnknownWords 我只想保存連字符,只想計算.txt連字符左右兩側的單詞。

我的輸出:

110 Known words read
79 Unknown words read //it is because it is counting hyphen as word

所需的輸出是:

110 Known words read
78 Unknown words read   

碼:

void WordStats::ReadTxtFile(){
    std::ifstream ifile(Filename);
    if(!ifile)
    {
        std::cerr << "Error Opening file " << Filename << std::endl;
        exit(1);
    }
    for (std::string word; ifile >> word; )
    {

        transform (word.begin(), word.end(), word.begin(), ::tolower);
        word.erase(std::remove_if(word.begin(), word.end(), [](char c)
        {
            return (c < 'a' || c > 'z') && c != '\'' && c != '-';
        }),  word.end());
        if (Dictionary.count(word))
        {
            KnownWords[word].push_back(ifile.tellg());
        }
        else
        {
            UnknownWords[word].push_back(ifile.tellg()); 
        }
    }
    //  std::string word; ifile >> word;


    std::cout << KnownWords.size() << " known words read." << std::endl;
    std::cout << UnknownWords.size() << " unknown words read." << std::endl;
}

如果您不想自己放一個僅是"-"的單詞,請在添加到單詞vectors之前檢查一下:

for (std::string word; ifile >> word; )
{

    transform (word.begin(), word.end(), word.begin(), ::tolower);
    word.erase(std::remove_if(word.begin(), word.end(), [](char c)
    {
        return (c < 'a' || c > 'z') && c != '\'' && c != '-';
    }),  word.end());
    if (word.find_first_not_of("-") == string::npos) { // Ignore word that's only hyphens
        continue;
    }
    if (Dictionary.count(word))
    {
        KnownWords[word].push_back(ifile.tellg());
    }
    else
    {
        UnknownWords[word].push_back(ifile.tellg()); 
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM