Maybe there are some more people that like to see bits. Feel free to do 
whatever you like with it. 

Idea is simple, map 8 bits from HitCollector to one pixel by changing gray 
levels. Implementation is Quick 'n Dirty, but does the job.

/**
 * Copyright 2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.imageio.ImageIO;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.HitCollector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.BitSet;

/*
 * Generates .png image of the search result. Maps bits from result  
 * sequentially from left to right untill specified image lenth is achieved 
 * and then continues from the begining in the next line.
 * 
 * Mapps 8 bits to one pixel, by changing leves of gray 
 * e.g 
 * WHITE PIXEL: 0 of eight bits set
 * GRAY PIXEL: some bits set (more set => darker) 
 * BLACK PIXEL: all 8 bits set  
 * 
 *  main has 3 params: index path, Query, and optional imageWidth 
 */
public class VisualizeBitVector {

    // Invoke the init method in BufImagePanel to build the image.
    public static void main(String[] args) throws IOException, ParseException {
        Img fImg;
        Directory dir;
        IndexReader reader;

        fImg = new Img();
        if (args.length < 2) {
            System.out.println("VisualizeBitVector index_path query 
[imageWidth]");
            System.exit(-1);
        }

        String path = args[0];
        String query = args[1];
        
        int imageWidth = 800;
        if(args.length > 2){
            imageWidth = Integer.parseInt(args[2]);
        }

        System.getProperties().setProperty(
                "org.apache.lucene.FSDirectory.class",
                "org.apache.lucene.store.MMapDirectory");
        dir = FSDirectory.getDirectory(path, false);
        reader = IndexReader.open(dir);

        BufferedImage img;

        img = fImg.makeImage(reader, query, imageWidth);
        File file = new File("queryBits.png");
        ImageIO.write(img, "png", file);

    }

}


class Img {
    BitSet bs;

    BufferedImage fBufferedImage = null;

    int fWidth = 0, fHeight = 0;

    byte[] fPixels = null;

    /** Build a BufferedImage from a pixel array. * 
     * @throws ParseException */

    public BufferedImage makeImage(IndexReader reader, String query, int 
imageWidth)
            throws IOException, ParseException {

        int maxDocs = reader.maxDoc();

        fWidth = imageWidth;
        fHeight = (maxDocs / 8 / fWidth) + 1;
        fPixels = new byte[fWidth * fHeight];

        //reset 
        Arrays.fill(fPixels, (byte) (0 | 0xFF));

        Searcher s = new IndexSearcher(reader);
        bs = new BitSet(maxDocs);

        HitCollector hc = new HitCollector() {
            public void collect(int doc, float score) {
                bs.set(doc);
            }
        };

        Analyzer analyzer = new StandardAnalyzer();
        QueryParser parser = new QueryParser("", analyzer);
        Query q = parser.parse(query);

        s.search(q, null, hc);

        //This is the only interesting thing here
        for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
            int pos = i >> 3;
            fPixels[pos] = (byte) ((fPixels[pos] & 0xFF) >>> 1);
        }


        s.close();
        reader.close();

        // Create a BufferedIamge of the gray values in bytes.
        fBufferedImage = new BufferedImage(fWidth, fHeight,
                BufferedImage.TYPE_BYTE_GRAY);

        // Get the writable raster so that data can be changed.
        WritableRaster wr = fBufferedImage.getRaster();

        // Now write the byte data to the raster
        wr.setDataElements(0, 0, fWidth, fHeight, fPixels);
        return fBufferedImage;
    }

}





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to