Java string Term Frequency Statistics instance code, java Term Frequency

Source: Internet
Author: User

Java string Term Frequency Statistics instance code, java Term Frequency

Copy codeThe Code is as follows:
Package com. gpdi. action;

Import java. util. ArrayList;
Import java. util. Collections;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;

Public class WordsStatistics {

Class Obj {
Int count;
Obj (int count ){
This. count = count;
}
}

Public List <WordCount> statistics (String word ){
List <WordCount> rs = new ArrayList <WordCount> ();
Map <String, Obj> map = new HashMap <String, Obj> ();

If (word = null ){
Return null;
}
Word = word. toLowerCase ();
Word = word. replaceAll ("'s ","");
Word = word. replaceAll (",","");
Word = word. replaceAll ("-","");
Word = word. replaceAll ("\\.","");
Word = word. replaceAll ("'","");
Word = word. replaceAll (":","");
Word = word. replaceAll ("! ","");
Word = word. replaceAll ("\ n ","");

String [] wordArray = word. split ("");
For (String simpleWord: wordArray ){
SimpleWord = simpleWord. trim ();
If (simpleWord! = Null &&! SimpleWord. inclusignorecase ("")){
Obj cnt = map. get (simpleWord );
If (cnt! = Null ){
Cnt. count ++;
} Else {
Map. put (simpleWord, new Obj (1 ));
}
}
}

For (String key: map. keySet ()){
WordCount wd = new WordCount (key, map. get (key). count );
Rs. add (wd );
}

Collections. sort (rs, new java. util. Comparator <WordCount> (){
@ Override
Public int compare (WordCount o1, WordCount o2 ){
Int result = 0;
If (o1.getCount ()> o2.getCount ()){
Result =-1;
} Else if (o1.getCount () <o2.getCount ()){
Result = 1;
} Else {
Int strRs = o1.getWord (). compareToIgnoreCase (o2.getWord ());
If (strRs> 0 ){
Result = 1;
} Else {
Result =-1;
}
}
Return result;
}

});
Return rs;
}


Public static void main (String args []) {
String word = "Pinterest is might be aa AB marketer's dream-ths site is largely used to curate products ";
WordsStatistics s = new WordsStatistics ();
List <WordCount> rs = s. statistics (word );
For (WordCount word1: rs ){
System. out. println (word1.getWord () + "*" + word1.getCount ());
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.