2017-05-22 1 views
1

나는 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 
+0

이에서 오는'ngramSize'을 분명하지 않다. – freedev

+0

아파치의'StringUtils' 클래스를 살펴볼 수 있습니다. 이 클래스에는 많은 유용한 메소드가 있습니다. 'split (String, char)'을 사용하여 문자열을 분리 한 다음'countMatches (String, String)'을 사용하여 문자열이 몇 번 발생했는지 찾을 수 있습니다. –

+0

죄송합니다. 서명을 추가하지 않았습니다. –

답변

0

이 메서드는 키가 다른 항목이고 값이 항목 수인 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; 
} 
+1

대단히 감사합니다 !! –

2

는, 입력에서 발견 된 각 키 (일명 String)가 카운터 (일명 Integer)를 업데이트합니다.

0

잘못 될 수있는 뭔가가 있다면 부탁드립니다. 각 값을 하나씩 삽입하십시오. 이미 존재하는 경우 다른 값으로 하나씩 증가 시키십시오. 행운을 빕니다

//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()); 

출력 :

[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} 
+0

고맙습니다. 나는 이것을 실제로해야했다! –

관련 문제