REBOL [
    Title: "File keyword index"
    Purpose: {Create a specialized structure that is a list of keywords
    and, for each, a block of file or folder names related to the keyword.}
]

;; [---------------------------------------------------------------------------]
;; [ This module was created for a special purpose of making a list of         ]
;; [ keywords and, for each, a block of file names associated with the         ]
;; [ keyword.  It was part of a primitive file indexing program.               ]
;; [ A function is provided to take a file name and a block of keywords        ]
;; [ and load them into the index.  After all filenames and keywords are       ]
;; [ loaded, a function must be called to generate the index.                  ]
;; [ After the index is generated, it may be used as needed.                   ]
;; [ The final result will look like this:                                     ]
;; [ [                                                                         ]
;; [     "keyword-1" [filename-1-1 filename-1-2 ...]                           ]
;; [     "keyword-2" [filename-2-1 filename-2-2 ...]                           ]
;; [     ...                                                                   ]
;; [ ]                                                                         ]
;; [ There is no data checking in the various functions because it is          ]
;; [ assumed we are getting clean data, because it is assumed that checking    ]
;; [ was done at a higher level when the data was created.                     ]
;; [ It seems that if I have a file name with the same keyword twice in        ]
;; [ the original data, I get that file name twice in the final output.        ]
;; [ My brain hurts from control breaks so I might leave it in there.          ]
;; [---------------------------------------------------------------------------]

KIX: make object! [

    KEYWORDLIST: []      ;; keywords from one file 
    KEYS-FOLDERS: []     ;; keyword-folder pairs 
    DATABLOCK: []        ;; final data block for filling in text lists
    FOLDERBLOCK: []      ;; block of files for one keyword 
    CURRENTKEYWORD: ""   ;; for control break on keyword

    LOAD-KEYWORDS: func [
        KEYBLOCK
        FILENAME
    ] [
        foreach KEYWORD KEYBLOCK [
            append KEYS-FOLDERS KEYWORD
            append KEYS-FOLDERS FILENAME
        ]
    ]

    BUILD-INDEX: does [
        sort/skip KEYS-FOLDERS 2
        foreach [KEYWORD FOLDERNAME] KEYS-FOLDERS [
            either not-equal? KEYWORD CURRENTKEYWORD [
                either not-equal? "" CURRENTKEYWORD [
                    append DATABLOCK CURRENTKEYWORD
                    append/only DATABLOCK FOLDERBLOCK
                    CURRENTKEYWORD: copy KEYWORD
                    FOLDERBLOCK: copy []
                    append FOLDERBLOCK FOLDERNAME 
                ] [
                    CURRENTKEYWORD: copy KEYWORD
                    append FOLDERBLOCK FOLDERNAME
                ]
            ] [
                append FOLDERBLOCK FOLDERNAME
            ]
        ]
        append DATABLOCK CURRENTKEYWORD
        append/only DATABLOCK FOLDERBLOCK             
    ]
]

;;Uncomment to test
;KIX/LOAD-KEYWORDS ["kw-1" "kw-2"] %filename-1.txt 
;KIX/LOAD-KEYWORDS ["kw-3" "kw-4"] %filename-2.txt 
;KIX/LOAD-KEYWORDS ["kw-1" "kw-3"] %filename-3.txt 
;KIX/LOAD-KEYWORDS ["kw-2" "kw-6"] %filename-4.txt 
;KIX/LOAD-KEYWORDS ["kw-4" "kw-4"] %filename-5.txt ;;feature or bug? 
;KIX/LOAD-KEYWORDS ["kw-7" "kw-8"] %filename-6.txt 
;KIX/BUILD-INDEX 
;foreach [KW NAMELIST] KIX/DATABLOCK [
;    print [KW ":" mold NAMELIST]
;]
;halt