User:Klever: Difference between revisions

(→‎VBA Examples: KWIC index)
Line 234:
'KWIC index
'assumptions:
' - all titles and catalog numbers can be held in an array in main memory
' - use the index in the array as the "catalog number"
' - 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-pitchwidth font assumed)
' 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 them
'(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 'switchswap
For j = 1 To 2
temp = index(i, j)
Line 336 ⟶ 348:
'print the KWIC index
spaces = Space(linelength / 2)
Debug.Print "IndexCat. number", "|"; Space((linelength - 10) / 2); "KWIC string"
Debug.Print String(linelength + 1415, "-")
For i = 1 To nkeys
atitle = title(index(i, 1))
ltitle = Len(atitle)
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 catalog nr. (here, equal to title nr.)
'index(.,2) is keyword position in title
ProcessTitles
SortTitles
PrintKWIC (120)80 'argument is the length of the KWIC titleslines (excluding catalog numbers)
End Sub
</lang>
 
Output (note that some titles are truncated at the start or the end):
Output:
<pre>
kwic
IndexCat. number | KWIC string
--------------------------------------------------------------------------------------------------------------------------------------
4 987 | Excel 2003 Formulas
9 33 | How to do Everything with Microsoft Office Excel 2003
2 13 | Microsoft Office Excel 2003 Inside Out
3 3435 | Mastering Excel 2003 Programming with VBA
1 10 | Microsoft Visio 2003 User's Guide
6 1244 | Excel 2003 VBA Programmer's Reference
8 9088 | Beginning Excel: What-if Data Analysis Tools
7 709 | Automated Data Analysis Using Excel
10 7733 | Data Analysis Using SQL and Excel
7 709 | Automated Data Analysis Using Excel
8 9088 | Beginning Excel: What-if Data Analysis ToolsT
8 9088 | Beginning Excel: What-if Data Analysis Tools
7 709 | Automated Data Analysis Using Excel
10 7733 | Data Analysis Using SQL and Excel
9 33 | How to do Everything with Microsoft Office Excel 2003Exce
5 1010 | Excel for Scientists and Engineers
9 33 | How to do Everything with Microsoft Office Excel 20032
4 987 | Excel 2003 Formulas
9 33 | How to do Everything with Microsoft Office Excel 2003
2 13 | Microsoft Office Excel 2003 Inside Out
3 3435 | Mastering Excel 2003 Programming with VBA
6 1244 | Excel 2003 VBA Programmer's Reference
7 709 | Automated Data Analysis Using Excel
10 7733 | Data Analysis Using SQL and Excel
5 1010 | Excel for Scientists and Engineers
8 9088 | Beginning Excel: What-if Data Analysis Tools
4 987 | Excel 2003 Formulas
1 10 | Microsoft Visio 2003 User's Guide
9 33 | How to do Everything with Microsoft Office Excel 2003Offi
2 13 | Microsoft Office Excel 2003 Inside Out
3 3435 | Mastering Excel 2003 Programming with VBAVB
9 33 | How to do Everything with Microsoft Office Excel 2003
2 13 | Microsoft Office Excel 2003 Inside Out
1 10 | Microsoft Visio 2003 User's Guide
9 33 | How to do Everything with Microsoft Office Excel 2003
2 13 | Microsoft Office Excel 2003 Inside Out
2 13 | Microsoft Office Excel 2003 Inside Out
6 1244 | Excel 2003 VBA Programmer's Reference
3 3435 | Mastering Excel 2003 Programming with VBA
6 1244 | Excel 2003 VBA Programmer's Reference
5 1010 | Excel for Scientists and Engineers
10 7733 | Data Analysis Using SQL and Excel
8 9088 | Beginning Excel: What-if Data Analysis Tools
1 10 | Microsoft Visio 2003 User's Guide
7 709 | Automated Data Analysis Using Excel
10 7733 | Data Analysis Using SQL and Excel
3 3435 | Mastering Excel 2003 Programming with VBA
6 1244 | Excel 2003 VBA Programmer's Reference
1 10 | Microsoft Visio 2003 User's Guide
8 9088 | Beginning Excel: What-if Data Analysis Tools
</pre>
 
Anonymous user