如何计算两个字符串的相互连合指数Java



我正在尝试计算两个字符串 A 和 B 的相互连合指数。我已经计算了每个字符串中每个字母的频率。但是,我不知道如何从那里继续。任何帮助,不胜感激。预期的输出应该是某个十进制值。谢谢!

public class MutualIndexOfAB
{
public double calculateMutual(String a, String b)
{
    int i, j;
    int NA = 0;
    int NB = 0;
    double sum = 0.0, total = 0.0;
    a = a.toUpperCase();
    b = b.toUpperCase();

    // calculate frequency of each letter in String a 
    int chA;
    for (i=0; i<a.length(); i++)
    {
        ch = a.charAt(i)-65;
        if (chA>=0 && chA <26)
        {
            values[chA]++;
            NA++;
        }
    }

    // calculate frequency of each letter in String b 
    int chB;
    for (j=0; j<b.length(); j++)
    {
        chB = b.charAt(j)-65;
        if (chB>=0 && chB <26)
        {
            values[chB]++;
            NB++;
        }
    }

}

public static void main(String[] args)
{
    MutualIndexOfAB test = new MutualIndexOfAB();
    String textA = "cyber security is about how we develop secure computers and computer networks, to ensure that the data stored and transmitted through them is protected from unauthorized access or to combat digital security threats and hazards. as we conduct more of our social, consumer and business activities online, there is a corresponding increase in the demand for ict professionals to manage our digital environment and economy.";
    String textB = "cyber security has been identified as one of the strategic priorities in australia to meet the demands of law enforcement, national and state governments, defense, security and finance industries. jobs of the future will be in all of these areas ensuring there is national capability to maintain and build our essential services and stop them from being disrupted, destroyed, or threatened, and that our personal information is not communicated, shared, visualized or analysed without our permission.";
    System.out.println("Mutual Index of Concidence of Texts A and B: " + test.calculateMutual(textA, textB));
}

}

只需创建一个带有索引(而不是两个(的循环,然后比较每个字符串的字符,如果它们匹配,则增加一个计数器。然后执行除以字符总数。

最新更新