我有一个名为my-training.train
的文件。 (.train是open-nlp
)中培训文件的扩展名。该文件已填充了一些数据:
Refund What is the refund status for my order #342? Can I place a refund request for electronics?
NewOffers Are there any new offers for your products? Is there any new offer on buying worth 5000?
Refund Can I place a refund request for electronics?
NewOffers Is there any new offer on buying worth 5000?
Refund What is the refund status for my order #343? Can I place a refund request for electronics?
NewOffers Are there any new offers for your products? Is there any new offer on buying worth 5000?
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
我想以编程方式在下一行添加一个句子。这是我写的代码:
Writer output;
output = new BufferedWriter(new FileWriter("my-training.train",true));
output.append("NewOffers Please inform me of the new offers");
output.close();
句子按预期插入。显示培训文件的最后三行:
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offers
但是当我再次运行代码时,句子会插入到前一个句子的下一个而不是新行。 (最后三行):
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offersNewOffers Please inform me of the new offers
在上面的代码中,我将行output.append("NewOffers Please inform me of the new offers");
替换为output.append("\nNewOffers Please inform me of the new offers");
,但是使用它会在第一次追加时产生问题。
获取初始文件my-training.train
,这是最后三行的结果:
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offers
插入空行。这使训练文件无效。应该没有空行,每个句子都应插入新行。
为什么第一行与其他行之间存在歧视行为?我做错了什么?
答案 0 :(得分:0)
第二行在末尾插入一个新行。在缓冲区完全写入之前,close()
禁止将新行放在“请告知我新的报价”的末尾。
解决此问题的最佳方法是在flush()
和append()
之间插入close()
。这样,新行将在关闭之前写入文件。
我不是100%熟悉API。我不确定Writer
是否有flush方法,因此您可能必须在输出flush之前将输出类型更改为BufferedWriter
或将输出转换回BufferedWriter
。
如果flush没有改变任何内容,那么append()
不会向文件发送新行,因此将文本更改为"Please Inform me of new offers\n"
将完成此任务。