import java.awt.BorderLayout;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.io.FilenameFilter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.apache.commons.io.FileUtils;
@SuppressWarnings("unused")
public class readFile extends JFrame {
JTextArea _resultArea = new JTextArea(100, 100);
JScrollPane scrollingArea = new JScrollPane(_resultArea);
final String newline = "\n";
public readFile(){
File folder = new File("C:\\Users\\user\\fypworkspace\\FYP");
File[] listOfFiles = folder.listFiles();
int numDoc = 0;
for (int i = 0; i < listOfFiles.length; i++) {
if ((listOfFiles[i].getName().endsWith(".txt"))) {
numDoc++;
}
}
System.out.println("The number of files is this folder is : " + numDoc);
// Calculating term frequency
int filename = 11;
String[] fileName = new String[filename];
int a = 0;
int totalCount = 0;
int wordCount = 0;
// Count the number of documents containing the query
System.out.println("Please enter the query :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan2.nextLine();
String[] array2 = word2.split(" ");
int[] numofDoc = new int[array2.length];
for (int b = 0; b < array2.length; b++) {
numofDoc[b] = 0;
for (int i = 0; i < filename; i++) {
try {
BufferedReader bf = new BufferedReader(new FileReader(
"C:\\Users\\user\\fypworkspace\\FYP\\abc"
+ i + ".txt"));
int matchedWord = 0;
Scanner s2 = new Scanner(bf);
{
while (s2.hasNext()) {
if (s2.next().equals(array2[b]))
matchedWord++;
}
}
if (matchedWord > 0)
numofDoc[b]++;
} catch (IOException e) {
System.out.println("File not found.");
}
}
_resultArea.append(array2[b]
+ " --> This number of files that contain this term "
+ numofDoc[b]+ newline);
}
// calculate TF-IDF (TermFrequency/InverseTermFrequency)
int queryVector = 1;
double similarity = 0.0;
int wordPower;
double[] similarityScore = new double [11];
double[][] arrays = new double[11][1];
for (a = 0; a < filename; a++) {
int totalwordPower = 0;
int totalWords = 0;
try {
_resultArea.append(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ "+ newline);
_resultArea.append("\n"+ newline);
_resultArea.append("The word inputted : " + word2+ newline);
File file = new File(
"C:\\Users\\user\\fypworkspace\\FYP\\abc"
+ a + ".txt");
_resultArea.append(" _________________"+ newline);
_resultArea.append("| File = abc" + a + ".txt | \t\t \n"+ newline);
for (int i = 0; i < array2.length; i++) {
totalCount = 0;
wordCount = 0;
Scanner s = new Scanner(file);
{
while (s.hasNext()) {
totalCount++;
if (s.next().equals(array2[i]))
wordCount++;
}
_resultArea.append(array2[i] + " --> Word count = "
+ "\t " + "|" + wordCount + "|"+ newline);
_resultArea.append(" Total count = " + "\t " + "|"
+ totalCount + "|"+ newline);
System.out.printf(" Term Frequency = | %8.4f |",
(double) wordCount / totalCount );
_resultArea.append("\t "+ newline);
double inverseTF = Math.log10((float) numDoc
/ (numofDoc[i]));
_resultArea.append(" --> IDF = " + inverseTF+ newline);
double TFIDF = (((double) wordCount / totalCount) * inverseTF);
_resultArea.append(" --> TF/IDF = " + TFIDF + "\n"+ newline);
totalWords += wordCount;
wordPower = (int) Math.pow(wordCount, 2);
totalwordPower += wordPower;
_resultArea.append("Document Vector : " + wordPower+ newline);
similarity = (totalWords * queryVector)
/ ((Math.sqrt((totalwordPower)) * (Math
.sqrt(((queryVector * 3))))));
similarityScore[a] = similarity;
}
}
} catch (FileNotFoundException e) {
System.out.println("File is not found");
}
_resultArea.append("The total query frequency for this file is "
+ totalWords+ newline);
_resultArea.append("The total document vector : " + totalwordPower+ newline);
_resultArea.append("The similarity is " + similarity+ newline);
_resultArea.append("\n"+ newline);
}
_resultArea.append(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ "
+ "\n"+ newline);
// Display the similarity 开发者_开发技巧score between the query and the document in a
// sorted form
_resultArea.append("The resulting similarity score of the query " + word2+ newline);
for (a = 0; a < filename; a++) {
_resultArea.append("abc" + a + ".txt = " + similarityScore[a]+ newline);
}
_resultArea.append("\n"+ newline);
//Array of sorted similarity score
for( int i=0; i<arrays.length; i++){
Arrays.sort(similarityScore);
arrays[i][0] = similarityScore[i] ;
}
for(int row = 0; row<11; row++){
for( int col=0; col<1; col ++){
_resultArea.append(arrays[row][col]+"\t"+ newline);
}
_resultArea.append("\n"+ newline);
}
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
this.setContentPane(content);
this.setTitle("TextAreaDemo B");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
public static void main(String[] args) throws FileNotFoundException {
JFrame win = new readFile();
win.setVisible(true);
}
}
Hi, i could not append this line
System.out.printf(" Term Frequency = | %8.4f |",
(double) wordCount / totalCount );
to the JTextArea. What do i missing ?
I try to change to
_resultArea.append(" Term Frequency = | %8.4f |",
(double) wordCount / totalCount );
but to no avail. :(
printf is not the same as println as the former formats the String in the same way that String.format(...) does, and this is how you solve it, call format on the String:
_resultArea.append(String.format(" Term Frequency = | %8.4f |", (double) wordCount / totalCount));
For more on this, check out the String API and the Formatter API.
An aside, if you have a similar question in the future, don't paste so much code since most of the code you've posted above is completely unrelated to the problem at hand. The best code to post is an SSCCE, and this link will tell you all about this: SSCCE
精彩评论