word VBA文档词频统计代码

合集下载

完整的中英文词频统计

完整的中英文词频统计

完整的中英⽂词频统计步骤:1.准备utf-8编码的⽂本⽂件file2.通过⽂件读取字符串 str3.对⽂本进⾏预处理4.分解提取单词 list5.单词计数字典 set , dict6.按词频排序 list.sort(key=)7.排除语法型词汇,代词、冠词、连词等⽆语义词8.输出TOP(20)⼀、.英⽂歌曲词频统计str2='''I will run, I will climb, I will soarI'm undefeatedJumpiing out of my skin, pull the chordYeah I believe itThe past, is everything we weredon't make us who we areSo I'll dream, until I make it real,and all I see is starsIts not until you fall that you flyWhen your dreams come alive you're unstoppableTake a shot, chase the sun, find the beautifulWe will glow in the dark turning dust to goldAnd we'll dream it possiblepossibleAnd we'll dream it possibleI will chase, I will reach, I will flyUntil I'm breaking, until I'm breakingOut of my cage, like a bird in the nightI know I'm changing, I know I'm changingIn, into something big, better than beforeAnd if it takes, takes a thousand livesThen it's worth fighting forIts not until you fall that you flyWhen your dreams come alive you're unstoppableTake a shot, chase the sun, find the beautifulWe will glow in the dark turning dust to goldAnd we'll dream it possibleit possibleFrom the bottom to the topWe're sparking wild fire'sNever quit and never stopThe rest of our livesFrom the bottom to the topWe're sparking wild fire'sNever quit and never stopIts not until you fall that you flyWhen your dreams come alive you're unstoppableTake a shot, chase the sun, find the beautifulWe will glow in the dark turning dust to goldAnd we'll dream it possiblepossibleAnd we'll dream it possible'''.lower()#aa = '''."?!'''#for word in aa:# str2 =str2.replace('word','')str2 =str2.replace('\n',' ')str2 =str2.replace(',',' ')print(str2)#去除特殊符号str2 = str2.strip()#去掉⾸尾空格str2 = str2.split()#通过指定分隔符对字符串进⾏切⽚print(str2)print('统计每个单词出现的次数为:')for word in str2:print(word,str2.count(word))strSet=set(str2)newSet={'a','will','it','out','of','my','the','i','in','to','when','and'}strSet1=strSet-newSet#去除介词和其他print(strSet1)strdict={} #单词计数字典for word in strSet1:strdict[word] = str2.count(word)print(len(strdict),strdict)strList = list(strdict.items())def takesecond(elem):#定义函数return elem[1]#strList.sort(key=lambda x:x[1],reverse=True)#匿名函数strList.sort(key=takesecond,reverse=True)#按照数值⼤⼩进⾏排序print(strList)for i in range(20):print (strList[i])#前⼆⼗2.中⽂⼩说词频统计import jieba;#准备utf-8编码的⽂本⽂件filef = open('doupo.txt','r',encoding='utf-8')#通过⽂件读取字符串 str,对⽂本进⾏预处理fo=f.read()f.close()print(fo)#⽤字典形式统计每个词的字数doupols = jieba.lcut(fo)doupodict = {}for word in doupols:if len(word)==1:continueelse:doupodict[word]=doupodict.get(word,0)+1print(doupodict)#cutprint(list(jieba.cut(fo))) #精确模式,将句⼦最精确的分开,适合⽂本分析print(list(jieba.cut(fo,cut_all=True))) #全模式,把句⼦中所有的可以成词的词语都扫描出来print(list(jieba.cut_for_search(fo))) #搜索引擎模式,在精确模式的基础上,对长词再次切分,提⾼召回率,适合⽤于搜索引擎分词#以列表返回可遍历的(键, 值) 元组数组wcList = list(doupodict.items())wcList.sort(key = lambda x:x[1],reverse=True) #出现词汇次数由⾼到低排序print(wcList)#第⼀个词循环遍历输出5次for i in range(5):print(wcList[1])。

