Anonymous user
User:Klever: Difference between revisions
→KWIC index
(→VBA Examples: KWIC index) |
|||
Line 234:
'KWIC index
'assumptions:
' - all titles and catalog numbers can be held in an array in main memory
' - disregard punctuation in titles
' - the KWIC index itself may be too large for main memory - do not store it in memory
Line 241 ⟶ 240:
' - the catalog number
' - the title with the keyword centered in a line of given length (e.g. 80 or 120)
' (constant-
' note: long titles may be truncated at the beginning or the end of the line
Line 248 ⟶ 247:
Const STOPWORDS = "a an and by for is it of on or the to with " 'that last space is needed!
Dim title() As String 'list of titles to be included in KWIC index
Dim catno() As Integer 'list of catalog numbers
Dim ntitle As Integer 'number of titles
Dim index() As Integer 'holds title number and position of keyword in title
Line 253:
Sub ReadTitles()
' read or - in this case - set the titles and catalog numbers
ntitle = 10
ReDim title(1 To ntitle)
ReDim catno(1 To ntitle)
title(1) = "Microsoft Visio 2003 User's Guide"
title(2) = "Microsoft Office Excel 2003 Inside Out"
Line 266 ⟶ 267:
title(9) = "How to do Everything with Microsoft Office Excel 2003"
title(10) = "Data Analysis Using SQL and Excel"
catno(1) = 10
catno(2) = 13
catno(3) = 3435
catno(4) = 987
catno(5) = 1010
catno(6) = 1244
catno(7) = 709
catno(8) = 9088
catno(9) = 33
catno(10) = 7733
End Sub
Line 315 ⟶ 326:
switched = True
Do While switched
'scan array for two shifted strings in the wrong order and swap
'(swap the index entries, not the strings)
'use case-insensitive compare
switched = False
Line 321 ⟶ 333:
string1 = LCase(Shift(title(index(i, 1)), index(i, 2)))
string2 = LCase(Shift(title(index(i + 1, 1)), index(i + 1, 2)))
If string2 < string1 Then '
For j = 1 To 2
temp = index(i, j)
Line 336 ⟶ 348:
'print the KWIC index
spaces = Space(linelength / 2)
Debug.Print "
Debug.Print String(linelength +
For i = 1 To nkeys
atitle = title(index(i, 1))
pos = index(i, 2)
'create shifted string so that keyword is centered in the line
Line 346 ⟶ 357:
part1 = Right$(spaces & Left$(atitle, pos - 1), linelength / 2)
kwicstring = Right$(part1, linelength / 2) & Left$(part2, linelength / 2)
Debug.Print catno(index(i, 1)), "|"; kwicstring
Next
End Sub
Line 355 ⟶ 366:
'set array
ReDim index(ntitle * MAXKEYS, 2)
'index(.,1) is
'index(.,2) is keyword position in title
ProcessTitles
SortTitles
PrintKWIC
End Sub
</lang>
Output (note that some titles are truncated at the start or the end):
<pre>
kwic
</pre>
|