• 解析文本中单词出现的次数小程序(JAVA)


    在网上找了个题目练习了下,统计某个文本中单词出现的次数,也是用了状态机实现。

    word类:记录单词及其次数

    View Code
     1 package com.app;
    2
    3 public class word {
    4 private String wordvalue;
    5 private double wordnum;
    6
    7 public String getWordvalue() {
    8 return wordvalue;
    9 }
    10 public void setWordvalue(String wordvalue) {
    11 this.wordvalue = wordvalue;
    12 }
    13 public double getWordnum() {
    14 return wordnum;
    15 }
    16 public void setWordnum(double wordnum) {
    17 this.wordnum = wordnum;
    18 }
    19
    20 }

    算法实现:

    View Code
      1 package com.app;
    2
    3 import java.io.File;
    4 import java.io.FileInputStream;
    5 import java.io.FileNotFoundException;
    6 import java.io.IOException;
    7 import java.io.InputStream;
    8 import java.io.InputStreamReader;
    9 import java.io.Reader;
    10 import java.util.ArrayList;
    11 import java.util.List;
    12
    13 public class processFile {
    14 private File orifile;
    15 private List<word> storeList;
    16
    17 enum type{
    18 YES, //当前是字母
    19 NO, //当前不是字母
    20 }
    21
    22 private type curtype;
    23
    24 public processFile(String string) {
    25 // TODO Auto-generated constructor stub
    26 orifile = new File(app.class.getResource(string).getFile());
    27 storeList = new ArrayList<word>();
    28 curtype = type.NO;
    29 }
    30
    31 public boolean isWord(int temp){
    32
    33 if(((temp <= 'z')&&(temp >= 'a'))
    34 ||((temp <= 'Z')&&(temp >= 'A'))){
    35 return true;
    36 }
    37
    38 return false;
    39 }
    40
    41 public void addWord(String word){
    42 for(int i = 0; i < storeList.size(); i++){
    43 if(storeList.get(i).getWordvalue().equalsIgnoreCase(word)){
    44 storeList.get(i).setWordnum(storeList.get(i).getWordnum() + 1);
    45 return;
    46 }
    47 }
    48
    49 word wd = new word();
    50 wd.setWordvalue(word);
    51 wd.setWordnum(1);
    52 storeList.add(wd);
    53
    54 return;
    55 }
    56
    57 public void showWord(){
    58
    59 for(int i = 0; i < storeList.size(); i++){
    60 System.out.println(storeList.get(i).getWordvalue() + ":"
    61 + storeList.get(i).getWordnum());
    62 }
    63
    64 return;
    65 }
    66
    67 public void process(){
    68
    69 InputStream in;
    70 try {
    71 in = new FileInputStream(orifile);
    72 Reader reader = new InputStreamReader(in);
    73
    74 int curtemp;
    75 StringBuffer temp = new StringBuffer();
    76
    77 while(-1 != (curtemp = reader.read())){
    78 if(type.NO == curtype){
    79 if(isWord(curtemp)){
    80 temp.append((char)curtemp);
    81 curtype = type.YES;
    82 }
    83 }else{
    84 if(isWord(curtemp)){
    85 temp.append((char)curtemp);
    86 }else{
    87 curtype = type.NO;
    88
    89 if(temp.length() != 0){
    90 addWord(temp.toString());
    91 temp.delete(0, temp.length());
    92 }
    93 }
    94 }
    95 }
    96
    97 } catch (FileNotFoundException e) {
    98 // TODO Auto-generated catch block
    99 e.printStackTrace();
    100 } catch (IOException e) {
    101 // TODO Auto-generated catch block
    102 e.printStackTrace();
    103 }
    104
    105
    106 return;
    107 }
    108 }




  • 相关阅读:
    如何经营爱情!
    document.getElementById('myframe')和window.frames[i]的区别
    [WPF] 使用 MVVM Toolkit 构建 MVVM 程序
    [WPF] 使用 Visual Studio App Center 持续监视应用使用情况和问题
    centos 7执行yum update时在clean up阶段挂住
    实现线性结构转树形结构(生成无限层级菜单)
    中文分词——HMM算法
    中文分词——最大匹配法
    爱因斯坦求和约定
    jupyter notebook美化
  • 原文地址:https://www.cnblogs.com/fredric/p/2431284.html
Copyright © 2020-2023  润新知