public static Map<String,String> getCNPunctuation(Map<String,String> summary){ String title = summary.get("title"); String metaContent = summary.get("meta_content"); // 定义英文标点和中文标点的对应关系 Map<String, String> punctuations = new HashMap<>(); punctuations.put(",", ","); punctuations.put(".", "。"); punctuations.put("!", "!"); punctuations.put("?", "?"); punctuations.put(":", ":"); punctuations.put(";", ";"); punctuations.put("(", "("); punctuations.put(")", ")"); // 遍历 Map,将字符串中的英文标点替换成中文标点 for (Map.Entry<String, String> entry : punctuations.entrySet()) { String enPunc = entry.getKey(); String cnPunc = entry.getValue(); title = title.replace(enPunc, cnPunc); metaContent = metaContent.replace(enPunc,cnPunc); } summary.put("title",title); summary.put("meta_content",metaContent); return summary; }这段代码传入的summary,我想加如一一个判断,如果句子末尾是省略号则不把.转换成。该如何实现
时间: 2024-03-05 08:52:18 浏览: 135
你可以在遍历 Map 替换标点前,先对句子末尾是否是省略号进行判断,如果是省略号,则不将英文句点替换为中文句点。下面是示例代码:
```
public static Map<String,String> getCNPunctuation(Map<String,String> summary){
String title = summary.get("title");
String metaContent = summary.get("meta_content");
// 定义英文标点和中文标点的对应关系
Map<String, String> punctuations = new HashMap<>();
punctuations.put(",", ",");
punctuations.put(".", "。");
punctuations.put("!", "!");
punctuations.put("?", "?");
punctuations.put(":", ":");
punctuations.put(";", ";");
punctuations.put("(", "(");
punctuations.put(")", ")");
// 判断句子末尾是否为省略号
boolean titleEndsWithEllipsis = title.endsWith("...");
boolean metaContentEndsWithEllipsis = metaContent.endsWith("...");
// 遍历 Map,将字符串中的英文标点替换成中文标点
for (Map.Entry<String, String> entry : punctuations.entrySet()) {
String enPunc = entry.getKey();
String cnPunc = entry.getValue();
// 如果句子末尾是省略号,则不将英文句点替换为中文句点
if (enPunc.equals(".") && (titleEndsWithEllipsis || metaContentEndsWithEllipsis)) {
continue;
}
title = title.replace(enPunc, cnPunc);
metaContent = metaContent.replace(enPunc,cnPunc);
}
summary.put("title",title);
summary.put("meta_content",metaContent);
return summary;
}
```
这样,如果输入的句子末尾是省略号,英文句点将不会被替换为中文句点。
阅读全文