DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   string: get token from string and compare token from text file (http://www.daniweb.com/forums/thread39547.html)

amen Feb 14th, 2006 2:14 am
string: get token from string and compare token from text file
 
i need to write a code on how can break a sentence into word and find out the word belong to.
example :
when user input a sentence cindy walk slow.
1.break the sentence into cindy,walk,slow.
2.get each word belong to and display.
- cindy is a noun
- walk is a verb
- slow is a adjective.

i had make 2 file text:
1. verb .txt
2. adjective

verb.txt
eat
walk
ran
sleep

adjective
fast
slow
pretty

this is the code i had done:

   const size=100; //size of string
  char string[size],temp[size][32];
  char *tokenPtr;
  int i=0;

  cout<<"input string\n";
  cin.getline(string,size);//read line of string

  /*function char *strtok(char *s1,const char *s2) breaks s1 into token(word)
          separated by character in s2
        */

  tokenPtr =strtok(string," ");//pointer to first token

  while (tokenPtr != NULL)
          {
              strcpy(temp[i],tokenPtr);//save the token to array
        i++;
              tokenPtr=strtok(NULL," ");
      }

          for(int j=0;j<i;j++)
  {
            char word1[30],word2[30];

      ifstream dictionary1("verb.txt");
      ifstream dictionary2("adjective.txt");

        while (dictionary1>>word1)
                if (strcmp(word1,temp[j])==0)
                                    cout<<temp[j]<<" is a verb \n";
                while (dictionary2>>word2)
                    if (strcmp(word2,temp[j])==0)
                      cout<<temp[j]<<" is a adjective \n";

  }
  return 0;
}

the output:
input string
ali walk slow
walk is a verb
slow is a adjective


what i want to know is how i can get word ali belong to?
i didn't want to make a text file that contain of the noun.
if the word not in the verb.txt and adjective.txt,therefore it is a noun..

please help me..
anyway thank you.

Ancient Dragon Feb 14th, 2006 7:08 am
Re: string: get token from string and compare token from text file
 
set a boolean flag to indicate if the word is found in one of the two files. If not set then it must be a noun. Something like this sudo-code. Note: search second file ONLY if the word is not in the first file.
set flag to false
search verb.txt file for word
if word found in verb.txt then set flag to true
if flag still false
begin
    search adjective.txt file for word
    if word found in adjective.txt file set flag to true
end if
if flag still false
  word is a noun

amen Feb 14th, 2006 10:25 pm
Re: string: get token from string and compare token from text file
 
thank you..i think i get it on..let me try first..


All times are GMT -4. The time now is 11:30 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC