Pages

2010年10月2日 星期六

Java String 依照單字出現次數/頻率高的依序印出

功能:
依照每個單字出現的次數,由大到小,排列印出。

假設:
String oriString = "This is a book. That is a pencil"

輸出:
is 出現 2 次
a 出現 2 次
This 出現 1 次
That 出現 1 次
book 出現 1 次
pencil 出現 1 次

程式碼:
  1. import java.util.regex.*;
  2. import java.util.*;
  3.  
  4. public class StringSort {
  5. public static void main(String[] args){
  6. String oriString = "This is a book. That is a pencil.";
  7. Pattern p = Pattern.compile("\\w[^\\.\\s]*");
  8. Matcher m = p.matcher(oriString);
  9. Set<string> sortVerb = new TreeSet<string>();
  10. ArrayList<string> strArray = new ArrayList<string>();
  11. while(m.find()){
  12. strArray.add(m.group());
  13. sortVerb.add(m.group());
  14. }
  15.  
  16. //計算oriString中的各個單字的次數
  17. int[] verbCount = new int[sortVerb.size()];
  18. int index = 0;
  19. for(String s : sortVerb){
  20. int count = 0;
  21. for(String sr : strArray){
  22. if(s.equals(sr))
  23. count++;
  24. }
  25. verbCount[index++] = count;
  26. }
  27.  
  28. //開始排序
  29. int[] indexArray = new int[sortVerb.size()];
  30. for(int i = 0; i < indexArray.length; i++)
  31. indexArray[i] = i;
  32. int temp, indexTemp;
  33. index = 0;
  34. for (int f = 1; f < sortVerb.size(); f++) {
  35. if (verbCount[f] < verbCount[f-1])
  36. continue;
  37. temp = verbCount[f];
  38. indexTemp = indexArray[f];
  39. index = f-1;
  40. while ((index >= 0)&&(verbCount[index] < temp)) {
  41. verbCount[index+1] = verbCount[index];
  42. indexArray[index+1] = indexArray[index];
  43. index--;
  44. }
  45. verbCount[index+1]=temp;
  46. indexArray[index+1] = indexTemp;
  47. }
  48. Object[] printStr = sortVerb.toArray();
  49. for(int i = 0; i < indexArray.length; i++)
  50. System.out.println(printStr[indexArray[i]].toString()+"出現 "+verbCount[i]+"次");
  51. }
  52. }
  53.  
執行結果:
  1. a出現 2
  2. is出現 2
  3. That出現 1
  4. This出現 1
  5. book出現 1
  6. pencil出現 1

Related Posts:

  • 自動擷取憑證與Https頁面資料在寫JSP使用Redirect的時候,如果是導向HTTPS頁面,也可能發生某些異常。 1. java.net.MalformedURLException: unknown protocol https ANS: 使用的方法不是 HTTPS 的方式,將jce.jar與jsse.jar導入classpath,在使用https連線函式 2. 頁面停留過久 ANS: 就像打開IE一樣,會叫你點選接受憑證,這時用程式跑就自動停住了,居然不會timeout … Read More
  • Java String 依照單字出現次數/頻率高的依序印出功能: 依照每個單字出現的次數,由大到小,排列印出。 假設: String oriString = "This is a book. That is a pencil" 輸出: is 出現 2 次 a 出現 2 次 This 出現 1 次 That 出現 1 次 book 出現 1 次 pencil 出現 1 次 程式碼: import java.util.regex.*; import java.util.*; public class… Read More
  • Big5部分中文顯示?處理最近在抓股票資料,但是因為有些上市櫃公司中文字,在預設Big5編碼中不支援,文字抓出來後會顯示?號,因此需要指定特定的編碼方式,才能正確的寫入資料庫中,目前Java的的解決方式如下: File sotckFile = new File(fileName); InputStreamReader isr = null; BufferedReader br = null; try { isr = new InputStreamReader(… Read More
  • 使用Triple DES 加密字串最近因為有需求,需要使用加密的方法來讓連線的帳號密碼用成亂碼來連線。 因此找到了使用 Tripple DES的方式加密字串。 1. 首先,先寄建立Decrypter與Encrypter package security.crypto; import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.IllegalBloc… Read More
  • Microsoft JDBC 連接 URL 範例 在下列範例中,範例程式碼會在連接 URL 中設定各種連接屬性,然後呼叫 DriverManager 類別的 getConnection 方法,以傳回SQLServerConnection 物件。 接著,範例程式碼會使用 SQLServerConnection 物件的 createStatement 方法建立 SQLServerStatemen… Read More

0 意見: