将 .txt 文件(ANSI 编码)转换为 .Arff 而不会丢失重音

问题描述 投票:0回答:1

我很难找到如何将 ANSI 编码中的 .txt 文件转换为 weka 中的 .arff 文件,而不会在此过程中丢失一些重音和单词的含义。我正在阅读西班牙语文章,问题是带有重音的单词转换不好,因为带有重音的字母是这样转换的。

我原来的.txt | .arff 文件转换结果

矿产 |矿山

字母“í”在这个过程中丢失了。

我现在的代码是这样的(weka大学提供的代码)

public Instances createDataset(String directoryPath) throws Exception {

FastVector atts = new FastVector(2);
atts.addElement(new Attribute("filename", (FastVector) null));
atts.addElement(new Attribute("contents", (FastVector) null));
Instances data = new Instances("text_files_in_" + directoryPath, atts, 0);

File dir = new File(directoryPath);
String[] files = dir.list();
for (int i = 0; i < files.length; i++) {
  if (files[i].endsWith(".txt")) {
try {
  double[] newInst = new double[2];
  newInst[0] = (double)data.attribute(0).addStringValue(files[i]);
  File txt = new File(directoryPath + File.separator + files[i]);
  // meto codigo nuevo aqui dentro


  // hasata aqui
  InputStreamReader is;
  is = new InputStreamReader(new FileInputStream(txt));
  StringBuffer txtStr = new StringBuffer();
  int c;
  while ((c = is.read()) != -1) {
    txtStr.append((char)c);
    // s pstir de aqui contamino yo el codigo
     // System.out.println("Sale " + is.toString();

  }
  newInst[1] = (double)data.attribute(1).addStringValue(txtStr.toString());
  data.add(new Instance(1.0, newInst));
} catch (Exception e) {
  //System.err.println("failed to convert file: " + directoryPath + File.separator + files[i]);
}
  }
}
return data;

}

我正在使用 Netbeans 从计算机中的文件中转换文件。 你可能认为我在这个页面的其他帖子中问了同样的问题,但实际上我不是,因为我真正需要的是一个可以正确转换西班牙语口音的转换器。

我试图将 Netbeans 中的编码更改为 UTF-8 和 ANSI,但没有一个解决方案对我有用(我转到 Netbeans8.1 中的配置文件 --> etc --> netbeans.conf 并添加netbeans_default_options=.........行中有 -J-Dfile.encoding=UTF-8 但仍然不起作用)。我对这个问题有点沮丧。

好吧,我在失去理智后找到了部分解决方案。事实上,这个解决方案并不是一个真正的解决方案,所以我希望有一天有人会回答一些可能拯救数据挖掘世界的问题。解决方案包括将文本保存为不带 BOM 的 UTF-8 (UTF-8 sin BOM)。您还必须配置 Netbeans 以读取 UTF8,如我上面所解释的。

java netbeans weka arff
1个回答
0
投票

我遇到了这个问题,我的解决方案是编码为 ANSI。

我用记事本++

步骤:

  • 打开文件

  • 转到顶部面板

  • Enconding -> ANSI 编码

© www.soinside.com 2019 - 2024. All rights reserved.