Java 如何计算数组中的重复字符串?
我已经浏览了整个堆栈,但在我的案例中没有一个例子是有效的(从我尝试的情况来看) 我想计算一个单词在数组中出现的次数。这是通过拆分输入字符串(如“Henry and Harry out”)并计算不同长度的不同字符来完成的(在下面的示例中为2) 请原谅我,如果我的风格是坏的,这是我的第一个项目 他=1 en=2 nr=1 ry=2 a=1 an=1 等。。。。。。。 以下是我的构造函数代码:Java 如何计算数组中的重复字符串?,java,arrays,Java,Arrays,我已经浏览了整个堆栈,但在我的案例中没有一个例子是有效的(从我尝试的情况来看) 我想计算一个单词在数组中出现的次数。这是通过拆分输入字符串(如“Henry and Harry out”)并计算不同长度的不同字符来完成的(在下面的示例中为2) 请原谅我,如果我的风格是坏的,这是我的第一个项目 他=1 en=2 nr=1 ry=2 a=1 an=1 等。。。。。。。 以下是我的构造函数代码: public NgramAnalyser(int n, String inp) {
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
public NgramAnalyser(int n,String inp)
{
布尔值=假;
ngram=new HashMap();//用于存储ngram字符串和计数
字母表大小=0;
ngramSize=n;
ArrayList tempList=新的ArrayList();
System.out.println(“inp-length:+inp.length());
System.out.println();
int finalIndex=0;
对于(int i=0;i一个简单的解决方案应该使用映射ngram
,并且在迭代ngram列表时,对于输入中找到的每个键(aka字符串
),更新计数器(aka整数
).此方法创建一个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;
}
公共映射ngram(字符串inp,整数n)
{
Map nGram=newhashmap();
对于(inti=0;i
此代码接受字符串,将其转换为相同的字母大小写,删除空格并转换为数组。逐个插入每个值,如果已经存在,则将其计数增加一倍。祝您好运
//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());
不清楚
ngramSize
从何而来。您可以查看Apache的StringUtils
类。该类有许多有用的方法。您可以使用split(String,char)
拆分字符串,然后使用countMatches(String,String)
查找字符串出现的次数。对不起,我忘了添加SignatureGramSize是一个参数,它不仅仅是一个构造函数,伙计!这就是全部的工具和工具!非常感谢!!非常感谢!我实际上也需要这样做!
[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}