Java:将ArrayList相互链接以获得一个";“顶级分类”;任务

Java:将ArrayList相互链接以获得一个";“顶级分类”;任务,java,arraylist,Java,Arraylist,注意:源代码包含多个类,所以为了您的时间,我不会发布它,但会给您上下文。如果我解释不清楚,请原谅。我在这方面做了很多工作,我的解释对我来说是有意义的,但对其他人来说可能没有意义 我的任务是确定用户输入属于哪个类别。例如,如果用户输入:我爱狗和猫。 程序将输出前2个类别: 狗, 猫 如果用户只输入“我爱狗”,程序将输出前2个类别为“狗,未找到其他类别” 如果只找到一个类别或根本没有,则默认响应为“无类别” 我已经为以下类别创建了数组列表:狗、猫、鸟。这些ArrayList包含的关键字将触发程序识别

注意:源代码包含多个类,所以为了您的时间,我不会发布它,但会给您上下文。如果我解释不清楚,请原谅。我在这方面做了很多工作,我的解释对我来说是有意义的,但对其他人来说可能没有意义

我的任务是确定用户输入属于哪个类别。例如,如果用户输入:我爱狗和猫。 程序将输出前2个类别: 狗, 猫

如果用户只输入“我爱狗”,程序将输出前2个类别为“狗,未找到其他类别”

如果只找到一个类别或根本没有,则默认响应为“无类别”

我已经为以下类别创建了数组列表:狗、猫、鸟。这些ArrayList包含的关键字将触发程序识别用户输入的类别

我基本上需要获得最高可能性和第二高可能性(如果适用),并将它们“链接”到一个字符串,该字符串将输出类别

这是我的代码,它试图获取前两个最高的可能性,并将它们输出到控制台上我的问题是让类别链接到各自的字符串,以确保输出可能性最高的类别。

    //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);
//创建优先级
int-topDouble=0;
字符串topString=“无类别”//默认响应
int-secondDouble=0;
String secondString=“无类别”//默认响应
ArrayList likelyDouble=新的ArrayList();
添加(猫);
添加(狗);
可能是双倍。加上(鸟);
ArrayList likelyString=新的ArrayList();
加上(“你说的是猫”);
//类似于双猫类
加上(“你说的是狗”);
//类似于双狗类
加上(“你说的是鸟”);
//类似于双猫类
整数计数=0;
用于(双d:可能是双d){
如果((d>0)和&(d>topDouble)){
topDouble=(int)d;
topString=likelyString.get(count);
}
否则如果((d>0)和(&(d>secondDouble)){
secondDouble=(int)d;
secondString=likelyString.get(count);
}
}
系统输出打印(topString+“\n”);
系统输出打印(第二个字符串);
我得到的输出默认为:

用户输入: 我喜欢狗和猫

无类别

仅供参考,该程序根据句子中的位置和类别被引用的次数确定用户谈论某个类别的可能性。可能性是一个经过计算的值。因此,如果根本没有提到类别,则可能性为0


谢谢你的帮助

我不清楚你想要什么,但我怀疑这与你的演员阵容有关:

topDouble = (int) d;
您总是将
topDouble
设置为0-假设可能性在[0,1]范围内。
这同样适用于
secondDouble

可能您想将
topDouble
secondDouble
声明为
double
,并将强制转换删除为int-以获得max/second值的两倍

此外-我看不到您增加
计数
,因此您总是
get()
数组列表中的第一个元素

只是一种设计,它认为[在我看来]有更好的方法:
创建一个新类:
类似于hoodstringdouble
,包含两个字段,一个是
字符串
,另一个是
double
。使其实现[基于
双精度
的值


你现在要做的就是对列表进行排序,并获得你想要的前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]);
List categories=new ArrayList();
类别。添加(“狗”);
类别。添加(“猫”);
类别。添加(“鸟类”);
Map counterMap=新HashMap
用于(字符串s:类别){
对映图。放置(s,0);
}
List inputString=new ArrayList();
add(“您谈论过猫”);
add(“您谈论过狗”);
add(“您谈论过鸟类”);
for(字符串s:inputString){
用于(字符串s2:类别){
//获取句子中类别的可能性
双d=getLikelyhood(s2,s);
//在你的地图上添加可能性
map.put(s2,map.get(s2)+d);
}
}
//在使用用户输入设置类别的可能性之后
//你只需要得到地图上的两个主要值
//我会给你一个小算法
int x=0;
字符串[]arrS=新字符串[m.size()];
对于(对象o:m.keySet().toArray()){
arrS[x++]=(字符串)o;
}
x=0;
Double[]arrI=新的Double[m.size()];
对于(对象o:m.values().toArray()){
arrI[x++]=(双)o;
}
int max1,max2,posMax1,posMax2;
max1=arrI[0];
max2=arrI[0];
posMax1=0;
posMax2=0;
对于(int i=1;i=max1){
max2=max1;
max1=arrI[i];
posMax2=posMax1;
posMax1=i;
}else if(arrI[i]>max2){
max2=arrI[i];
posMax2=i;
}
}
System.out.println(“最大类别:+arrS[posMax1]);
System.out.println(“第二最大类:+arrS[posMax2]);

希望这对您有所帮助。

count
永远不会递增,因此您将始终获得
中的第一个元素,如字符串
,根据您的程序,它应该是“您谈论过的猫”,但您的输出仅表示“狗”为什么只有两个?真的,您应该返回找到的类别列表,可以是任意大小的。您当然可以停止查看两个元素,但您可能会发现t