首页 > 代码库 > 词频统计(WEB版)

词频统计(WEB版)

通过点击浏览按钮输入文件:

技术分享

点击查询按钮后返回结果:

技术分享

前台代码:

技术分享
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <h4>词频统计器
13             </h4>
14     <div>
15         <p>
16             请选择文件:<asp:FileUpload ID="FileUpload1" runat="server" />
17         </p>
18         <p>
19             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 21px; width: 62px" Text="查询" />
20         </p> 
21     </div>
22         <div>
23           
24             <asp:TextBox ID="TextBox1" runat="server" Height="300px" TextMode="MultiLine" Width="326px" Enabled="False"></asp:TextBox>
25           
26         </div>
27     </form>
28 </body>
29 </html>
View Code

后台代码:

技术分享
 1 using System;
 2 using System.Collections;
 3 using System.Configuration;
 4 using System.Data;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.Security;
 8 using System.Web.UI;
 9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Text;
14 using System.Collections.Generic;
15 using System.IO;
16 
17 public partial class _Default : System.Web.UI.Page
18 {
19     protected void Page_Load(object sender, EventArgs e)
20     {
21     }
22 
23     protected void Button1_Click(object sender, EventArgs e)
24     {
25         if(FileUpload1.HasFiles)
26         {
27             int n=0;
28             string strfile = FileUpload1.PostedFile.FileName;
29             string strout;
30             StreamReader sr = File.OpenText(strfile);
31             String input = sr.ReadToEnd();
32             sr.Close();
33             //Response.Write(input);
34             char [] text = input.ToCharArray();
35             Dictionary<string, int> map = new Dictionary<string, int>();
36             for(int i=0;i<text.Length;i++)
37             {
38                 string s = "";
39                 while (i< text.Length&&((text[i] >= a && text[i] <= z) || (text[i] >= A && text[i] <= Z) || text[i] == -))
40                 {
41                     if (text[i] >= A && text[i] <= Z)
42                         s += (text[i] + 32);
43                     else
44                         s += text[i];
45                     i++;
46                 }
47                 if (!map.ContainsKey(s))
48                 {
49                     if (s == "") continue;
50                     n++;
51                     map.Add(s, 1);
52                 }
53                 else
54                 {
55                     map[s]++;
56                 }                  
57             }
58             strout = "";
59             strout = "单次总数为:" + n.ToString() + "\n";
60             List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(map);
61             myList.Sort(delegate (KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
62             {
63                 return s2.Value.CompareTo(s1.Value);
64             });
65             map.Clear();
66 
67             foreach (KeyValuePair<string, int> pair in myList)
68             {
69                 strout = strout + pair.Key + " " + pair.Value + "\n";
70                 //dic.Add(pair.Key, pair.Value);
71 
72             }
73 
74             
75         
76             TextBox1.Text = strout;
77            
78         }  
79     }
80 }
View Code

 

词频统计(WEB版)