VBA统计WORD文档中汉字、英文及其组合出现的次数,并输出

180it 2019-09-25 PM 4141℃ 0条

使用前,需要在word内插入一个textbox1和一个commandbutton1,然后去掉文档中的标点符号。笔者比较懒,未加入剔除标点的代码。

理论上下述代码可以统计999个字符(含标点)的文本,如需增加文本数量,则只需要增加数组体积即可。
代码只是通过比较简单的for ,do循环实现,没啥技术含量哈。

Sub Button1_click()

'统计词频
Dim input_word As String
Dim input_word_part(1 To 9999) As String
Dim output_word_part(1 To 9999) As String
Dim temp_input_word_part As String
Dim count_word(1 To 9999) As Integer
Dim count_output As String
Dim input_len As Integer
Dim i, j, k, p As Integer

'i为数组序号,'j为字符长度
'input_word_part()为全部元素
'output_word_part()为去重后元素

input_word = Trim(TextBox1.Text)
input_len = Len(input_word)

'数组赋值
i = 1
j = 1
Do While j <= 9

If Len(Mid(input_word, i, j)) = j Then
    input_word_part(j * 1000 + i) = Mid(input_word, i, j)
    temp_input_word_part = Mid(input_word, i, j)
End If
 i = i + 1
 
If i = 1000 Then
    MsgBox "字符长度为" & j & "的数据量超限!"
    Exit Do
End If

If Mid(input_word, i, j) = "" Then
    i = 1
    j = j + 1
End If

Loop

'元素去重
i = 2 'input指针
j = 1 '为字符长度
k = 1 'output比较指针
p = 2 'output指针
Do While j <= 9

output_word_part(j * 1000 + 1) = input_word_part(j * 1000 + 1)
temp_input_word_part = input_word_part(j * 1000 + i)
    
    
 p = 2
 k = 1
    Do
    If temp_input_word_part = output_word_part(j * 1000 + k) Then
        Exit Do

    ElseIf temp_input_word_part <> output_word_part(j * 1000 + k) Then
        If output_word_part(j * 1000 + p) <> "" Then
        p = p + 1
        ElseIf output_word_part(j * 1000 + p) = "" Then
        output_word_part(j * 1000 + p) = temp_input_word_part
        p = p + 1
        End If
    End If
        k = k + 1
        If input_word_part(j * 1000 + k) = "" Then
        Exit Do
        End If
    Loop

i = i + 1


If input_word_part(j * 1000 + i) = "" Then
j = j + 1
i = 2
End If

Loop

'统计数组元素出现频率

i = 1 'input指针
j = 1 '为字符长度
p = 1 'output指针

Do While j <= 9
temp_input_word_part = output_word_part(j * 1000 + p)

i = 1
Do
If output_word_part(j * 1000 + p) <> "" Then

    If input_word_part(j * 1000 + i) = temp_input_word_part Then
    count_word(j * 1000 + p) = count_word(j * 1000 + p) + 1
    i = i + 1
        If input_word_part(j * 1000 + i) = "" Then
        Exit Do
        End If
    
    
    ElseIf input_word_part(j * 1000 + i) <> temp_input_word_part Then
    i = i + 1
        If input_word_part(j * 1000 + i) = "" Then
        Exit Do
        End If
        
    End If
    
ElseIf output_word_part(j * 1000 + p) = "" Then
    Exit Do
    
End If
Loop

p = p + 1
If output_word_part(j * 1000 + p) = "" Then
p = 1
j = j + 1
End If

Loop

'输出结果
p = 1 'output指针

For j = 2 To 9 '通常两个汉字以上才可称为词语,未列出单个汉字的出现频率

Do
count_output = count_output & "“" & output_word_part(j * 1000 + p) & "”的数量为:" & count_word(j * 1000 + p) & Chr(13)
p = p + 1
    If output_word_part(j * 1000 + p) = "" Then
        j = j + 1
        p = 1
        Exit Do
    End If
Loop

Next

Documents.Add.Content.Text = "统计结果如下:" & Chr(13) & count_output

End Sub
————————————————

原文链接:https://blog.csdn.net/weixin_44559388/article/details/87861395

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

VBA统计WORD文档中汉字、英文及其组合出现的次数,并输出