Sunday, September 26, 2004

 

How to find keyword combinations

This batch file (combin.bat) is was written on a Windows XP system, and might need modification for older or newer systems. It takes one parameter--the number of keywords (or groups of keywords). The output is a 2nd batch file which will be used to write the output to a file titled 'List'.
Combin.bat:
@echo off
Rem Find out if we're dealing with sets of keywords
echo \%1>combin.tmp
find /i "set" combin.tmp>nul
if not errorlevel==1 %*
Rem In XP %* refers to all parameters given a batch file
if %1.==%0. goto printit
Rem If the 1st parameter is the same as the batch file, we're printing the results
Rem If we give no parameter or we don't give a valid number, select 8 'keywords'
if %1.==. %0 8
if %2.==. echo \1\2\3\4\5\6\7\8\>combin.tmp
find "%1" combin.tmp>nul
if %2.==. if errorlevel==1 %0 8
if %2.==. echo \ >combin.tmp
find "%1" combin.tmp>nul
if %2.==. if not errorlevel==1 %0 8
Rem Ensure that we don't duplicate groups of keywords
if %1==7 if not %2.==. echo %2 >combin.tmp
find "!!!!!!!!" combin.tmp>nul
if %1==7 if not %2.==. if not errorlevel==1 goto end
if %1==6 if not %2.==. echo %2 >combin.tmp
find "!!!!!!!" combin.tmp >nul
if %1==6 if not %2.==. if not errorlevel==1 goto end
if %1==5 if not %2.==. echo %2 >combin.tmp
find "!!!!!!" combin.tmp >nul
if %1==5 if not %2.==. if not errorlevel==1 goto end
if %1==4 if not %2.==. echo %2 >combin.tmp
find "!!!!!" combin.tmp >nul
if %1==4 if not %2.==. if not errorlevel==1 goto end
if %1==3 if not %2.==. echo %2 >combin.tmp
find "!!!!" combin.tmp>nul
if %1==3 if not %2.==. if not errorlevel==1 goto end
if %1==2 if not %2.==. echo %2 >combin.tmp
find "!!!" combin.tmp >nul
if %1==2 if not %2.==. if not errorlevel==1 goto end
if %1==1 if not %2.==. echo %2 >combin.tmp
find "!!" combin.tmp >nul
if %1==1 if not %2.==. if not errorlevel==1 goto end
if %2.==.. goto end
if %3.==.. goto end
if %4.==.. goto end
if %5.==.. goto end
if %6.==.. goto end
if %7.==.. goto end
if %8.==.. goto end
if %9.==.. goto end
if not %1.==. if %1.==%2. goto end
if not %1.==. if %1.==%3. goto end
if not %1.==. if %1.==%4. goto end
if not %1.==. if %1.==%5. goto end
if not %1.==. if %1.==%6. goto end
if not %1.==. if %1.==%7. goto end
if not %1.==. if %1.==%8. goto end
if not %1.==. if %1.==%9. goto end
if not %2.==. if %2.==%3. goto end
if not %2.==. if %2.==%4. goto end
if not %2.==. if %2.==%5. goto end
if not %2.==. if %2.==%6. goto end
if not %2.==. if %2.==%7. goto end
if not %2.==. if %2.==%8. goto end
if not %2.==. if %2.==%9. goto end
if not %3.==. if %3.==%4. goto end
if not %3.==. if %3.==%5. goto end
if not %3.==. if %3.==%6. goto end
if not %3.==. if %3.==%7. goto end
if not %3.==. if %3.==%8. goto end
if not %3.==. if %3.==%9. goto end
if not %4.==. if %4.==%5. goto end
if not %4.==. if %4.==%6. goto end
if not %4.==. if %4.==%7. goto end
if not %4.==. if %4.==%8. goto end
if not %4.==. if %4.==%9. goto end
if not %5.==. if %5.==%6. goto end
if not %5.==. if %5.==%7. goto end
if not %5.==. if %5.==%8. goto end
if not %5.==. if %5.==%9. goto end
if not %6.==. if %6.==%7. goto end
if not %6.==. if %6.==%8. goto end
if not %6.==. if %6.==%9. goto end
if not %7.==. if %7.==%8. goto end
if not %7.==. if %7.==%9. goto end
if not %8.==. if %8.==%9. goto end
if not %2.==. echo %2 >combin.tmp
find "%1" combin.tmp>nul
if not %2.==. if not errorlevel==1 goto end
if not %3.==. echo %3 >combin.tmp
find "%2" combin.tmp>nul
if not %3.==. if not errorlevel==1 goto end
if not %4.==. echo %4 >combin.tmp
find "%3" combin.tmp>nul
if not %4.==. if not errorlevel==1 goto end
if not %5.==. echo %5 >combin.tmp
find "%4" combin.tmp>nul
if not %5.==. if not errorlevel==1 goto end
if not %6.==. echo %6 >combin.tmp
find "%5" combin.tmp>nul
if not %6.==. if not errorlevel==1 goto end
if not %7.==. echo %7 >combin.tmp
find "%6" combin.tmp>nul
if not %7.==. if not errorlevel==1 goto end
if not %8.==. echo %8 >combin.tmp
find "%7" combin.tmp>nul
if not %8.==. if not errorlevel==1 goto end
if not %9.==. echo %9 >combin.tmp
find "%8" combin.tmp>nul
if not %9.==. if not errorlevel==1 goto end
Rem build list of keyword combinations
if not %2.==. echo call %0 %0 %%* %2 %3 %4 %5 %6 %7 %8 %9 >>list.bat
if %2.==. for %%o in (!!!!!!!! !!!!!!! !!!!!! !!!!! !!!! !!! !! !) do call %0 %1 %%o
if not %2.==. if %3.==. for %%o in (. !!!!!!! !!!!!! !!!!! !!!! !!! !! !) do call %0 %1 %2 %%o
if not %3.==. if %4.==. for %%o in (. . !!!!!! !!!!! !!!! !!! !! !) do call %0 %1 %2 %3 %%o
if not %4.==. if %5.==. for %%o in (. . . !!!!! !!!! !!! !! !) do call %0 %1 %2 %3 %4 %%o
if not %5.==. if %6.==. for %%o in (. . . . !!!! !!! !! !) do call %0 %1 %2 %3 %4 %5 %%o
if not %6.==. if %7.==. for %%o in (. . . . . !!! !! !) do call %0 %1 %2 %3 %4 %5 %6 %%o
if not %7.==. if %8.==. for %%o in (. . . . . . !! !) do call %0 %1 %2 %3 %4 %5 %6 %7 %%o
if not %8.==. if %9.==. for %%o in (. . . . . . . !) do call %0 %1 %2 %3 %4 %5 %6 %7 %8 %%o
goto end
:printit
shift
shift
echo \%0 >combin.tmp
find /i "\set" combin.tmp>nul
if not errorlevel==1 %*
echo %*
echo %*>>list
del combin.tmp
:end

Comments:
Due to the time to select valid combinations, selecting more than 4 or 5 keywords can take the batch file some time to run. The solution is to select GROUPS of keywords. Thus, for 6 keywords, 2 groups of three will work.
1st we select for 3 keywords.
Our output is LIST.BAT, which looks like this:
call combin combin %* !!!
call combin combin %* !!! !!
call combin combin %* !!! !! !
call combin combin %* !!! !
call combin combin %* !!
call combin combin %* !! !
call combin combin %* !

We rename list.bat as SET1.BAT, and copy SET1.BAT to SET2.BAT. We now have 2 sets of !s. Let's now change them to represent the actual keywords, thusly:
In SET1.BAT, we change !!! to KW1 (for KeyWord1), !! to KW2, ! to KW3; and in SET2.BAT we change !!! to KW4, !! to KW5, and ! to KW6:
SET1.BAT
call combin combin %* KW1
call combin combin %* KW1 KW2
call combin combin %* KW1 KW2 KW3
call combin combin %* KW1 KW3
call combin combin %* KW2
call combin combin %* KW2 KW3
call combin combin %* KW3
----
SET2.BAT
call combin combin %* KW4
call combin combin %* KW4 KW5
call combin combin %* KW4 KW5 KW6
call combin combin %* KW4 KW6
call combin combin %* KW5
call combin combin %* KW5 KW6
call combin combin %* KW6

Now lets build the list which refers to these two sets of KeyWords. We re-run COMBIN.BAT selecting 2 (sets of keywords). List.bat comes out like this:
call combin combin %* !!
call combin combin %* !! !
call combin combin %* !

We change !! to Set1 and ! to Set2, thus:
call combin combin %* Set1
call combin combin %* Set1 Set2
call combin combin %* Set2

We then run LIST.BAT, creating LIST, which looks like:
combin KW1
combin KW1 KW2
combin KW1 KW2 KW3
combin KW1 KW3
combin KW2
combin KW2 KW3
combin KW3
combin KW1 KW4
combin KW1 KW4 KW5
combin KW1 KW4 KW5 KW6
combin KW1 KW4 KW6
combin KW1 KW5
combin KW1 KW5 KW6
combin KW1 KW6
combin KW1 KW2 KW4
combin KW1 KW2 KW4 KW5
combin KW1 KW2 KW4 KW5 KW6
combin KW1 KW2 KW4 KW6
combin KW1 KW2 KW5
combin KW1 KW2 KW5 KW6
combin KW1 KW2 KW6
combin KW1 KW2 KW3 KW4
combin KW1 KW2 KW3 KW4 KW5
combin KW1 KW2 KW3 KW4 KW5 KW6
combin KW1 KW2 KW3 KW4 KW6
combin KW1 KW2 KW3 KW5
combin KW1 KW2 KW3 KW5 KW6
combin KW1 KW2 KW3 KW6
combin KW1 KW3 KW4
combin KW1 KW3 KW4 KW5
combin KW1 KW3 KW4 KW5 KW6
combin KW1 KW3 KW4 KW6
combin KW1 KW3 KW5
combin KW1 KW3 KW5 KW6
combin KW1 KW3 KW6
combin KW2 KW4
combin KW2 KW4 KW5
combin KW2 KW4 KW5 KW6
combin KW2 KW4 KW6
combin KW2 KW5
combin KW2 KW5 KW6
combin KW2 KW6
combin KW2 KW3 KW4
combin KW2 KW3 KW4 KW5
combin KW2 KW3 KW4 KW5 KW6
combin KW2 KW3 KW4 KW6
combin KW2 KW3 KW5
combin KW2 KW3 KW5 KW6
combin KW2 KW3 KW6
combin KW3 KW4
combin KW3 KW4 KW5
combin KW3 KW4 KW5 KW6
combin KW3 KW4 KW6
combin KW3 KW5
combin KW3 KW5 KW6
combin KW3 KW6
combin KW4
combin KW4 KW5
combin KW4 KW5 KW6
combin KW4 KW6
combin KW5
combin KW5 KW6
combin KW6

Replace 'combin ' with a space, and then replace each KW with the appropriate keyword, e.g. replace KW1 with Easy, KW2 with Useful, KW3 with rapid... This gives a list of all combinations of our chosen keywords.
 
For 11 keywords, I chose 2 sets of 4 keywords and 1 set of 3 keywords, for a total of 3 sets.
For 13 keywords, I chose 3 sets of 4 keywords and 1 set of 1 keyword.

Be Well,
Bob Griffin
 
hey bob, have you ever tried perl? it makes such tasks much easier..
 
No, I haven't tried Perl yet.
This particular batch file was designed for a friend who lives about 450 miles from me, and was posted here so that he could easily access it.

I'd like to look into Perl some time. Thanks for the recommendation.
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?