나는 Stack을 살펴 봤지만 예제에서는 (내가 시도한 것에서) 전혀 작동하지 않는다.배열에서 중복 문자열을 어떻게 계산합니까?
단어가 배열에서 몇 번이나 발생하는지 계산하고 싶습니다. 이 작업은 "Henry and Harry went went"와 같이 길이가 다른 문자를 계산하는 등의 입력 문자열을 분할하여 수행합니다 (다음 예에서는 2입니다) 내 스타일이 좋지 않으면 용서해주십시오. ...
그는 = 1
EN = 2
NR = 1
스피 = 2
A = 1
= 1
등 ....... 여기 생성자 내 코드입니다 : N- 그램의 목록을 반복하는 동안 간단한 솔루션의 Map<String, Integer> ngram
를 사용한다
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
이에서 오는'ngramSize'을 분명하지 않다. – freedev
아파치의'StringUtils' 클래스를 살펴볼 수 있습니다. 이 클래스에는 많은 유용한 메소드가 있습니다. 'split (String, char)'을 사용하여 문자열을 분리 한 다음'countMatches (String, String)'을 사용하여 문자열이 몇 번 발생했는지 찾을 수 있습니다. –
죄송합니다. 서명을 추가하지 않았습니다. –