[英]Expanding abbreviations from .csv file
So I have this task to do, I need to expand text abbreviations from the message into their full form from the .csv file, I loaded that file into HashMap, with keys as abbreviations and values as full forms.所以我有这个任务要做,我需要将消息中的文本缩写扩展为 .csv 文件中的完整形式,我将该文件加载到 HashMap 中,键作为缩写,值作为完整形式。 There is a loop to iterate through the keys and if statement which replaces abbreviation to full form if it finds any.
有一个循环来遍历键和 if 语句,如果找到任何缩写,它会将缩写替换为完整形式。 I kind of figured it out and it is working as it should but I want to send this changed String (with abbreviations expanded) somewhere else out of if statement to save the full message to the file.
我有点想通了,它正在正常工作,但我想将这个更改后的字符串(扩展了缩写)发送到 if 语句之外的其他地方,以将完整消息保存到文件中。 I know that this String exists only in this if statement but maybe there is another way of doing it?
我知道这个字符串只存在于这个 if 语句中,但也许还有另一种方法呢? Or maybe I'm doing something wrong?
或者也许我做错了什么? I became a bit rusty with Java so maybe there is a simple explanation that I don't know about.
我对 Java 有点生疏了,所以也许有一个我不知道的简单解释。 Here is the code I have :
这是我的代码:
public class AbbreviationExpander {
static void AbrExpander(String messageBody) {
//read the .csv file
String csvFile = "textwords.csv";
String line = "";
String cvsSplitBy = ",";
String bodyOut = messageBody;
HashMap<String, String> list = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
String[] abbreviatonFile = line.split(cvsSplitBy);
//load the read data into the hashmap
list.put(abbreviatonFile[0], abbreviatonFile[1]);
}
for (String key : list.keySet()) {
//if any abbreviations found then replace them with expanded version
if (messageBody.contains(key)) {
bodyOut = bodyOut.replace(key, key + "<" + list.get(key).toLowerCase() + ">");
try {
File file = new File("SMS message" + System.currentTimeMillis() + ".txt");
FileWriter myWriter = new FileWriter(file);
myWriter.write(bodyOut);
myWriter.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
} catch (IOException f) {
f.printStackTrace();
}
}
}
Not sure I understood well your problem.不确定我是否理解你的问题。 But I think you should separate the different steps in your code.
但我认为您应该将代码中的不同步骤分开。
I mean your try-catch
block that writes your output should be outside the for-loop
and outside the reading try-catch
.我的意思是,写入输出的
try-catch
块应该在for-loop
之外并在读取try-catch
。 And your for-loop
should be outside your reading try-catch
.你的
for-loop
应该在你的阅读try-catch
。
public class AbbreviationExpander {
static void AbrExpander(String messageBody) {
String csvFile = "textwords.csv";
String line = "";
String cvsSplitBy = ",";
String bodyOut = messageBody;
HashMap<String, String> list = new HashMap<>();
//read the .csv file
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
String[] abbreviatonFile = line.split(cvsSplitBy);
//load the read data into the hashmap
list.put(abbreviatonFile[0], abbreviatonFile[1]);
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
//if any abbreviations found then replace them with expanded version
for (String key : list.keySet()) {
if (messageBody.contains(key)) {
bodyOut = bodyOut.replace(key, key + "<" + list.get(key).toLowerCase() + ">");
}
}
//output the result in your file
try {
File file = new File("SMS message" + System.currentTimeMillis() + ".txt");
FileWriter myWriter = new FileWriter(file);
myWriter.write(bodyOut);
myWriter.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.