word vba统计word文档基础信息

word vba统计word文档基础信息

统计word文档基础信息文/青爷用word vba可以很方便地统计一篇word文档的基础信息,包括一篇文档的节数、域代码数量、表格数、书签数、浮动式图片(Shape)数、嵌入式图片(inlineshape)数等等。

这样一目了然,可以迅速形成对文章结构的总体认知。

一般来说,一篇文档包含的节、域代码、表格等数量越多,文档结构越复杂,对其后期的修改排版也随之复杂。

================================================Sub d()Rem 统计word文档基础信息Rem 以下均不考虑页眉页脚i1 = ActiveDocument.Shapes.Counti2 = ActiveDocument.InlineShapes.Counti3 = ActiveDocument.Sections.Counti4 = ActiveDocument.Tables.Counti5 = ActiveDocument.Bookmarks.Counti6 = ActiveDocument.Fields.Counti7 = ActiveDocument.Frames.Counti8 = ActiveDocument.Footnotes.Counti10 = ActiveDocument.OMaths.Counti13 = rmation(wdNumberOfPagesInDocument) '页数i14 = ActiveDocument.Endnotes.Counti15 = puteStatistics(wdStatisticWords) '字数For Each oField In ActiveDocument.FieldsIf oField.Code.Text Like "*Equation*" Or oField.Code.Text Like "*EQUATION*" Then 'Mathtype公式数量iflag = iflag + 1End IfNextIf iflag = 0 Then i9 = 0 Else i9 = iflagFor Each oField In ActiveDocument.FieldsIf oField.Code.Text Like "*TOC*" Then '目录lflag = lflag + 1End IfNextIf lflag = 0 Then i16 = 0 Else i16 = lflagFor i = ActiveDocument.Shapes.Count To 1 Step -1If ActiveDocument.Shapes(i).Type = msoTextBox Then '文本框数量jflag = jflag + 1End IfNextIf jflag = 0 Then i11 = 0 Else i11 = jflagFor Each oField In ActiveDocument.Fields'Debug.Print oField.TypeIf oField.Type = wdFieldPageRef Or oField.Type = wdFieldTOC Or oField.Code.Text Like "*Toc*" Then '目录域数量k = k + 1End IfNextRem 统计所有表格的嵌套表格个数(只统计第一层嵌套表格,针对该word文档统计全局嵌套表格总数)For Each Tbl In ActiveDocument.Tableskflag = kflag + Tbl.Tables.CountNextIf kflag = 0 Then i12 = 0 Else i12 = kflagMsgBox "本文共" & i3 & "节!本文共" & i13 & "页!" & "平均每页" & Round(i15 / i13, 0) & "个字!" & Chr(13) & Chr(13) & "本文共" & i6 - k & "个非目录域(其中" & i9 & "个Mathtype公式," & i6 - k - i9 & "个其他域)!" _& Chr(13) & Chr(13) & "本文共" & i16 & "个目录!" & Chr(13) & Chr(13) & "本文共" & i10 & "个自带公式!" _& Chr(13) & Chr(13) & "本文共" & i4 & "个表格(其中" & i12 & "个嵌套表格)!" & Chr(13) & Chr(13) & "本文共" & i5 & "个书签!" _& Chr(13) & Chr(13) & "本文共" & i8 & "个脚注!本文共" & i14 & "个尾注!" & Chr(13) & Chr(13) & "本文共" & i2 - i9 & "个InlineShape(不含Mathtype公式)!" & Chr(13) & Chr(13) _& "本文共" & i1 & "个Shape(其中" & i11 & "个文本框)!" & "本文共" & i7 & "个图文框!"End Sub。

如何在Word中统计相同字符

如何在Word中统计相同字符

