我在序列化方面遇到了一些问题
这是我将 mClassifier 对象写入文件的代码:
FileOutputStream fileOut = new FileOutputStream("C:\\polarity.model");
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
mClassifier.compileTo(objOut);
objOut.close();
它工作正常并将内容写入文件。
但是有一个问题:myClassifier
object 是 type DynamicLMClassifier
。compileTo
然而,上面的方法返回一个 LMClassifier 的实例(超类)
这是我读取对象的代码:
FileInputStream in = new FileInputStream("C:\\polarity.model");
ObjectInputStream ois = new ObjectInputStream(in);
mClassifier = (DynamicLMClassifier)(ois.readObject());
ois.close();
当我读取对象时,我将其键入DynamicLMClassifier
,它也可以正常工作,但我没有得到我想要的输出。再次读取对象时,不应该将其类型转换为LMClassifier
而不是DynamicLMClassifier
. 但是,如果我这样做,编译器会抱怨它应该是 type DynamicLMClassifier
。
以上可能是问题还是我在其他地方做错了什么。我的意思是没有序列化的代码工作得很好,我得到了想要的输出,我的意思是当对象在内存中时。
编辑:这是完整的代码(只需删除train()
andgetSentiments()
方法中的序列化部分,它就可以按预期工作),还要注意在(1)中我没有调用序列化getSentiments()
,我只是在训练,即调用 train () 方法( 2) 现在我在 (1) 之后有一个序列化模型,并且我没有通过仅在 main 中注释掉适当的代码来调用该train()
方法:getSentiment()
public class PolarityBasic{
File mPolarityDir;
String[] mCategories;
DynamicLMClassifier<NGramProcessLM> mClassifier,readClassifier;
PolarityBasic(String[] args) {
System.out.println("\nBASIC POLARITY DEMO");
mPolarityDir = new File("C:\\review_polarity","txt_sentoken");
System.out.println("\nData Directory=" + mPolarityDir);
mCategories = mPolarityDir.list();
int nGram = 8;
mClassifier
= DynamicLMClassifier
.createNGramProcess(mCategories,nGram);
}
void run() throws ClassNotFoundException, IOException {
train();
}
boolean isTrainingFile(File file) {
return file.getName().charAt(2) != '9'; // test on fold 9
}
void train() throws IOException {
int numTrainingCases = 0;
int numTrainingChars = 0;
System.out.println("\nTraining.");
for (int i = 0; i < mCategories.length; ++i) {
String category = mCategories[i];
Classification classification
= new Classification(category);
File file = new File(mPolarityDir,mCategories[i]);
File[] trainFiles = file.listFiles();
for (int j = 0; j < trainFiles.length; ++j) {
File trainFile = trainFiles[j];
if (isTrainingFile(trainFile)) {
++numTrainingCases;
String review = Files.readFromFile(trainFile,"ISO-8859-1");
numTrainingChars += review.length();
Classified<CharSequence> classified
= new Classified<CharSequence>(review,classification);
mClassifier.handle(classified);
}
}
}
FileOutputStream fileOut = new FileOutputStream("C:\\review_polarity/polarity.model");
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
mClassifier.compileTo(objOut);
objOut.close();
System.out.println(" # Training Cases=" + numTrainingCases);
System.out.println(" # Training Chars=" + numTrainingChars);
}
String getSentiment(String text) {
try{
FileInputStream in = new FileInputStream("C:\\review_polarity/polarity.model");
ObjectInputStream ois = new ObjectInputStream(in);
mClassifier = (DynamicLMClassifier)(ois.readObject());
ois.close();
}
catch(Exception e){}
Classification classification = null;
classification = readClassifier.classify(text);
System.out.println("classification: " + classification);
return (classification.bestCategory());
}
public static void main(String[] args) {
try {
PolarityBasic pB = new PolarityBasic(args);
pB.run();
String text = null;
text = "It was awesome !";
System.out.println("The text \"" + text + "\" is "
+ pB.getSentiment(text));
} catch (Throwable t) {
System.out.println("Thrown: " + t);
t.printStackTrace(System.out);
}
}
}