我已經以不同的方式問了這個問題幾次。每當我獲得突破時,我都會遇到另一個問題。這也是因爲我不熟悉Java,並且對像Google Maps這樣的集合有困難。所以請耐心等待。將兩個LinkedHashMaps與值作爲列表比較
我有兩張地圖是這樣的:
Map1 -{ORGANIZATION=[Fulton Tax Commissioner 's Office, Grady Hospital, Fulton Health Department], LOCATION=[Bellwood, Alpharetta]}
Map2 - {ORGANIZATION=[Atlanta Police Department, Fulton Tax Commissioner, Fulton Health Department], LOCATION=[Alpharetta], PERSON=[Bellwood, Grady Hospital]}
的地圖被定義爲:LinkedHashMap<String, List<String>> sampleMap = new LinkedHashMap<String, List<String>>();
我基於數值比較這兩個地圖和只有3個按鍵,即組織,個人和位置。 Map1是我比較Map2的金牌。現在我面臨的問題是,當我迭代Map1中的ORGANIZATION鍵值並檢查Map2中的匹配項時,即使我的第一個條目在Map2(富爾頓稅務專員)中有部分匹配,但因爲Map2的第一個條目(亞特蘭大警察局)是不是一場比賽,我得到一個不正確的結果(我正在尋找確切和部分匹配)。這裏的結果是增加了真正的肯定的,假的肯定的和假的否定的計數器,這使我能夠最終計算精確度和召回率,即命名實體識別。
編輯
我期待這樣做的結果是
Organization:
True Positive Count = 2
False Negative Count = 1
False Positive Count = 1
Person:
False Positive Count = 2
Location:
True Positive Count = 1
False Negative Count = 1
我目前得到的輸出是:
Organization:
True Positive Count = 1
False Negative Count = 2
False Positive Count = 0
Person:
True Positive Count = 0
False Negative Count = 0
False Positive Count = 2
Location:
True Positive Count = 0
False Negative Count = 1
False Positive Count = 0
CODE
private static List<Integer> compareMaps(LinkedHashMap<String, List<String>> annotationMap, LinkedHashMap<String, List<String>> rageMap)
{
List<Integer> compareResults = new ArrayList<Integer>();
if (!annotationMap.entrySet().containsAll(rageMap.entrySet())){
for (Entry<String, List<String>> rageEntry : rageMap.entrySet()){
if (rageEntry.getKey().equals("ORGANIZATION") && !(annotationMap.containsKey(rageEntry.getKey()))){
for (int j = 0; j< rageEntry.getValue().size(); j++) {
orgFalsePositiveCount++;
}
}
if (rageEntry.getKey().equals("PERSON") && !(annotationMap.containsKey(rageEntry.getKey()))){
// System.out.println(rageEntry.getKey());
// System.out.println(annotationMap.entrySet());
for (int j = 0; j< rageEntry.getValue().size(); j++) {
perFalsePositiveCount++;
}
}
if (rageEntry.getKey().equals("LOCATION") && !(annotationMap.containsKey(rageEntry.getKey()))){
for (int j = 0; j< rageEntry.getValue().size(); j++) {
locFalsePositiveCount++;
}
}
}
}
for (Entry<String, List<String>> entry : annotationMap.entrySet()){
int i_index = 0;
if (rageMap.entrySet().isEmpty()){
orgFalseNegativeCount++;
continue;
}
// for (Entry<String, List<String>> rageEntry : rageMap.entrySet()){
if (entry.getKey().equals("ORGANIZATION")){
for(String val : entry.getValue()) {
if (rageMap.get(entry.getKey()) == null){
orgFalseNegativeCount++;
continue;
}
recusion: for (int i = i_index; i< rageMap.get(entry.getKey()).size();){
String rageVal = rageMap.get(entry.getKey()).get(i);
if(val.equals(rageVal)){
orgTruePositiveCount++;
i_index++;
break recusion;
}
else if((val.length() > rageVal.length()) && val.contains(rageVal)){ //|| dataB.get(entryA.getKey()).contains(entryA.getValue())){
orgTruePositiveCount++;
i_index++;
break recusion;
}
else if((val.length() < rageVal.length()) && rageVal.contains(val)){
orgTruePositiveCount++;
i_index++;
break recusion;
}
else if(!val.contains(rageVal)){
orgFalseNegativeCount++;
i_index++;
break recusion;
}
else if(!rageVal.contains(val)){
orgFalsePositiveCount++;
i_index++;
break recusion;
}
}
}
}
......................... //(Same for person and location)
compareResults.add(orgTruePositiveCount);
compareResults.add(orgFalseNegativeCount);
compareResults.add(orgFalsePositiveCount);
compareResults.add(perTruePositiveCount);
compareResults.add(perFalseNegativeCount);
compareResults.add(perFalsePositiveCount);
compareResults.add(locTruePositiveCount);
compareResults.add(locFalseNegativeCount);
compareResults.add(locFalsePositiveCount);
System.out.println(compareResults);
return compareResults;
}
你應該更正式地描述你想要接收什麼結果 – Andremoniy
@Andremoniy完成!我的代碼完成了大部分的工作,但我想我要找的是對「如何比較之前需要對地圖進行排序?」這樣的問題的答案。或者有什麼我應該做的,以防止這個問題? – serendipity
什麼是價值類型?它是一個集合還是一個列表? – vanje