如何在Word中统计相同字符(文字)出现的个数首先,在Word窗口中,执行操作:“工具”→“宏”→“宏”,弹出如下图的对话框!上图中,在输入框中输入宏的名称,如“查找相同字符个数”,之后点击“创建”按钮;弹出如下图的“Microsoft Visual Basic”窗口,并且会自动产生一个录入代码的过程名称!在上图代码输入小窗口中,录入如下图的代码即可!以上的代码输入了之后,就在“Microsoft Visual Basic”窗口中执行如下图的操作!上述操作完成就会自动返回到Word窗口!之后,在Word窗口中执行操作:“工具”→“自定义”,弹出如下图的窗口!上图中,首先选择“命令”页框,之后在“类别”里面找到“宏”,之后在右边的“命令”里面找到如上图的宏名称,即刚才我们所编写的那代码的宏名,“查找相同字符个数”;之后,如下图一样,用鼠标左键拖动的办法,将这个宏拖入“编辑”菜单中,这个时候,这个宏的名称就和菜单中的命令一样,显示在该菜单里面了!上图中,拖入菜单之后,只要选择宏的名称,点击右键,就可以修改名称了,为方便使用,建议修改为“查找相同字符个数”,如下图!如何使用这宏名呢,这操作就太简单了,如上图一样执行操作,就会弹出一个对话框,如下图!输入要查找的字符,如“计算机”,之后点击确定按钮,则会弹出如下图的对话框,提示您找到多少个了!这个宏的功能非常实用,尽管操作过程有点儿麻烦,貌似有点罗嗦,但是您做好一次之后,以后就会在“编辑”菜单中显示了,以后直接点击就能使用了!如何在excell中统计相同字符(文字)出现的个数用COUNTIF(range,criteria),例如=countif(A1:A100,"天天")[文档可能无法思考全面,请浏览后下载,另外祝您生活愉快,工作顺利,万事如意!]。

计算输入文本中词语数量的程序

计算输入文本中词语数量的程序

计算输入文本中词语数量的程序
编写一个程序来计算输入文本中词语的数量是一个相对简单的
任务。

你可以使用任何你喜欢的编程语言来实现这个程序,比如Python、Java、C++等。

下面我以Python为例,给出一个简单的实现:
python.
def count_words(text):
words = text.split() # 通过空格分割文本得到单词列表。

return len(words) # 返回单词列表的长度即为单词数量。

input_text = input("请输入文本,")。

word_count = count_words(input_text)。

print("输入文本中包含的单词数量为,", word_count)。

这个程序首先定义了一个函数`count_words`,该函数接受一个
文本参数,然后通过空格分割文本得到单词列表,最后返回单词列
表的长度即为单词数量。

然后通过`input`函数获取用户输入的文本,调用`count_words`函数计算单词数量,并打印输出结果。

当然,这只是一个简单的实现,实际中可能需要考虑更多的情况,比如标点符号的处理、特殊字符的处理等。

但是基本的思路就
是将文本按照空格分割成单词,然后统计单词的数量。

希望这个示
例对你有所帮助。

统计word文档字数 ComputeStatistics

统计word文档字数 ComputeStatistics

