我正在解析一堆员工事故报告以用于报告目的.
事件报告本身是自由文本,我必须按身体位置对伤害进行分类.我想避免if{}elseif{}elseif{}....}else{}.
事件报告示例:
Employee slipped on wet stairs and injured her knee and right arm, and struck her head on the handrail.
Run Code Online (Sandbox Code Playgroud)
应该在受影响的区域添加"膝盖","手臂"和"头部".
Employee was lifting boxes without approved protective equipment resulting in a back strain.
Run Code Online (Sandbox Code Playgroud)
应该向受影响的区域添加"返回".
While attempting to unjam copier, employee got right index finger caught in machinery resulting in a 1-inch cut.
Run Code Online (Sandbox Code Playgroud)
应该在受影响的区域添加"手指".
现在,我有:
private static StaffInjuryData setAffectedAreas(String incident, StaffInjuryData sid){
incident = incident.toUpperCase(); //eliminate case issues
if(incident.contains("HEAD")){
sid.addAffectedArea("HEAD");
}else if(incident.contains("FACE")){
sid.addAffectedArea("FACE");
}else if(incident.contains("EYE")){
sid.addAffectedArea("EYE");
}else if(incident.contains("NOSE")){
sid.addAffectedArea("NOSE");
}
//etc, etc, etc
return sid;
}
Run Code Online (Sandbox Code Playgroud)
是否有更简单/更有效的方法if-elseif-ad inifinitum来做到这一点?
一种方法是从各个身体部位构造正则表达式,使用它来搜索字符串,并将单个匹配添加到列表中:
Pattern bodyParts = Pattern.compile("\\b(head|face|eye|nose)\\b", Pattern.CASE_INSENSITIVE);
Run Code Online (Sandbox Code Playgroud)
使用的\b两端防止部分匹配,例如发现"head"在包含文本"forehead"或"eye"内部的"eyelid".
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |