2012-02-28 19 views
1

私はリストを返す文字列入力を伴うこの関数sentanceParseを持っています。入力は "こんにちは私の名前はアントンです。あなたの名前は何ですか?"戻り値は "Hello my name is Anton"と "What's your name?"を含むリストになります。しかし、これは何が起こるかではありません。文中の空白が区切り文字のように扱われているので、返り値は、私が期待していたものではなく、 "Hello"、 "my"、 "name"などと思われます。リスト内の空白文字列はありますか?

私はこれをどのように解決することを提案しますか?

私はこの問題は私のコード内に存在しません、私もポストにそれを追加します100%を確認していないとして:

メイン:

list<string> mylist = sentanceParse(textCipher); 
list<string>::iterator it; 
for(it = mylist.begin(); it != mylist.end(); it++){ 
    textCipher = *it; 
    cout << textCipher << endl; //This prints out the words separately instead of the entire sentances. 

sentanceParse:

list<string> sentanceParse(string strParse){ 
    list<string> strList; 
    int len = strParse.length(); 
    int pos = 0; 
    int count = 0; 
    for(int i = 0; i < len; i++){ 
     if(strParse.at(i) == '.' || strParse.at(i) == '!' || strParse.at(i) == '?'){ 
      if(i < strParse.length() - 1){ 
       while(i < strParse.length() - 1 && (strParse.at(i+1) == '.' || strParse.at(i+1) == '!' || strParse.at(i+1) == '?')){ 
        if(strParse.at(i+1) == '?'){ 
         strParse.replace(i, 1, "?"); 
        } 
        strParse.erase(i+1, 1); 
        len -= 1; 
       } 
      } 
      char strTemp[2000]; 
      int lenTemp = strParse.copy(strTemp, i - pos + 1, pos); 
      strTemp[lenTemp] = '\0'; 
      std::string strAdd(strTemp); 
      strList.push_back(strAdd); 
      pos = i + 1; 
      count ++; 
     } 
    } 

    if(count == 0){ 
     strList.push_back(strParse); 
    } 

    return strList; 
} 
+0

ここでブーストを使用していない理由は何ですか?例えば、 'boost :: tokenizer'(http://www.boost.org/doc/libs/1_49_0/libs/tokenizer/index.html)があります。あなたの仕事は完璧にうまくいくでしょう。少し..スパルタン)。 – Xeo

+0

実際には聞いたことがないので、私はそれをチェックします。 – Anton

+0

基本的には、 'tokenizer >のようになります(strParse、char_seperator ("。!? ")); (自動&tok:toks){/ *各文を処理する... * /} ' – Xeo

答えて

1

文章解析の実装が間違っていますが、ここではより簡単な解決策です。

std::list<std::string> sentence_parse(const std::string &str){ 
    std::string temp; 
    std::list<std::string> t; 

    for(int x=0; x<str.size();++x){ 
     if(str[x]=='.'||str[x]=='!'||str[x]=='?'){ 
      if(temp!="")t.push_back(temp);//Handle special case of input with 
             //multiple punctuation Ex. Hi!!!! 
      temp=""; 
     }else temp+=str[x]; 
    } 
    return t; 
} 

編集:ここでは

は、この機能を使用して完全なプログラム例です。あなたのコンソールにいくつかの文章を入力し、Enterを押すと、改行で句読点の代わりに文を吐き出します。

#include <iostream> 
#include <string> 
#include <list> 
std::list<std::string> sentence_parse(const std::string &str){ 
    std::string temp; 
    std::list<std::string> t; 

    for(int x=0; x<str.size();++x){ 
     if(str[x]=='.'||str[x]=='!'||str[x]=='?'){ 
      if(temp!="")t.push_back(temp);//Handle special case of input with 
              //multiple punctuation Ex. Hi!!!! 
      temp=""; 
     }else temp+=str[x]; 
    } 
    return t; 
} 
int main (int argc, const char * argv[]) 
{ 
    std::string s; 

    while (std::getline(std::cin,s)) {  
     std::list<std::string> t= sentence_parse(s); 
     std::list<std::string>::iterator x=t.begin(); 
     while (x!=t.end()) { 
      std::cout<<*x<<"\n"; 
      ++x; 
     } 

    } 

    return 0; 
} 
+0

しかし、入力が「One two three!」であった場合、リスト内で「One」、「Two」、「Three」のように分割されませんか?私の問題は、区切り文字のように動作する空白スペースです。 – Anton

+0

このコードを試してみると、空白がセパレータとして機能しなくなります。 –

+0

動作しません(テスト済み)。それは、各与えられたセンテンスから最後の単語だけを返します。センテンスがリストに追加されると、最後の空白の後のコンテンツだけが含まれます。 – Anton

0
// This function should be easy to adapt to any basic libary 
// this is in Windows MFC 
// pass in a string, a char and a stringarray 
// returns an array of strings using char as the separator 

void tokenizeString(CString theString, TCHAR theToken, CStringArray *theParameters) 
{ 
    CString temp = ""; 
    int i = 0; 

    for(i = 0; i < theString.GetLength(); i++) 
    {         
     if (theString.GetAt(i) != theToken) 
     { 
      temp += theString.GetAt(i); 
     } 
     else 
     { 
      theParameters->Add(temp); 
      temp = ""; 
     } 
     if(i == theString.GetLength()-1) 
      theParameters->Add(temp); 
    } 
} 
+0

これは、入力がユーザー生成されなかった場合に機能します。入力が「こんにちは!!!私はアントンです。あなたの名前は何ですか?」と入力してみましょう。この場合、私は帰りが「こんにちは!」、「私はアントンです」、「あなたの名前は何ですか?」と願っています。私はおそらくあなたがリストの代わりにここでやっているやり方で配列を使って作業することを考えます。 – Anton

関連する問題