Created
September 2, 2015 06:06
-
-
Save yaswanthrajyadiki/f77697f4b7c75211f0e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ArrayList; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.util.StringTokenizer; | |
import java.util.Collections; | |
class DocumentOrdering { | |
List<String> fileNames; | |
List<String> fileContents; | |
List<String> words; | |
List<Integer> wordCount; | |
DocumentOrdering(List<String> fileNames, List<String> words) { | |
this.fileNames = fileNames; | |
this.words = words; | |
this.fileContents = new ArrayList<String>(); | |
this.wordCount = new ArrayList<Integer>(); | |
this.extractFileContent(); | |
} | |
public void extractFileContent() { | |
StringBuffer content = new StringBuffer(); | |
for (String fileName : fileNames) { | |
try { | |
BufferedReader br = new BufferedReader(new FileReader(fileName)); | |
String line; | |
while ((line = br.readLine()) != null) { | |
content.append(line); | |
} | |
fileContents.add(content.toString()); | |
br.close(); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} | |
public List<String> sortingDocs() { | |
int k = 0; | |
for (String content : fileContents) { | |
int count = 0; | |
StringTokenizer st = new StringTokenizer(content,".? \t\n\"!"); | |
while (st.hasMoreTokens()){ | |
String contentWord = st.nextToken(); | |
for (String word : words) { | |
if(contentWord.equalsIgnoreCase(word)) { | |
count++; | |
} | |
} | |
} | |
fileNames.set(k,fileNames.get(k) + "-" + count); | |
wordCount.add(count); | |
k++; | |
} | |
for (int i = 0; i < wordCount.size(); i++) { | |
for (int j = 0; j < wordCount.size(); j++) { | |
if (wordCount.get(i) > wordCount.get(j)) { | |
Collections.swap(fileNames,i,j); | |
} | |
} | |
} | |
return fileNames; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ArrayList; | |
class DocumentOrderingDemo { | |
public static void main(String[] args) { | |
List<String> filenames = new ArrayList<String>(); | |
List<String> words = new ArrayList<String>(); | |
filenames.add("Document1.txt"); | |
filenames.add("Document2.txt"); | |
filenames.add("Document3.txt"); | |
filenames.add("Document4.txt"); | |
words.add("Animal"); | |
words.add("fossil"); | |
words.add("animals"); | |
words.add("types"); | |
DocumentOrdering documents = new DocumentOrdering(filenames, words); | |
List<String> sortedFilenames = documents.sortingDocs(); | |
for (String filename : sortedFilenames) { | |
System.out.println(filename); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment