如何计算数组中的重复字符串?

时间:2023-02-07 07:42:05

I Have looked through Stack, but none of the examples work in my case (from what I have tried).

我已经浏览了Stack,但是没有一个例子适用于我的情况(根据我的尝试)。

I want to count how many times a word occurs in an array. This is done by splitting up an input String, such as "Henry and Harry went out" and counting the distinct characters of varying length (in the following example it is 2) Please forgive me if my style is bad, its my first project...

我想计算一个单词在数组中出现的次数。这是通过拆分输入字符串来完成的,例如“亨利和哈利走出去”并计算不同长度的不同字符(在下面的例子中它是2)请原谅我,如果我的风格不好,这是我的第一个项目。 ..

He = 1

他= 1

en = 2

en = 2

nr = 1

nr = 1

ry = 2

ry = 2

a = 1

a = 1

an = 1

an = 1

etc....... Here is my code for the constructor:

等.......这是构造函数的代码:

   public NgramAnalyser(int n, String inp) 
   { 
       boolean processed = false;
       ngram = new HashMap<>(); // used to store the ngram strings and count
       alphabetSize = 0;
       ngramSize = n;
       ArrayList<String> tempList = new ArrayList<String>();
       System.out.println("inp length: " + inp.length());
       System.out.println();
       int finalIndex = 0;

       for(int i=0; i<inp.length()-(ngramSize - 1); i++)
       {
           tempList.add(inp.substring(i,i+ngramSize));
           alphabetSize++;
           if(i == (inp.length()- ngramSize))
        // if i (the index) has reached the boundary limit ( before it gets an error), then...
           {
               processed = true;
               finalIndex = i;
               break;
           }
    }

       if(processed == true)
       { 
          for(int i=1; i<(ngramSize); i++)
          {
             String startString = inp.substring(finalIndex+i,inp.length());
             String endString = inp.substring(0, i);
             tempList.add(startString + endString);
          }  
       }

       for(String item: tempList)
       {
        System.out.println(item);
       }

    }
    // code for counting the ngrams and sorting them

3 个解决方案

#1


0  

This method creates a HashMap with the keys being the different items and the values the item count. I think the code is pretty easy to understand but ask if there's something that isn't clear or might be wrong

此方法创建一个HashMap,其中键是不同的项和项计数的值。我认为代码很容易理解,但询问是否存在不清楚或可能错误的问题

public Map<String, Integer> ngram(String inp, Integer n)
{
    Map<String, Integer> nGram = new HashMap<>();
    for(int i = 0; i < inp.length() - n - 1; i++)
    {
        String item = inp.substring(i, i+n);
        int itemCount = nGram.getOrDefault(item, 0);
        nGram.put(item, itemCount+1);
    }
    return nGram;
}

#2


2  

A simple solution should use the Map<String, Integer> ngram and, while iterating on your list of ngram, for each key (aka String) found in your input update the counter (aka Integer).

一个简单的解决方案应该使用Map ngram,并在迭代你的ngram列表时,为你输入中找到的每个键(aka String)更新计数器(也就是整数)。 ,integer>

#3


0  

This code takes the string converts it to same alphabetical case, remove spaces and turns to array. insert each value one by one, if it already exist increment its count by one other wise put the count as one. Good luck

此代码将字符串转换为相同的字母大小写,删除空格并转向数组。逐个插入每个值,如果它已经存在则将其计数增加一个其他明智的计数为1。祝好运

 //take random string, convert to same case to (Lower or upper) then turn to 
character array
        char[] charArray = "This is an example text".replaceAll("\\s","").toLowerCase().toCharArray();
        System.out.println(Arrays.toString(charArray));
        Map<Character, Integer> charCount = new HashMap<>();
        for (char c : charArray){
            //if key doesnt exist put it and update count value to 1
            if(!charCount.containsKey(c)){
                charCount.put(c, 1);
            }else{
                //if key exist increment value by 1
                charCount.put(c, charCount.get(c) + 1);
            }
        }

        System.out.println(charCount.toString());

output:

[t, h, i, s, i, s, a, n, e, x, a, m, p, l, e, t, e, x, t]
{p=1, a=2, s=2, t=3, e=3, h=1, x=2, i=2, l=1, m=1, n=1}

#1


0  

This method creates a HashMap with the keys being the different items and the values the item count. I think the code is pretty easy to understand but ask if there's something that isn't clear or might be wrong

此方法创建一个HashMap,其中键是不同的项和项计数的值。我认为代码很容易理解,但询问是否存在不清楚或可能错误的问题

public Map<String, Integer> ngram(String inp, Integer n)
{
    Map<String, Integer> nGram = new HashMap<>();
    for(int i = 0; i < inp.length() - n - 1; i++)
    {
        String item = inp.substring(i, i+n);
        int itemCount = nGram.getOrDefault(item, 0);
        nGram.put(item, itemCount+1);
    }
    return nGram;
}

#2


2  

A simple solution should use the Map<String, Integer> ngram and, while iterating on your list of ngram, for each key (aka String) found in your input update the counter (aka Integer).

一个简单的解决方案应该使用Map ngram,并在迭代你的ngram列表时,为你输入中找到的每个键(aka String)更新计数器(也就是整数)。 ,integer>

#3


0  

This code takes the string converts it to same alphabetical case, remove spaces and turns to array. insert each value one by one, if it already exist increment its count by one other wise put the count as one. Good luck

此代码将字符串转换为相同的字母大小写,删除空格并转向数组。逐个插入每个值,如果它已经存在则将其计数增加一个其他明智的计数为1。祝好运

 //take random string, convert to same case to (Lower or upper) then turn to 
character array
        char[] charArray = "This is an example text".replaceAll("\\s","").toLowerCase().toCharArray();
        System.out.println(Arrays.toString(charArray));
        Map<Character, Integer> charCount = new HashMap<>();
        for (char c : charArray){
            //if key doesnt exist put it and update count value to 1
            if(!charCount.containsKey(c)){
                charCount.put(c, 1);
            }else{
                //if key exist increment value by 1
                charCount.put(c, charCount.get(c) + 1);
            }
        }

        System.out.println(charCount.toString());

output:

[t, h, i, s, i, s, a, n, e, x, a, m, p, l, e, t, e, x, t]
{p=1, a=2, s=2, t=3, e=3, h=1, x=2, i=2, l=1, m=1, n=1}