string: get token from string and compare token from text file

Reply

Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

string: get token from string and compare token from text file

 
0
  #1
Feb 14th, 2006
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
  1. eat
  2. walk
  3. ran
  4. sleep

adjective
  1. fast
  2. slow
  3. pretty

this is the code i had done:

  1. const size=100; //size of string
  2. char string[size],temp[size][32];
  3. char *tokenPtr;
  4. int i=0;
  5.  
  6. cout<<"input string\n";
  7. cin.getline(string,size);//read line of string
  8.  
  9. /*function char *strtok(char *s1,const char *s2) breaks s1 into token(word)
  10.   separated by character in s2
  11. */
  12.  
  13. tokenPtr =strtok(string," ");//pointer to first token
  14.  
  15. while (tokenPtr != NULL)
  16. {
  17. strcpy(temp[i],tokenPtr);//save the token to array
  18. i++;
  19. tokenPtr=strtok(NULL," ");
  20. }
  21.  
  22. for(int j=0;j<i;j++)
  23. {
  24. char word1[30],word2[30];
  25.  
  26. ifstream dictionary1("verb.txt");
  27. ifstream dictionary2("adjective.txt");
  28.  
  29. while (dictionary1>>word1)
  30. if (strcmp(word1,temp[j])==0)
  31. cout<<temp[j]<<" is a verb \n";
  32. while (dictionary2>>word2)
  33. if (strcmp(word2,temp[j])==0)
  34. cout<<temp[j]<<" is a adjective \n";
  35.  
  36. }
  37. return 0;
  38. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,171
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1439
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: string: get token from string and compare token from text file

 
0
  #2
Feb 14th, 2006
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.
  1. set flag to false
  2. search verb.txt file for word
  3. if word found in verb.txt then set flag to true
  4. if flag still false
  5. begin
  6. search adjective.txt file for word
  7. if word found in adjective.txt file set flag to true
  8. end if
  9. if flag still false
  10. word is a noun
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

Re: string: get token from string and compare token from text file

 
0
  #3
Feb 14th, 2006
thank you..i think i get it on..let me try first..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC