Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

txt parser

0 views
Skip to first unread message

Bernye

unread,
Dec 22, 2009, 11:57:01 AM12/22/09
to
Hi Folks,

I need routine in vba that counts the number of occurency of all the words
in e txt document and creates e txt report with a list of all the words of
the text and the number of occurencies for each word.

Could you help me to find out a solution?

Thank you very much


Fumei2 via OfficeKB.com

unread,
Dec 22, 2009, 12:54:16 PM12/22/09
to
What have you tried so far? Please post any code you have come with.

--
Message posted via http://www.officekb.com

Fumei2 via OfficeKB.com

unread,
Dec 22, 2009, 12:55:26 PM12/22/09
to

Oh, and be specific. Do you really want to get the number of occurences of
the word "the" for example? Or "a"? Or "and"?

Fumei2 wrote:
>What have you tried so far? Please post any code you have come with.
>

>>Hi Folks,
>>
>[quoted text clipped - 5 lines]
>>
>>Thank you very much

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200912/1

Doug Robbins - Word MVP

unread,
Dec 22, 2009, 2:15:53 PM12/22/09
to

Sub WordFrequency()

Dim SingleWord As String 'Raw word pulled from doc
Const maxwords = 9000 'Maximum unique words allowed
Dim Words(maxwords) As String 'Array to hold unique words
Dim Freq(maxwords) As Integer 'Frequency counter for Unique
Words
Dim WordNum As Integer 'Number of unique words
Dim ByFreq As Boolean 'Flag for sorting order
Dim ttlwds As Long 'Total words in the document
Dim Excludes As String 'Words to be excluded
Dim Found As Boolean 'Temporary flag
Dim j, k, l, Temp As Integer 'Temporary variables
Dim tword As String '

' Set up excluded words
' Excludes =
"[the][a][of][is][to][for][this][that][by][be][and][are]"
Excludes = ""
Excludes = InputBox$("Enter words that you wish to exclude,
surrounding each word with [ ].", "Excluded Words", "")
' Excludes = Excludes & InputBox$("The following words are excluded:
" & Excludes & ". Enter words that you wish to exclude, surrounding each
word with [ ].", "Excluded Words", "")
' Find out how to sort
ByFreq = True
Ans = InputBox$("Sort by WORD or by FREQ?", "Sort order", "FREQ")
If Ans = "" Then End
If UCase(Ans) = "WORD" Then
ByFreq = False
End If
Selection.HomeKey Unit:=wdStory
System.Cursor = wdCursorWait
WordNum = 0
ttlwds = ActiveDocument.Words.Count
Totalwords = ActiveDocument.BuiltInDocumentProperties(wdPropertyWords)
' Control the repeat
For Each aword In ActiveDocument.Words
SingleWord = Trim(aword)
If SingleWord < "A" Or SingleWord > "z" Then SingleWord = ""
'Out of range?
If InStr(Excludes, "[" & SingleWord & "]") Then SingleWord = ""
'On exclude list?
If Len(SingleWord) > 0 Then
Found = False
For j = 1 To WordNum
If Words(j) = SingleWord Then
Freq(j) = Freq(j) + 1
Found = True
Exit For
End If
Next j
If Not Found Then
WordNum = WordNum + 1
Words(WordNum) = SingleWord
Freq(WordNum) = 1
End If
If WordNum > maxwords - 1 Then
j = MsgBox("The maximum array size has been exceeded.
Increase maxwords.", vbOKOnly)
Exit For
End If
End If
ttlwds = ttlwds - 1
StatusBar = "Remaining: " & ttlwds & " Unique: " & WordNum
Next aword

' Now sort it into word order
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 = "Sorting: " & WordNum - j
Next j

' Now write out the results
tmpName = ActiveDocument.AttachedTemplate.FullName
Documents.Add Template:=tmpName, NewTemplate:=False
Selection.ParagraphFormat.TabStops.ClearAll
With Selection
For j = 1 To WordNum
.TypeText Text:=Words(j) & vbTab & Trim(Str(Freq(j))) &
vbCrLf
Next j
End With
ActiveDocument.Range.Select
Selection.ConvertToTable
Selection.Collapse wdCollapseStart
ActiveDocument.Tables(1).Rows.Add BeforeRow:=Selection.Rows(1)
ActiveDocument.Tables(1).Cell(1, 1).Range.InsertBefore "Word"
ActiveDocument.Tables(1).Cell(1, 2).Range.InsertBefore
"Occurrences"
ActiveDocument.Tables(1).Range.ParagraphFormat.Alignment =
wdAlignParagraphCenter
ActiveDocument.Tables(1).Rows.Add
ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.InsertBefore "Total words in Document"
ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
2).Range.InsertBefore Totalwords
ActiveDocument.Tables(1).Rows.Add
ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.InsertBefore "Number of different words in Document"
ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
2).Range.InsertBefore Trim(Str(WordNum))
System.Cursor = wdCursorNormal
' j = MsgBox("There were " & Trim(Str(WordNum)) & " different words
", vbOKOnly, "Finished")
Selection.HomeKey wdStory