Dim fp As String = TextBox1.TextDim fl() As String = Directory.GetFiles(fp, "*.docx", SearchOption.AllDirec tories)Dim dt As New DataTabledt.Columns.Add("路径")dt.Columns.Add("文件名")'dt.Columns.Add()dt.Columns.Add("字符数_不记空格_注释")dt.Columns.Add("字符数_不记空格")dt.Columns.Add("字符数_记空格_注释")dt.Columns.Add("字符数_记空格")dt.Columns.Add("字数_注释")dt.Columns.Add("字数")probar.Maximum = fl.Lengthprobar.Value = 0For Each fn As String In flDim fnl() As String = tools1.getfileinfo(fn)Dim wdApp As Microsoft.Office.Interop.Word.ApplicationwdApp = CreateObject("Word.Application")Dim c1, c2, c3, c4, c5, c6 As StringTry'wdApp.ActiveDocument.BuiltInDocumentProperties(Index)Dim doc As Microsoft.Office.Interop.Word.Documentdoc = wdApp.Documents.Open(fn)' doc.BuiltInDocumentProperties(Index) 这样也可以'Dim prop As Microsoft.Office.Interop.Word.WdBuiltInProperty'prop = doc.BuiltInDocumentProperties'Dim doc As New Aspose.Words.Document(fn)'Dim prop As Aspose.Words.Properties.BuiltInDocumentPropertiesDim stat As Microsoft.Office.Interop.Word.WdStatisticstat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticCharact ers'c1 = puteStatistics(Microsoft.Office.Interop.Word.WdStatist ic.wdStatisticCharacters, True)'c2 = puteStatistics(Microsoft.Office.Interop.Word.WdStatist ic.wdStatisticCharacters, False)c3 = puteStatistics(Microsoft.Office.Interop.Word.WdStatisti c.wdStatisticCharactersWithSpaces, True)c4 = puteStatistics(Microsoft.Office.Interop.Word.WdStatisti c.wdStatisticCharactersWithSpaces, False)c5 = puteStatistics(Microsoft.Office.Interop.Word.WdStatisti c.wdStatisticWords, True)c6 = puteStatistics(Microsoft.Office.Interop.Word.WdStatisti c.wdStatisticWords, False)doc.Close()wdApp.Quit()wdApp = NothingCatch ex As Exception'MsgBox(fn & ex.Message)wdApp.Quit()wdApp = NothingEnd Try'wdApp.Quit()'wdApp = NothingDim dr As DataRow = dt.NewRowdr(0) = fnl(0)dr(1) = fnl(1)dr(2) = c1dr(3) = c2dr(4) = c3dr(5) = c4dr(6) = c5dr(7) = c6dt.Rows.Add(dr)tsCount1.Text = fnprobar.PerformStep()Application.DoEvents()Nextdgv1.DataSource = dt。

词频统计的主要流程

词频统计的主要流程

词频统计的主要流程引言词频统计是一种非常常见且实用的文本分析方法,它可以揭示文本中词语的使用情况和重要性。

在文本挖掘、自然语言处理、信息检索等领域中,词频统计被广泛应用于文本预处理、特征提取和文本分类等任务中。

本文将介绍词频统计的主要流程,包括数据预处理、构建词汇表、计算词频和排序等关键步骤。

我们将逐步深入探讨这些步骤,并给出详细的示例代码,以帮助读者更好地理解词频统计的过程和方法。

数据预处理在进行词频统计之前,需要对原始文本进行预处理,以便去除无用的标点符号、停用词等干扰因素,并将文本转换为合适的形式进行处理。

数据预处理的具体步骤如下: 1. 将文本转换为小写字母,以避免大小写的差异对统计结果造成影响。

2. 去除标点符号,包括句号、逗号、双引号等。

3. 去除停用词,停用词是指在文本分析中无实际含义的高频词汇,如“的”、“了”、“是”等。

常用的停用词列表可以从开源项目或自然语言处理工具包中获取。

4. 进行词干提取,将词语的不同形式转换为其原始形式。

例如,将单词的复数形式、时态变化等转换为词干形式。

5. 分词,将文本按照词语为单位进行切分。

常用的中文分词工具包有jieba、snownlp等。

下面给出一个示例代码,展示如何对原始文本进行数据预处理:import reimport stringfrom nltk.corpus import stopwordsfrom nltk.stem import SnowballStemmerimport jiebadef preprocess_text(text):# 将文本转换为小写text = text.lower()# 去除标点符号text = text.translate(str.maketrans('', '', string.punctuation)) # 去除停用词stop_words = set(stopwords.words('english')) # 英文停用词text = ' '.join([word for word in text.split() if word not in stop_words]) # 进行词干提取stemmer = SnowballStemmer('english')text = ' '.join([stemmer.stem(word) for word in text.split()]) # 中文分词text = ' '.join(jieba.cut(text))return text# 示例文本text = "Hello, world! This is a sample text."preprocessed_text = preprocess_text(text)print(preprocessed_text)以上代码演示了如何对英文文本进行预处理。

