0

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);
    }
}
help-info.de
  • 6,695
  • 16
  • 39
  • 41
T.newGuy1620
  • 15
  • 1
  • 1
  • 6

3 Answers3

0

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

C.Gibby
  • 115
  • 1
  • 7
0

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

}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • You may say I’m doing double work by splitting each sentence again (you had already done that in the first part of the main method), but unless you have very many sentences, I think that it shouldn’t matter. The double work can be eliminated by further rewriting if required, of course. – Ole V.V. Aug 12 '16 at 15:50
-1

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 :-)

Michael Parker
  • 12,724
  • 5
  • 37
  • 58