End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Bernye" <a...@dld.dl> wrote in message
news:xT6Ym.112759$9f6.1...@twister1.libero.it...

Karl E. Peterson

unread,
Dec 22, 2009, 2:40:24 PM12/22/09
to
Doug Robbins - Word MVP used his keyboard to write :
> Sub WordFrequency()
<snip>

Think Bernye will get an A for that? ;-)

--
.NET: It's About Trust!
http://vfred.mvps.org


Bernye

unread,
Dec 24, 2009, 5:14:08 AM12/24/09
to

"Karl E. Peterson" <ka...@exmvps.org> ha scritto nel messaggio
news:%23Dn4c6z...@TK2MSFTNGP05.phx.gbl...

> Doug Robbins - Word MVP used his keyboard to write :
>> Sub WordFrequency()
> <snip>
>
> Think Bernye will get an A for that? ;-)

For What?
Well, I'd want to parse an english book (txt) to build a statistics of the
number of word occurences.
I don't speak english very well so I'd want to learn it by watching tv,
speaking with mother tongue people and.... reading books.
If I build a list of the more frequent words apperaring in the book, i can
read it more fastly and i can fix better in my mind those words.

I ve already wrote VBA code to my technical specific purpose and I'm going
to parse the vba code to understand how it works!

*****Thank you very much!!!*****

I will let you know the results of my analysis!!

Merry Christmas to all!


Bernye

unread,
Dec 24, 2009, 5:17:56 AM12/24/09
to

Written! Written! I mean I've already Written! :-D


"Bernye" <a...@dld.dl> ha scritto nel messaggio
news:Q9HYm.113437$9f6.1...@twister1.libero.it...

Doug Robbins - Word MVP

unread,
Dec 24, 2009, 6:35:31 PM12/24/09
to
Karl may have thought that you asked the question in connection with a
school homework exercise

The ;-) at the end of his post is a smiley for wink. That is, his
suggestion was in jest.

Sorry if the above introduces a whole new set of English terms for you to
understand.

While English is my mother tongue, having spent many years in countries
where English is not the language, I do realise how difficult a language it
is for non-English speaking people to master.

Good luck with your attempt.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Bernye" <a...@dld.dl> wrote in message

news:Q9HYm.113437$9f6.1...@twister1.libero.it...

Bernye

unread,
Dec 28, 2009, 4:59:14 PM12/28/09
to

"Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org> ha scritto nel messaggio
news:97F990A2-95C1-47B9...@microsoft.com...

>
> Sub WordFrequency()
>
> Dim SingleWord As String 'Raw word pulled from doc
> Const maxwords = 9000 'Maximum unique words allowed
> Dim Words(maxwords) As String 'Array to hold unique words
> Dim Freq(maxwords) As Integer 'Frequency counter for Unique
[CUT]


It works very well!
It's what I need.
Thanks a lot.

Bernye


Karl E. Peterson

unread,
Jan 6, 2010, 9:42:47 PM1/6/10
to
Doug Robbins - Word MVP used his keyboard to write :
> Karl may have thought that you asked the question in connection with a school
> homework exercise

Guilty. It sure sounded like one to me.

> The ;-) at the end of his post is a smiley for wink. That is, his
> suggestion was in jest.

Right, thanks...

0 new messages