简体   繁体   中英

how to count how many times each word appears?

I'm trying to count how many times each word from uniqueBagOfWords appears in each sentence from the 'sentences' arraylist.

uniqueBagOFwords = [i, like, to, play, tennis, think, football, needs, big, changes]

I would like to be able to count how many times a word from uniqueBagOfWords appears in each sentence....At the moment I can only add 1 to the position of the word if it appears at all but I would like to add the number of times it appears. At the moment it prints out this:

i like to play tennis = 1111100000

i think football needs big changes = 1000011111

i like football football = 1100001000

How would I alter this code so it prints out the following..

i like to play tennis = 1111100000

i think football needs big changes = 1000011111

i like football football = 1100002000

 public static void main(String[] args) {
        List<String> sentences = new ArrayList<String>();
        sentences.add("i like to play tennis");
        sentences.add("i think football needs big changes");
        sentences.add("i like football football");

    List<String[]> bagOfWords = new ArrayList<String[]>();
    for (String str : sentences) {
        bagOfWords.add(str.split(" "));

    }
    Set<String> uniqueBagOfWords = new LinkedHashSet<String>();
    for (String[] s : bagOfWords) {
        for (String ss : s)
            for (String st : ss.split(" "))
                if (!uniqueBagOfWords.contains(st))
                    uniqueBagOfWords.add(st);
    }

    for (String s : sentences) {
        StringBuilder numOfOccurences = new StringBuilder();
        int count = 0;

        for (String word : uniqueBagOfWords) {

            if (s.contains(word)) {

                numOfOccurences.append(count+1);
            } else {
                numOfOccurences.append("0");
            }
        }
        System.out.println(s + " = " + numOfOccurences);
    }
}

This really isn't the best fix, but it works

public static void main(String[] args) {
    List<String> sentences = new ArrayList<String>();
    sentences.add("i like to play tennis");
    sentences.add("i think football needs big changes");
    sentences.add("i like football football");


List<String[]> bagOfWords = new ArrayList<String[]>();
for (String str : sentences) {
    bagOfWords.add(str.split(" "));

}
Set<String> uniqueBagOfWords = new LinkedHashSet<String>();
for (String[] s : bagOfWords) {
    for (String ss : s)
        for (String st : ss.split(" "))
            if (!uniqueBagOfWords.contains(st))
                uniqueBagOfWords.add(st);

}



for (String st : sentences) {
    StringBuilder numOfOccurences = new StringBuilder();
    int[] array ={0,0,0,0,0,0,0,0,0,0};
    int num=0;
    for (String s : st.split(" ")){
        num=0;
        for (String word : uniqueBagOfWords) {

            if (s.equals(word)) {
                array[num] = array[num]+1;
            }
            num++;
        }
    }
    num=0;
    for(int number : array){
        numOfOccurences.append(number);
    }
    System.out.println(st + " = " + numOfOccurences);

}

This is the output I got:

i like to play tennis = 1111100000

i think football needs big changes = 1000011111

i like football football = 1100002000

You may rewrite the last for loop like this:

for (String s : sentences) {
    StringBuilder numOfOccurences = new StringBuilder();

    for (String word : uniqueBagOfWords) {
        int count = 0;
        for (String wordFromSentence : s.split(" ")) {
            if (wordFromSentence.equals(word)) {
                count++;
            }
        }
        numOfOccurences.append(count);
    }
    System.out.println(s + " = " + numOfOccurences);

}

I'm not completely sure of your goal.

If just want to print out your output in a single line, rather than have a newline at the end of each number, the just use:

System.out.print(s + " = " + numOfOccurences);

rather than

System.out.println(s + " = " + numOfOccurences);

Note the use of print instead of println . println automatically appends newline character ( \\n ) to the end of the output.

But perhaps also take a look at java.lang.Array for some helpful searching utilities. Note: arrays need to be sorted before you can search them.

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

Lots of nice utilities in here.

Best of luck :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM