+ -
当前位置:首页 → 问答吧 → 我做的word词频统计

我做的word词频统计

时间:2011-05-18

来源:互联网

今天用字典+excel排序做了个word词频统计程序,没想到速度这么快。

Sub word词频排序()
'首先请引用excel11,我的是office2003
Application.ScreenUpdating = False
Dim dic As Object
Dim i, s
Dim f()
Dim m()
Set dic = CreateObject("scripting.dictionary")
For Each i In ActiveDocument.Words
s = CStr(i)
If i <> Chr(13) Then
If dic.Exists(s) Then
dic.Item(s) = dic.Item(s) + 1
Else
dic.Add (s), 1
End If
End If
Next
f = dic.keys
m = dic.items
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlApp.Visible = True
With xlSheet
[a2].Resize(dic.Count, 1) = xlBook.Application.Transpose(f)
[b2].Resize(dic.Count, 1) = xlBook.Application.Transpose(m)
[a1].Resize(1, 2) = Array("词条名称   ", "找到个数")
Range("B2:B" & dic.Count).Select
    Range("A2:B" & dic.Count).Sort Key1:=Range("B2"), Order1:=xlDescending, Header:= _
        xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        SortMethod:=xlPinYin, DataOption1:=xlSortNormal
End With
Application.ScreenUpdating = True
End Sub

作者: sqhsqhli   发布时间: 2011-05-18

好玩。顶一下学习学习。

作者: 香川群子   发布时间: 2011-05-18