【问题标题】:parsing words in a document using specific delimiters使用特定分隔符解析文档中的单词
【发布时间】:2013-01-10 14:33:14
【问题描述】:

我有一个要从中解析单词的文档,但我想将非 a-z、A-Z、0-9 或撇号的任何内容视为空白。如果我之前使用以下代码,我该怎么做:

ifstream file;
file.open(filePath);

while(file >> word){
    listOfWords.push_back(word); // I want to make sure only words with the stated 
                                 // range of characters exist in my list.
}

因此,例如,单词 hor.se 将是我列表中的两个元素,“hor”和“se”。

【问题讨论】:

    标签: string delimiter text-parsing


    【解决方案1】:

    创建一个“空白字符”列表,然后每次遇到一个字符时,检查该字符是否在列表中,如果是,您就开始了一个新单词。这个例子是用python写的,但是概念是一样的。

    def get_words(whitespace_chars, string):
        words = []
        current_word = ""
        for x in range(0, len(string)):
            #check to see if we hit the end of a word.                                                                                                                                                                                           
            if(string[x] in whitespace_chars and current_word != ""):
                words.append(current_word)
                current_word = ""
            #add current letter to current word.                                                                                     
            else:
                current_word += string[x]
        #if the last letter isnt whitespace then the last word wont be added, so add here.                                                                                                                                                       
        if(current_word != ""):
            words.append(current_word)
        return words
    

    返回词

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多