我设法让这个代码编译出错误。但不知何故,它并没有返回我在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