vba_统计Word文档字数、页数等信息方法探讨

vba_统计Word文档字数、页数等信息方法探讨

VBA 统计Word 字数、页数方法探讨我们在日常工作若需要文档的字数和页数,或是VBA编程处理文档时需要用的文档的字数或页数作参数。

那么如何获得一个文档的字数和页数,VBA有多种方法获得文档的字数和页数,那么具体如何获得字数和页数?获的数字到底代表了什么意义?与我们平时理解除的文档字数和页数有什么异同?相信你看完本文后,应该就明白了。

1. 使用Document.BuiltInDocumentProperties方法统计字数1.1第一种方法的实例1及结论我们先来看一个例子:如要统计一篇活动word文档的字数,可以在VBE代码窗口中输入以下代码:Public Sub getword()'显示当前文档的字数MsgBox "当前文档的的总字数为:" & ActiveDocument.BuiltInDocument Properties(wdPropertyWords)End Sub1、在代码窗口中输入上述代码,如图1-1所示。

运行按钮图1-12、点运行按钮,运行结果如下图1-2所示。

3、对此统计的数字我进行了多次测试,特作如下说明:(1)这里统计的字数是指汉字的字数+标点符号+加英文的单词数,英文的单词不区分大小写和正角性,只要有空格分开的就算两个字,如:Seeto、see,to、s ee.to、wdStatisticCharacters都是算一个字,但See to就是两个字,而see,to则算3个字。

图1-2程序运行结果(2)空格再多都不算作字,如“黄山村夫”和“黄 山 村 夫”都是算4个字。

(3)中文的引号只有配对后才能算一个字,否则不算。

(4)西文的标点符号与西文相连时不算字,但与中文相连时就算一个字,如:A,或a.都是算一个字,而“我,”或“我.”都是算2个字。

1.2 第一种方法的实例2及结论我们来看实例2,利用VBA 的ActiveDocument 对象的BuiltInDocumentPrope rties ,统计一篇活动word 文档的总页数,可以在VBE 代码窗口中输入以下代码:Public Sub getpagenumbers()'显示当前文档的页数MsgBox "当前文档的总页数为:" & ActiveDocument.BuiltInDocumentPro perties(wdPropertyPages)End Sub1、在代码窗口中输入上述代码,如图1-3所示。

vba 统计Word 字数、页数等信息

vba 统计Word 字数、页数等信息

VBA 统计Word 字数、页数等信息方法一. 使用Document.BuiltInDocumentProperties 方法统计适用于Document对象和Template 对象。

返回一个DocumentProperties 集合,该集合代表了指定文档的所有内置的文档属性。

可使用的参数有:wdPropertyAppName 应用程序名.wdPropertyAuthor 作者.wdPropertyBytes 字节数.wdPropertyCategory 类别.wdPropertyCharacters 字符数.wdPropertyCharsWSpaces 字符数(计空格).wdPropertyComments 批注.wdPropertyCompany 公司.wdPropertyKeywords 关键词.wdPropertyLastAuthor 上一个作者.wdPropertyLines 行数.wdPropertyManager 经理.wdPropertyNotes 注释.wdPropertyPages 页数.wdPropertyParas 段数.wdPropertyRevision 修订次数.wdPropertySecurity 安全性.wdPropertySubject 主题.wdPropertyTemplate 模板.wdPropertyTimeCreated 创建时间.wdPropertyTimeLastPrinted 上次打印时间.wdPropertyTimeLastSaved 上次保存时间.wdPropertyTitle 标题.wdPropertyVBATotalEdit 编辑时间总计.wdPropertyWords 字数.如要统计一篇活动word文档的字数:ActiveDocument.BuiltInDocumentProp erties(wdPropert yWords)方法二.使用puteStatistics方法统计可使用的参数如下:wdStatisticCharacters 字符数.wdStatisticCharactersWithSpaces 字符数(计空格).wdStatisticFarEastCharacters 中文字符和朝鲜文.wdStatisticLines 行数.wdStatisticPages 页数.wdStatisticParagraphs 段数.wdStatisticWords 字数.使用如下:统计活动文档的字数(包括脚注):1.应用于Document 对象的ComputeStatistics 方法。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Sub 词频统计()
'
' 词频统计Macro
'
Dim SingleWord As String '从当前文档提取的一个单词
Const maxWords = 15000 '允许出现的不同单词的最大数量,如不够,可适当加大Dim Words(maxWords) As String '用来保存各个不同的单词
Dim Freq(maxWords) As Integer '出现频度计数器
Dim WordNum As Integer '不同单词的数量
Dim ByFreq As Boolean '输出结果的排序标准
Dim ttlwds As Long '文档中的单词总数
Dim Excludes As String '不在统计范围内的单词
Dim Found As Boolean '临时标记
Dim j, k, l, Temp As Integer '临时变量
Dim tWord As String '
' 设置要排除的单词。

' 英文排除词:[the][a][of][is][to][for][this][that][by][be][and][are]
' 排除词可以从各大搜索引擎的说明获得,可根据实际情况修改
Excludes = "[][的][是]"
' 向用户询问排序标准
ByFreq = True
ans = InputBox$("根据单词(1)还是频度(2)排序?", "排序标准", "1")
If ans = "" Then End
If Trim(ans) = "1" Then
ByFreq = False
End If
'开始分析文档
Selection.HomeKey Unit:=wdStory
System.Cursor = wdCursorWait
WordNum = 0
ttlwds = ActiveDocument.Words.Count
' 处理文档中的每个单词
For Each aWord In ActiveDocument.Words
'英文单词不区分大小写
SingleWord = Trim(LCase(aWord))
'该单词是否在排除列表中?
If InStr(Excludes, "[" & SingleWord & "]") Then SingleWord = ""
If Len(SingleWord) > 0 Then
'找到一个需要处理的单词
Found = False
For j = 1 To WordNum
If Words(j) = SingleWord Then
' 这个单词已经出现过了
' 把它的出现频度加1
Freq(j) = Freq(j) + 1
Found = True
Exit For
End If
Next j
If Not Found Then
' 这个单词还没有出现过
' 将它登记为一个新的单词
' 出现频度设置为1
WordNum = WordNum + 1
Words(WordNum) = SingleWord
Freq(WordNum) = 1
End If
If WordNum > maxWords - 1 Then
j = MsgBox("已达到单词数量的最大限制值。

请增加maxWords的值.", vbOKOnly) Exit For
End If
End If
ttlwds = ttlwds - 1
'在状态栏上显示处理进度
StatusBar = "剩余:" & ttlwds & " 不同单词数量: " & WordNum
Next aWord
' 对处理结果进行排序
For j = 1 To WordNum - 1
k = j
For l = j + 1 To WordNum
If (Not ByFreq And Words(l) < Words(k)) Or (ByFreq And Freq(l) > Freq(k)) Then k = l Next l
If k <> j Then
tWord = Words(j)
Words(j) = Words(k)
Words(k) = tWord
Temp = Freq(j)
Freq(j) = Freq(k)
Freq(k) = Temp
End If
'排序进度
StatusBar = "正在排序:" & WordNum - j
Next j
' 将统计结果显示到一个新的Word文档
tmpName = ActiveDocument.AttachedTemplate.FullName
' 创建一个新文档
Documents.Add Template:=tmpName, NewTemplate:=False
'清除...
Selection.ParagraphFormat.TabStops.ClearAll
' 将处理结果写入新文档,每个单词一行
With Selection
For j = 1 To WordNum
.TypeText Text:=Trim(Str(Freq(j))) & vbTab & Words(j) & vbCrLf
Next j
End With
System.Cursor = wdCursorNormal
j = MsgBox("该文档总共有" & Trim(Str(WordNum)) & "个不同的单词。

", vbOKOnly, "分析完毕!")
End Sub。

相关文档
最新文档