944,124 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16561
  • C++ RSS
Feb 14th, 2006
0

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

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. eat
  2. walk
  3. ran
  4. sleep

adjective
C++ Syntax (Toggle Plain Text)
  1. fast
  2. slow
  3. pretty

this is the code i had done:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Feb 14th, 2006
0

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.
C++ Syntax (Toggle Plain Text)
  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
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Feb 14th, 2006
0

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

thank you..i think i get it on..let me try first..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Is this notation possible?
Next Thread in C++ Forum Timeline: Problem with object declaration





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC