I was reading this beautiful book on python at http://greenteapress.com out there I came across a python implementation for finding out the word frequency in a given sentence. You might naturally ask, why do frequency analysis? Well the reasons are many, but the primary usage may be in the field of compression, if we know which letter has the maximum freq in a file we can devise efficient encoding techniques to compress a given file.
So let's cut out the talk here goes the implementation.
package dictionary;
import java.util.Map;
import java.util.TreeMap;
public class LetterFrequency {
private Map
private String feed;
public LetterFrequency(String feed)
{
this.feed=feed;
}
public Map
{
char feedChar;
Integer freq;
for(int i=0; i<feed.length(); i++)
{
feedChar=feed.charAt(i);
freq=map.get(feedChar);
map.put(feedChar, freq==null?1:freq+1);
}
return map;
}
public static void main(String[] args) {
LetterFrequency frequency=new LetterFrequency("Vivek Ramaswamy");
System.out.println("LetterFrequency "+ frequency.findFrequency());
}
}
No comments:
Post a Comment