Compare text and replace!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stealwings
    New Member
    • Feb 2007
    • 34

    Compare text and replace!

    I have a little problem with my program, or maybe it is not that little, anyway here is the code:
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    #include <stdio.h>
    #include <cstdlib>
    #include <string>
    #include <stdlib.h>
    #include <iomanip>
    #include <vector>
    #define MAXLENGTH 256
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char MyWord[MAXLENGTH];			//Temp array to save input file words.
    	char MyStandard[MAXLENGTH];		//Temp array to save the standard words list
    //	char *MyString;
    	vector<string> MyWords;			// Vector to hold input file.
    	vector<string> Standard;		// Vector to hold Text-normalization-phrases
    
    //	string FileName;
    	string original;
    	string replace;
    	ifstream ReadWord;
    	ifstream ReadStandard;
    	ofstream WriteWord;
    //	cout<<"imput file name plz"<<endl;
    //	cin>>FileName;
    	ReadWord.open("file.txt");
    	WriteWord.open("file.dat");
    
    	ReadStandard.open("Standard.txt");	
    	if (!ReadStandard)
    	{
    		cerr<<"Standard text file not found."<<endl;	
    		exit (1);
    	}
    	
    	while(!ReadStandard.eof())						//read the file till reaches the end.
    	{
    		
    		ReadStandard.getline(MyStandard,MAXLENGTH);	// read a line from the file.(one line is a phrase)
    		Standard.push_back(MyStandard);				//push the phrase into the vector "Standard";
    	
    	}												//read standard words over
    	ReadStandard.close();							//close the file handle;
    
    	if(!WriteWord.is_open())
    	{
    		cerr<<"couldn't create file"<<endl;
    		exit(1);
    	}
    	if (!ReadWord)
    	{
    		cerr<<"not found1"<<endl;
    		exit (1);
    	}
    	
    	while(!ReadWord.eof())
    	{
    		
    		ReadWord.getline(MyWord,MAXLENGTH);
    		MyWords.push_back(MyWord);
    	
    	}
    	ReadWord.close();
    //	WriteWord.close();
    
    	
    	for(unsigned i=0;i<MyWords.size();i++) 
    	{
    		
    		for(unsigned j=0;j<Standard.size();j++)  //each MyWords compare to the Standard and see if it is in the standard vector
    		{
    			if(MyWords[i]==Standard[j])			// if it equal to one of the words in the standard vector 
    				MyWords[i]=MyWords[i]+"/";		// add the slash to the end;
    		}
    		
    		cout<<MyWords[i]<<endl;
    		i++;
    	}
    
    	for(unsigned j=0;j<MyWords.size();j++)		// for each word in MyWords vector  
    	{
    		WriteWord<<MyWords[j]<<endl;			// write it to the destination file
    	}
    	WriteWord.flush();							// flush the memory to ensure all is written to the file
    	WriteWord.close();							// close the write file handls
    	return 0;
    }
    This code is use to compare an input text file which is named as file.txt with a standard text file (Standar.txt), and see if the input file text matches with my standards, if matches then it will add an "/" after each match. But only if they are exactly the same, i.e there is a phrase "hello every one" in both texts it will write out "hello ever one/" after the process. But what I want to do is that if there is a phrase "hi, hello everyone" and in my standard I have the phrase "hello everyone" it will compared them and the write out "hi, hello everyone/". Can some one give me some clues of how can I make it work like that? Thx in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    look up the C string standard library functions, specifically strstr which finds the occurance of 1 string inside another.

    Comment

    • stealwings
      New Member
      • Feb 2007
      • 34

      #3
      Thx, I already fix my problem with the find function, but I'm not sure whether if it's the best way to do it so, can some of the experts plz check out my code and tell me if there is a better way to do what I have to do, thx in advance.THIS IS THE PART OF THE CODE THAT DOES THE SEARCH, COMPARE AND REPLACE.
      Code:
      //SEARCH AND COMPARE, THEN REPLACE IF MATCH IS TRUE.
      	for(unsigned i=0;i<MyWords.size();i++) 
      	{
      		
      		for(unsigned j=0;j<Standard.size();j++)  //each MyWords compare to the Standard and see if it is in the standard vector
      		{
      			string::size_type FoundAt=MyWords[i].find(Standard[j]);
      			while( string::npos != FoundAt )
      				{
      					MyWords[i].replace( FoundAt, Standard[j].length(),Standard[j]+"/");
      					cout<< MyWords[i]<< endl;
      					FoundAt = MyWords[i].find( Standard[j], FoundAt + Standard[j].length() );
      				}
      
      			//if(MyWords[i]==Standard[j])			// if it equal to one of the words in the standard vector 
      			//	MyWords[i]=MyWords[i]+"/";			// add the slash to the end;
      		}
      		
      	}

      Comment

      • stealwings
        New Member
        • Feb 2007
        • 34

        #4
        Hello everyone;
        I have a new question about this program that I couldn't solve myself yet. In the last post the function I use to search for my required word is too slow, takes at least a hundred years to finish comparing a large text, since it does a linear search. I know that using binary search it will speed up much much more, but here is where problem appears. To do a binary search I will need to cut the sentences of my input file into words then compare them with my Sorted Array. Well, the problem is that my sorted array does not only consist of words, but phrases, so I will have to take one word from the input and see if there is a match, if there is a match then take the second one and do the same until it reaches to the same phrase. Now my question is how do I do that, how do I take one word, compare it, and then take the second, and the third.....and so on without deleting the previous ones until I meet up with a the same phrase of my sorted array. Can some one give me some hints plz.

        Thanks in advance!!!!!!

        Comment

        • stealwings
          New Member
          • Feb 2007
          • 34

          #5
          Sorry if the last post for my question was too long, maybe a little example of what I'm looking for will be easier to be understand.
          e.g. Lets say I have a sentences "This world is full of beauty!" and I have in my sorted array the phrase "full of beauty", I would like to take each word of the sentence to and compare it with my array like the following:
          1. Take the word "This", since it doesn't match delete it
          2. Take the word "world", since it doesn't match delete it.
          3. and so on until "full", since "full" is part of my phrase "full of beauty" conserve it and take the next and compare it, and so on till I get the full phrase "full of beauty" then return that the phrase was found.

          Please help me out, this question is driving me crazy. Thank in advance! And sorry for all the trouble.

          Comment

          • stealwings
            New Member
            • Feb 2007
            • 34

            #6
            There isn't any body who can give me hand in this problem yet? Or shall I star a new discussions for my question to be answered?

            Comment

            • DeMan
              Top Contributor
              • Nov 2006
              • 1799

              #7
              This has now (at least partly) been answered in your other post....

              Comment

              • stealwings
                New Member
                • Feb 2007
                • 34

                #8
                Originally posted by DeMan
                This has now (at least partly) been answered in your other post....
                Yes, thank you DeMan :)

                Comment

                Working...