首页 > 技术文章 > Huffman 统计词频(C#)

Awara 2015-11-30 18:25 原文

用C#语言实现简单的词频统计.

 1 using System;
 2 using System.Collections;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace HashTestStati
12 {
13     public partial class Form1 : Form
14     {
15         static int i = 0;//定义一个静态变量i
16 
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void BXS_Click(object sender, EventArgs e)
23         {
24             string[] files = new string[3];//定义一个数组
25             files[0] = " My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. ";
26             files[1] = "When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. ";
27             files[2] = "Occasionally, Dad would get out his mandolin and play for the family.";
28             i++;//i自加
29             BXS.Text = "文件显示" + i;//实现按钮的显示
30             TXS.Text = files[i - 1];//把文章显示在textbox中
31 
32         }
33         //按下按钮显示统计数据
34               private void BTJ_Click(object sender, EventArgs e)
35         {
36             string strtext_low;
37             strtext_low = upperlower();
38             ArrayList strword = new ArrayList();
39             strword = separateword(strtext_low);
40             Hashtable hs = new Hashtable();
41             hs = hashcreate(strword);
42             TXS.Text = "";
43             foreach (DictionaryEntry de in hs)//输出
44             {
45                 string str = string.Format("{0,-15} {1,3}", de.Key, de.Value);
46                 TXS.Text = TXS.Text + str + "\r\n";
47             }  
48         }
49         //往哈夫曼树中写入方法
50               private Hashtable hashcreate(ArrayList strword) {
51 
52                   Hashtable ht = new Hashtable();
53                   foreach (string i in strword)//遍历wtrword
54                       if (!ht.Contains(i))
55                           ht.Add(i, 1);
56                       else
57                       {
58                           ht[i] = (int)ht[i] + 1;
59                       }
60                   return ht;
61               }
62 
63 
64             //定义一个转化方法
65               private ArrayList separateword(string strtext_low) {
66 
67                   ArrayList sepword = new ArrayList();//定义一个动态数组用于传进已转换小写的数组
68                   string strtext = strtext_low;
69                   strtext = strtext.Replace(',', ' ');//用Replace()方法将‘,’,‘。’转化为空格
70                   strtext = strtext.Replace('.', ' ');
71                    string[] word = new string[500];//定义一个500长度的数组接收转换完成的单词
72                    word = strtext.Split(' ');//使用Split()方法分割单词
73                    foreach (string i in word)//遍历循环将其传入动态数组sepword
74                        sepword.Add(i);
75                    return sepword;
76               }
77 
78 
79 
80      
81 
82         //定义一个转化小写方法
83         private string upperlower() {
84 
85             return TXS.Text.ToLower();//返回小写值
86         }
87 
88       
89 
90       
91     }
92 }

 

推荐阅读