Page 1 of 3 123 LastLast
Results 1 to 20 of 41

Thread: Help me,Please

  1. #1
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default Help me,Please

    I want to compare to text files.I will have to compare that a single sentence of one file is matches or not.Let,one text file is copied from another.Then sentences of one file is rearranged.Then output must be 100%.
    how to write this code??

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Read line1 of file1 and line1 of file2 and compare them. If they match, read line2 of the files. Do that until you reach the end of the files, or one file.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    If you want a bit more advanced algorithm have a look at the diff algorithm.

    kind regards,

    Jos

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by PhHein View Post
    Read line1 of file1 and line1 of file2 and compare them. If they match, read line2 of the files. Do that until you reach the end of the files, or one file.
    If the files are fairly small then I suggest to read the first file into the memory, to a list, and compare with lines in the second file.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Learn how to read in a text file, it should be elementary from there.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    By searching the forum he/she can find lots of related thread, for comparing contents like this.

  7. #7
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    al_Marshy_1981,I know that how read from .txt file.My problem is not that.


    Let's see.

    text file 1:
    This is Bangladesh.Bangladesh is a beautiful country.I like it very much.I proud to be a bangladeshi.

    text file 1:
    Bangladesh is a beautiful country.I proud to be a bangladeshi.This is Bangladesh.I like it very much.



    See that sentences are same but position is changed.I have to write a code which will search it and For this example result of comparing must be 100% same.

    Anyone help me,please.
    Thanks everyone.

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    The two text files are not exactly the same are they?

    post what you are doing at the moment.

  9. #9
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    al_Marshy_1981,I know that how read from .txt file.My problem is not that.


    Let's see.

    text file 1:
    This is Bangladesh.Bangladesh is a beautiful country.I like it very much.I proud to be a bangladeshi.

    text file 1:
    Bangladesh is a beautiful country.I proud to be a bangladeshi.This is Bangladesh.I like it very much.



    See that sentences are same but position is changed.I have to write a code which will search it and For this example result of comparing must be 100% same.

    Anyone help me,please.
    Thanks everyone.
    Oooh. I'd go with Eranga's suggestion. Store both files in an array (arr1 and arr2). Then, using nested loops, compare each line of arr1 with arr2. If this isn't a homework assignment and you really want to shoot for perfection, go with JosAH's suggestion.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    The two text files are not exactly the same are they?
    Seems to me both files are identical relevant to the content. Only difference is the line arrangement is different.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Oooh. I'd go with Eranga's suggestion. Store both files in an array (arr1 and arr2). Then, using nested loops, compare each line of arr1 with arr2. If this isn't a homework assignment and you really want to shoot for perfection, go with JosAH's suggestion.
    Yes, But I'm saying that again. With a huge files it's not much better. You must go with another way, through a database is much effective. But you had to have a mechanism to upload the file content to the database as well.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    Anyone help me,please.
    Thanks everyone.
    I've give you a suggestion in my one the previous post. Did you try to do that? Show your effort here, if you stuck on anything we'll help you. :)

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Oooh. I'd go with Eranga's suggestion. Store both files in an array (arr1 and arr2). Then, using nested loops, compare each line of arr1 with arr2. If this isn't a homework assignment and you really want to shoot for perfection, go with JosAH's suggestion.
    If both files are (linewise) permutations of each other or nearly linewise permutations I'd store them both in (two) Lists and use the retainAll( ... ) and/or removeAll( ... ) methods ... I'm lazy ;-)

    kind regards,

    Jos

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  15. #15
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I have written a small program. This is a simple example of that what you are want. Try to improve this program, or write new on this base.

    Java Code:
    import java.util.Arrays;
    import java.util.List;
    
    public class Sentences2 {
    	public static void main(String[] args) {
    		int matches = 0;
    		
    		String firstSentence = "My name.Your name.His name.";
    		String secondSentence = "Your name.His name.My name.";
    		
    		String[] firstArray = null;
    		String[] secondArray = null;
    		
    		firstArray = firstSentence.split("\\.");
    		secondArray = secondSentence.split("\\.");
    		
    		List<String> firstList = Arrays.asList(firstArray);
    		List<String> secondList = Arrays.asList(secondArray);
    		
    		for(int i = 0; i < firstList.size(); i++)
    			if(secondList.contains(firstList.get(i)))
    				matches++;
    				
    		
    		if(matches == firstList.size())
    			System.out.println("Match");
    		else
    			System.out.println("Does not match");
    	}
    }




    We have 2 texts:
    Java Code:
    String firstSentence = "My name.Your name.His name.";
    String secondSentence = "Your name.His name.My name.";



    We want to separate sentences from this texts. So we use function split(), and regular expression to make a new sentence when he finds a dot.
    Then we save everything in String array.
    Java Code:
    	firstArray = firstSentence.split("\\.");
    		secondArray = secondSentence.split("\\.");

    Then I want to convert arrays to Lists :D
    Java Code:
    List<String> firstList = Arrays.asList(firstArray);
    List<String> secondList = Arrays.asList(secondArray);


    Then we loop first text sentence by sentence and try to find that sentences on second text. If two sentences is matched, then we increment number of matches.
    Java Code:
    for(int i = 0; i < firstList.size(); i++)
    			if(secondList.contains(firstList.get(i)))
    				matches++;


    If there is same number of matches as same number of sentences then two text are the same.
    Java Code:
    if(matches == firstList.size())
    			System.out.println("Match");
    		else
    			System.out.println("Does not match");


    Please do not use this program as your final version because it has few deficiencies. Try to fix it and improve it.
    Last edited by cselic; 06-26-2010 at 03:23 PM. Reason: typos

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by cselic View Post
    Please do not use this program as your final version because it has few deficiencies.
    You can say that again; suppose the second file contains the sentences A, B and C while the first file contains the sentences A, A, and A. Three matches are found so your algorithm concludes that both files are equal (given a permutation).

    kind regards,

    Jos

  17. #17
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Thanks to everyone for spending time for me.
    Now I am gonna to understand cselic's code and then implement it on mine one.

  18. #18
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    If both files are (linewise) permutations of each other or nearly linewise permutations I'd store them both in (two) Lists and use the retainAll( ... ) and/or removeAll( ... ) methods ... I'm lazy ;-)

    kind regards,

    Jos
    :O!

    You learn something everyday.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  19. #19
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    You can say that again; suppose the second file contains the sentences A, B and C while the first file contains the sentences A, A, and A. Three matches are found so your algorithm concludes that both files are equal (given a permutation).
    You are right. It's better to work with remove(index), instead of variable match.

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    Thanks to everyone for spending time for me.
    Now I am gonna to understand cselic's code and then implement it on mine one.
    Think about what Jos pointed, that code not working completely as you want. It's better if you can find that and solve it.

    More than that, Jos gives you the logic in one of his previous post. Try to workout on that. So you can lean a lot, while solving the problem.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •