簡體   English   中英

Java:將數組列表相互鏈接以執行“頂級分類”任務

[英]Java: Linking arraylists to each other for a “top categorization” task

注意:源代碼包括多個類,因此,為了您的方便起見,我不會發布它,但會為您提供上下文。 如果我不能很好地解釋,請原諒。 我已經做了很多工作,我的解釋對我來說很有意義,但對其他人可能沒有意義。

我的任務是確定用戶輸入所屬的類別。 例如,如果用戶輸入:我愛狗和貓。 該程序將輸出前2類:狗,貓

如果用戶僅輸入:“我愛狗”,則程序會將前2個類別輸出為“狗,未找到其他類別”

如果僅找到一個類別或根本沒有一個類別,則默認響應為“ no category”。

我為以下類別創建了數組列表:狗,貓,鳥。 這些數組列表包含關鍵字,這些關鍵字將觸發程序來識別用戶輸入將屬於的類別。

我基本上需要獲得最高的可能性和第二高的可能性(如果適用),並將它們“鏈接”到將輸出類別的字符串。

這是我的代碼,它嘗試獲取前2個最高的可能性並將其輸出到控制台。 我的問題是使類別鏈接到各自的字符串,以確保輸出可能性最高的類別。

    //Create prioritization
    int topDouble = 0;
    String topString = "no category"; //default response
    int secondDouble = 0;
    String secondString = "no category"; // default response

    ArrayList<Double> likelyDouble = new ArrayList<Double>();
    likelyDouble.add(cats); 
    likelyDouble.add(dogs);
    likelyDouble.add(birds);

    ArrayList<String> likelyString = new ArrayList<String>();
    likelyString.add("you talked about cats"); 
                  //to parallel likelyDouble cats category
    likelyString.add("you talked about dogs");
                  //to parallel likelyDouble dogs category
    likelyString.add("you talked about birds");
                  //to parallel likelyDouble cats category

    int count = 0;
    for (double d : likelyDouble){
        if((d>0) && (d > topDouble)){
            topDouble = (int) d;
            topString = likelyString.get(count);
        }
        else if((d>0) && (d > secondDouble)){
            secondDouble = (int) d;
            secondString = likelyString.get(count);
        }
    }

    System.out.print(topString + "\n");
    System.out.print(secondString);

我得到的輸出默認為:

用戶輸入:我喜歡貓和狗。

小狗

沒有類別

FYI程序根據句子中的位置和引用該類別的次數,確定用戶在談論某個類別的可能性。 可能性是計算出的值。 因此,如果根本不提及類別,則可能性為0。

感謝您所有的幫助!

我不清楚您的追求,但我懷疑這與您的演員有關:

topDouble = (int) d;

您始終將topDouble設置為0-假設可能性在[0,1]范圍內。
同樣適用於secondDouble

可能您想將topDoublesecondDouble聲明為double ,並將secondDoublesecondDouble以獲取max / second值的兩倍。

另外-我看不到您增加count ,所以您總是get() ArrayList的第一個元素。

只是為了更好的方法而設計 [我認為]:
創建一個新類: LikelyhoodStringDouble有2個字段,一個是String ,另一個是double 使它實現Comparable [基於double的值。

您現在所要做的就是使用Collections.sort()對列表進行排序,並獲得所需的前k個元素(在您的情況下,k = 2)

如果您說對了,您可以嘗試使用地圖存儲任何用戶可以輸入的每個輸入類別的可能性。

給出的樣本:

List<String> categories = new ArrayList<String>();
categories.add("dogs");
categories.add("cats");
categories.add("birds");
Map<String, Double> counterMap = new HashMap<String, Double>
for(String s : categories) {
    counterMap.put(s, 0);
}
List<String> inputString = new ArrayList<String>();
inputString.add("you talked about cats");
inputString.add("you talked about dogs");
inputString.add("you talked about birds");
for(String s : inputString) {
    for(String s2 : categories) {
        //get the likelyhood of the category in the sentence
        Double d = getLikelyhood(s2, s);
        //add the likelyhood in your map
        map.put(s2, map.get(s2) + d);
    }
}

//after setting the likelyhood of the categories with the user input
//you just need to get the 2 major values in the map
//I'll let you a small algorithm for this
int x = 0;
String[] arrS = new String[m.size()];
for(Object o : m.keySet().toArray()) {
    arrS[x++] = (String)o;
}
x = 0;
Double[] arrI = new Double[m.size()];
for(Object o : m.values().toArray()) {
    arrI[x++] = (Double)o;
}
int max1, max2, posMax1, posMax2;
max1 = arrI[0];
max2 = arrI[0];
posMax1 = 0;
posMax2 = 0;
for(int i=1; i < arrI.length; i++) {
    if (arrI[i] >= max1) {
        max2 = max1;
        max1 = arrI[i];
        posMax2 = posMax1;
        posMax1 = i;
    } else if (arrI[i] > max2) {
        max2 = arrI[i];
        posMax2 = i;
    }
}
System.out.println("Max category: " + arrS[posMax1]);
System.out.println("Second Max category: " + arrS[posMax2]);

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM