Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



Compare two documents with Map

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 26-02-2010
Member
 
Join Date: Dec 2009
Posts: 292
Compare two documents with Map

Hello
I am asked to write a program that compares the similarity of 2 text files in "mapping" the occurrence of each word that appears, being the key words and values the number of times each word appears. While I understand the principle of the map but to be honest, I do not know what to write as code to implement it It has a few examples but are limited to words and not entirely files. In short I have to compare two documents using the map. do you have any idea about this? I do not even know that it can be done in java. Please help.
Reply With Quote
  #2  
Old 26-02-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: Compare two documents with Map

Hello,
To view the files in a directory are:
Code:
File f =new File("rep");
File [] files;
if(f.isDirectory()){
   files = dir.listFiles();
}
Do not make a map of <string,string> but a map of <String,Integer>
Then for each file to:
- Read the file line by line (BufferedReader and FileReader)
- For each line, split into words (see StringBuilder StringTokenizer or regex)
- For each word if the word is not present in the map, do an "add (word, 1) otherwise do a get (word).
Reply With Quote
  #3  
Old 26-02-2010
Member
 
Join Date: Nov 2008
Posts: 240
Re: Compare two documents with Map

Hey
I also want to try a program in this way. I have done some work on this , just check it out and it I am wrong the guide me with the correct one.
Code:
Public class DocMap {
	
	Public static void hand (String[] args) throws IOException {
	
		File fl = new File(". / text /");
Map(fl);
	}
	
	Public static void Map (File fl2) throws IOException{
		
	try {	
	FileReader fr = new FileReader(fl2);
BufferedReader buff = new BufferedReader(en);
String s = buff.readLine();

String ln =null;
			while ((ln = buff.readLine())!=null) {
				StringTokenizer st =new StringTokenizer(ln);
Map <String,Integer> = m new HashMap <String,Integer>();
			}
		}
		
		catch (IOException e) {
			System.out.System.out.println("Standard"+ e);
		}
	}
}
Reply With Quote
  #4  
Old 26-02-2010
Member
 
Join Date: Dec 2009
Posts: 292
Re: Compare two documents with Map

Hello,
We were told to indicate the directory with the new File ( ". / Text /"), texts are in the same directory as the program that accesses it. By cons what you mean with the attribute "r"? What I understand is that if I do a FileReader to a file in the directory, I have no problem with access. I do something wrong somewhere in my program. If I add a few lines of code in my hand that I list the file directory r1, it goes Show me the 5 text files that are in the directory. So the problem is the:
Code:
 Public static void Map (File fl) throws IOException{
Do you have any idea about this. Thanks in advance.
Reply With Quote
  #5  
Old 26-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
Re: Compare two documents with Map

Hello,
I bet you test to open and read the directory while it is inside the files to be read. If it is, instead of "new FileReader (fl)" do:
Code:
for (int i =0; i <dirls.length; i + +) {
   BufferedReader rd =new BufferedReader(new FileReader(dirls[i]));
   ...
}
I think you should try the following code. Just try it and compile it and if you get any error then post back with the error you get.
Reply With Quote
  #6  
Old 26-02-2010
Member
 
Join Date: Nov 2009
Posts: 331
Re: Compare two documents with Map

Hello,
I have a code with me , you can have a look at it, This may help you
Code:
File dir =new File("mydir");
if(! dir.isDirectory()){
   System.err.System.out.println(dir.getAbsolutePath()+"Not a dir.");
   System.exit(1);
}
File [] flls = dir.listFiles();
for(int i =0; i <flls.length; i + +){
   if(! flls[i].isFile()){
      System.out.System.out.println(flls[i].getAbsolutePath()+"Not a file, we pass to the next item.");
   }else{
      System.out.System.out.println("It treats the file"+ flls[i].getAbsolutePath());
      try{
         BufferedReader rd =new BufferedReader(new FileReader(flls[i]));
         Sring line =null;
         while((line = rd.readLine())!=null){
            System.out.System.out.println(row);
         }
         rd.close();
      }catch(IOException e){
         System.err.System.out.println("Error reading file"+ flls[i].getAbsolutePath()+" : "+ e.function toString() {
    [native code]
}());
         e.printStackTrace();
      }
   }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Compare two documents with Map"
Thread Thread Starter Forum Replies Last Post
Premium version Documents To Go in Blackberry OS7 Vs. Free versions of Documents To Go Grama Portable Devices 8 28-11-2011 08:41 AM
How to limit the number of documents shown in Start / Documents baker127 Windows XP Support 2 01-03-2011 07:05 PM
Convert works documents to word documents Arzaan Tips & Tweaks 3 04-03-2009 06:46 PM
Access is Denied. C\Documents and Settings\All Users\Documents Sch00017 Windows Security 2 24-01-2007 11:13 PM
Access Denied. C:\Documents and Settings\All Users\Documents Sch00017 Windows Security 0 24-01-2007 11:51 AM


All times are GMT +5.5. The time now is 12:06 PM.