我設法讓這個代碼編譯出錯誤。但不知何故,它並沒有返回我在file1.txt和file.txt中寫入的字符串,我將它的路徑傳遞給str1和str2。我的目標是使用這個開源庫來測量包含在2個文本文件中的字符串之間的相似度。fileToSTring不斷返回「」
在它的Javadoc內,其指出...
public static java.lang.StringBuffer fileToString(java.io.File f)
private call to load a file and return its content as a string.
Parameters:
f - a file for which to load its content
Returns:
a string containing the files contents or "" if empty or not present
這裏是我修改後的代碼試圖利用FileLoader功能,但未能歸還文件中的字符串。最終的結果不斷讓我回到「」。我不知道哪裏是我的錯:
package uk.ac.shef.wit.simmetrics;
import java.io.File;
import uk.ac.shef.wit.simmetrics.similaritymetrics.*;
import uk.ac.shef.wit.simmetrics.utils.*;
public class SimpleExample {
public static void main(final String[] args) {
if(args.length != 2) {
usage();
} else {
String str1 = "arg[0]";
String str2 = "arg[1]";
File objFile1 = new File(str1);
File objFile2 = new File(str2);
FileLoader obj1 = new FileLoader();
FileLoader obj2 = new FileLoader();
str1 = obj1.fileToString(objFile1).toString();
str2 = obj2.fileToString(objFile2).toString();
System.out.println(str1);
System.out.println(str2);
AbstractStringMetric metric = new MongeElkan();
//this single line performs the similarity test
float result = metric.getSimilarity(str1, str2);
//outputs the results
outputResult(result, metric, str1, str2);
}
}
private static void outputResult(final float result, final AbstractStringMetric metric, final String str1, final String str2) {
System.out.println("Using Metric " + metric.getShortDescriptionString() + " on strings \"" + str1 + "\" & \"" + str2 + "\" gives a similarity score of " + result);
}
private static void usage() {
System.out.println("Performs a rudimentary string metric comparison from the arguments given.\n\tArgs:\n\t\t1) String1 to compare\n\t\t2)String2 to compare\n\n\tReturns:\n\t\tA standard output (command line of the similarity metric with the given test strings, for more details of this simple class please see the SimpleExample.java source file)");
}
}
更新:我已經修改了代碼,但它給了我這個錯誤:
SimpleExample.java:79: cannot find symbol
symbol : variable arg
location: class uk.ac.shef.wit.simmetrics.SimpleExample
String str1 = arg[0];
^
SimpleExample.java:80: cannot find symbol
symbol : variable arg
location: class uk.ac.shef.wit.simmetrics.SimpleExample
String str2 = arg[1];
^
@karikari,我有一個錯字,嘗試'args'而不是'arg'。另外,FileLoader類從哪裏來? – 2010-04-26 00:47:56
@anthony,FileLoader類來自simmetric.jar,一個開源的庫。 – karikari 2010-04-26 03:57:07