|
|
@@ -17,7 +17,7 @@ import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
- * @description:
|
|
|
+ * @description: 文件地址可以配置到yml中,避免线上线下不一致
|
|
|
* @author: Guos
|
|
|
* @time: 2025/12/5 下午3:07
|
|
|
*/
|
|
|
@@ -27,6 +27,7 @@ public class DrugComponentAnalyzer {
|
|
|
private static final Set<String> FORBIDDEN_COMPONENTS = new HashSet<>(Arrays.asList(
|
|
|
"罂粟壳", "吗啡", "海洛因", "可卡因", "芬太尼","罂粟","洋地黄毒苷"
|
|
|
));
|
|
|
+
|
|
|
// 停用词集合
|
|
|
// private static Set<String> STOP_WORDS = new HashSet<>(Arrays.asList(
|
|
|
// "洋地黄毒苷"
|
|
|
@@ -37,10 +38,10 @@ public class DrugComponentAnalyzer {
|
|
|
|
|
|
// 词典文件绝对路径(核心:直接指向服务器本地文件)
|
|
|
private static final String DRUG_FILE_PATH = "C:\\Tools\\jar\\drug.txt";
|
|
|
+
|
|
|
// 词典数据集合
|
|
|
private static final Set<String> DRUG_DICT = new HashSet<>();
|
|
|
|
|
|
- // 整合后的静态代码块:一次性完成「加载WordDictionary」+「读取DRUG_DICT」
|
|
|
static {
|
|
|
try {
|
|
|
// 1. 校验文件是否存在
|
|
|
@@ -48,27 +49,19 @@ public class DrugComponentAnalyzer {
|
|
|
if (!drugFile.exists()) {
|
|
|
throw new FileNotFoundException("词典文件不存在:" + DRUG_FILE_PATH);
|
|
|
}
|
|
|
-
|
|
|
// 2. 核心逻辑:给 WordDictionary 加载词典文件(依赖 Path)
|
|
|
Path path = drugFile.toPath();
|
|
|
WordDictionary.getInstance().loadUserDict(path);
|
|
|
-
|
|
|
-
|
|
|
+ //这个是线下的使用方式
|
|
|
+// ClassPathResource resource = new ClassPathResource("drug.txt");
|
|
|
+// System.out.println("文件路径:"+resource.getFile().getPath());
|
|
|
+// Path linePath = resource.getFile().toPath();
|
|
|
+// WordDictionary.getInstance().loadUserDict(linePath);
|
|
|
} catch (Exception e) {
|
|
|
- // 统一抛出异常,包含完整错误信息
|
|
|
throw new RuntimeException("加载词典失败(路径:" + DRUG_FILE_PATH + ")", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 对外提供文件路径的方法(兼容原有依赖path的逻辑)
|
|
|
- public static String getDrugFilePath() {
|
|
|
- return DRUG_FILE_PATH;
|
|
|
- }
|
|
|
-
|
|
|
- // 可选:对外提供词典集合(如果业务需要)
|
|
|
- public static Set<String> getDrugDict() {
|
|
|
- return DRUG_DICT;
|
|
|
- }
|
|
|
/**
|
|
|
* 拆分药品成分(过滤停用词,仅保留有效成分)
|
|
|
*/
|
|
|
@@ -130,26 +123,26 @@ public class DrugComponentAnalyzer {
|
|
|
public boolean isForbidden() { return isForbidden; }
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- // 测试文本1:含禁售成分
|
|
|
- String text1 = "洋地黄毒苷,左啡诺";
|
|
|
- CheckResult result1 = checkForbiddenComponents(FORBIDDEN_COMPONENTS, text1);
|
|
|
- System.out.println("文本1拆分成分:" + result1.getAllComponents());
|
|
|
- System.out.println("文本1禁售成分:" + result1.getForbiddenComponents());
|
|
|
- System.out.println("文本1是否禁售:" + result1.isForbidden());
|
|
|
- // 输出:
|
|
|
- // 文本1拆分成分:[对乙酰氨基酚, 罂粟壳, 淀粉]
|
|
|
- // 文本1禁售成分:[罂粟壳]
|
|
|
- // 文本1是否禁售:true
|
|
|
-
|
|
|
- // 测试文本2:无禁售成分
|
|
|
- String text2 = "阿莫西林克拉维酸钾分散片,辅料为硬脂酸镁";
|
|
|
- CheckResult result2 = checkForbiddenComponents(FORBIDDEN_COMPONENTS, text2);
|
|
|
- System.out.println("\n文本2拆分成分:" + result2.getAllComponents());
|
|
|
- System.out.println("文本2是否禁售:" + result2.isForbidden());
|
|
|
- // 输出:
|
|
|
- // 文本2拆分成分:[阿莫西林克拉维酸钾, 硬脂酸镁]
|
|
|
- // 文本2是否禁售:false
|
|
|
- }
|
|
|
+// public static void main(String[] args) {
|
|
|
+// // 测试文本1:含禁售成分
|
|
|
+// String text1 = "洋地黄毒苷,左啡诺";
|
|
|
+// CheckResult result1 = checkForbiddenComponents(FORBIDDEN_COMPONENTS, text1);
|
|
|
+// System.out.println("文本1拆分成分:" + result1.getAllComponents());
|
|
|
+// System.out.println("文本1禁售成分:" + result1.getForbiddenComponents());
|
|
|
+// System.out.println("文本1是否禁售:" + result1.isForbidden());
|
|
|
+// // 输出:
|
|
|
+// // 文本1拆分成分:[对乙酰氨基酚, 罂粟壳, 淀粉]
|
|
|
+// // 文本1禁售成分:[罂粟壳]
|
|
|
+// // 文本1是否禁售:true
|
|
|
+//
|
|
|
+// // 测试文本2:无禁售成分
|
|
|
+// String text2 = "阿莫西林克拉维酸钾分散片,辅料为硬脂酸镁";
|
|
|
+// CheckResult result2 = checkForbiddenComponents(FORBIDDEN_COMPONENTS, text2);
|
|
|
+// System.out.println("\n文本2拆分成分:" + result2.getAllComponents());
|
|
|
+// System.out.println("文本2是否禁售:" + result2.isForbidden());
|
|
|
+// // 输出:
|
|
|
+// // 文本2拆分成分:[阿莫西林克拉维酸钾, 硬脂酸镁]
|
|
|
+// // 文本2是否禁售:false
|
|
|
+// }
|
|
|
|
|
|
}
|