NEO
NEO

Reputation: 469

Manipulate Strings

I am trying to find the most repeated word in a string, with this code:

public class Word 
{
    private String toWord;
    private int Count;

    public Word(int count, String word){
        toWord = word;
        Count = count;
    }

    public static void main(String args[]){
        String str="my name is neo and my other name is also neo because I am neo";
        String []str1=str.split(" ");
        Word w1=new Word(0,str1[0]);
        LinkedList<Word> list = new LinkedList<Word>();
        list.add(w1);
        ListIterator itr = list.listIterator();
        for(int i=1;i<str1.length;i++){
            while(itr.hasNext()){
                if(str1[i].equalsTO(????));
                else
                    list.add(new Word(0,str1[i]));
            }
        }
    }
}

How do I compare the String from the String Array str1 to the String stored in the linked list and then how do I increase the respective count.

I will then print the string with the highest count, I don't know how to do that either.

Upvotes: 1

Views: 476

Answers (6)

user187702
user187702

Reputation:

Use Apache Commons StringUtils org.apache.commons.lang.StringUtils to get the count.

String str="my name is neo and my other name is also neo because I am neo";
// Make a unique list (java.util.Set) of words.
Set<String> stSet = new HashSet<String>(Arrays.asList(str.split(" ")));
int sz = stSet.size();
int[] counts = new int[sz];
Map<Integer,String> matches = new HashMap<Integer,String>(sz);
int i = 0;
for (String s : stSet) {
   // saves the individual word count in a sortable array.
   counts[i] = StringUtils.countMatches(str,s));
   // saves the word count and the word in a HashMap for easy retrieval.
   matches.put(counts[i],s);
   i++;
}
Arrays.sort(counts);
int max = counts.length - 1;
System.out.println("The the word with the most occurrances is: "+matches.get(counts[max])+", the number of occurrances is: "+counts[max]);

Upvotes: 0

Ayush Vatsyayan
Ayush Vatsyayan

Reputation: 13

I think you can use some regex over here like

    final String str = "my name is neo and my other name is also neo because I am neo";

    final String[] arr = str.split (" ");
    final Set <String> set = new HashSet <String> ();
    for (final String word : arr) {
        System.out.println ("arr " + word);
        set.add (word);
    }

    String preWord = "";
    int preCount = 0;
    for (final String word : set) {
        System.out.println ("----------------");

        final Pattern p2 = Pattern.compile ("\\b" + word + "\\b");
        final Matcher m2 = p2.matcher (str);
        int count = 0;

        while (m2.find ()) {
            count++;
        }

        System.out.println ("preCount " + preWord + ":" + word + ":" + preCount + ":" + count);

        if ((preCount < count)) {
            preWord = word;
            preCount = count;
            System.out.println ("assigning word " + word + ":" + count);
        }
    }

    System.out.println ("result " + preWord + ":" + preCount);

Upvotes: 0

Eternal Noob
Eternal Noob

Reputation: 2807

I would suggest use HashMap instead of linked list.

Iterate through the string.
For each word,
Check if the word is in the Map,
If it is there increment count and
Otherwise insert with count 1

Upvotes: 4

sjr
sjr

Reputation: 9895

Using Google Guava:

Multiset<String> words = HashMultiset.create(Splitter.on(" ").split(input));

Then

String topWord = words.isEmpty() ? null 
    : Iterables.get(Ordering.natural().immutableSortedCopy(words), 0);

You can get the frequency of the top word with words.count(topWord).

Upvotes: 0

Schultz9999
Schultz9999

Reputation: 8946

C#? You may try to use LINQ GroupBy and then Count or Max - very straightforward.

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67355

You'll need to store each word to a list, perhaps a long with a count variable that indicates how many times that word has been used.

For each word, increment the count if it's already in the list, or add it to the list if it's not.

Upvotes: 0

Related Questions