|
| |||||||||
| Tags: comparing two maps, documents, hasmap, map, vectors |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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. |
|
#2
| |||
| |||
| 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();
} 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). |
|
#3
| |||
| |||
| 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);
}
}
} |
|
#4
| |||
| |||
| 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{ |
|
#5
| |||
| |||
| 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]));
...
} |
|
#6
| |||
| |||
| 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();
}
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |