首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >想要计算字符串在Java中的出现次数

想要计算字符串在Java中的出现次数
EN

Stack Overflow用户
提问于 2015-03-29 07:17:10
回答 3查看 117关注 0票数 0

因此我有一个.txt文件,我使用以下命令调用它

代码语言:javascript
复制
String[] data = loadStrings("data/data.txt");

该文件已经排序,基本上看起来如下所示:

代码语言:javascript
复制
Animal
Animal
Cat
Cat
Cat
Dog

我希望在java中创建一个算法来计算排序列表,而不使用任何像Multisets或Maps/HashMaps这样的库。到目前为止,我已经成功地让它打印出出现频率最高的单词,如下所示:

代码语言:javascript
复制
ArrayList<String> words = new ArrayList();

int[] occurrence = new int[2000];
Arrays.sort(data);

for (int i = 0; i < data.length; i ++ ) {
words.add(data[i]);     //Put each word into the words ArrayList
}
for(int i =0; i<data.length; i++) {
 occurrence[i] =0;
 for(int j=i+1; j<data.length; j++) {
   if(data[i].equals(data[j])) {
     occurrence[i] = occurrence[i]+1;
   }
 }
}
int max = 0;
String most_talked ="";
for(int i =0;i<data.length;i++) {
  if(occurrence[i]>max) {
    max = occurrence[i];
    most_talked = data[i];
  }
 }
 println("The most talked keyword is " + most_talked + " occuring " + max + " times.");

我想要的不仅仅是出现频率最高的单词,可能是前5名或前10名。希望这已经足够清楚了。感谢您的阅读

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-29 07:45:23

既然你说你不想使用某种数据结构,我认为你可以做这样的事情,但它的性能不佳。我通常更喜欢存储索引而不是值。

代码语言:javascript
复制
ArrayList<String> words = new ArrayList();

int[] occurrence = new int[2000];
Arrays.sort(data);


int nwords = 0;
occurrence[nwords]=1;
words.add(data[0]);        
for (int i = 1; i < data.length; i ++ ) {
    if(!data[i].equals(data[i-1])){ //if a new word is found
        words.add(data[i]);         //put it into the words ArrayList
        nwords++;                   //increment the index
        occurrence[nwords]=0;       //initialize its occurrence counter
    }
    occurrence[nwords]++;           //increment the occurrence counter
}

int max;
for(int k=0; k<5; k++){  //loop to find 5 times the most talked word
  max = 0;               //index of the most talked word
  for(int i = 1; i<words.size(); i++) { //for every word
    if(occurrence[i]>occurrence[max]) { //if it is more talked than max
      max = i;                          //than it is the new most talked
    }
  }
  println("The most talked keyword is " + words.get(max) + " occuring " + occurence[max] + " times.");
  occurence[max]=0;
}

每次我找到具有较高出现值的值时,我将其出现计数器设置为0,并再次重复该数组,这重复了5次。

票数 1
EN

Stack Overflow用户

发布于 2015-03-29 07:29:54

如果你不能使用Guava的Multiset,那么你可以自己实现一个等价的。基本上,您只需要创建一个Map<String, Integer>,它跟踪每个单词(键)的计数(值)。这意味着要改变这一点

代码语言:javascript
复制
ArrayList<String> words = new ArrayList<String>();
// ...
for (int i = 0; i < data.length; i ++ ) {
  words.add(data[i]);     //Put each word into the words ArrayList
}

如下所示:

代码语言:javascript
复制
Map<String, Integer> words = new HashMap<String>();
// ...
for (String word : data) {
  Integer count = words.get(word);
  words.put(word, (count != null : count.intValue() + 1 ? 1));
}

填充地图后,只需使用sort it by the values即可。

如果您也不能使用Map,您可以执行以下操作:

首先,为单词计数创建一个包装类:

代码语言:javascript
复制
public class WordCount implements Comparable<WordCount> {
    private String word;
    private int count;

    public WordCount(String w, int c) {
      this.word = w;
      this.count = c;
    }

    public String getWord() {
      return word;
    }

    public int getCount() {
      return count;
    }

    public void incrementCount() {
      count++;
    }                 

    @Override
    public int compareTo(WordCount other) {
      return this.count - other.count;
    }
}

然后,更改代码以在列表中存储WordCount实例(而不是String):

代码语言:javascript
复制
ArrayList<WordCount> words = new ArrayList<WordCount>();
// ...
for (String word : data) {
    WordCount wc = new WordCount(word, 1);
    boolean wordFound = false;

    for (WordCount existing : words) {
        if (existing.getWord().equals(wc.getWord())) {
            existing.incrementCount();
            wordFound = true;
            break;
        }
    }

    if (!wordFound) {
        words.add(wc);
    }
}

最后,填充List之后,只需使用Collections.sort()对其进行排序。这很容易,因为值对象实现了Comparable

代码语言:javascript
复制
Collections.sort(words, Collections.reverseOrder());
票数 1
EN

Stack Overflow用户

发布于 2015-03-29 07:44:52

你可以试试像这样简单的东西..

代码语言:javascript
复制
int count = 0;

for( int i = 0; i < words.size(); i++ ){
    System.out.printf("%s: ", words.get( i ));
    for( int j = 0; j < words.size(); j++ ) {
        if( words.get( i ).equals( words.get( j ) ) )
            count++;
    }                                               
    System.out.printf( "%d\n", count );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29323751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档