package yi.iu.imgdb.learn;

import yi.util.io.*;
import java.util.*;

/**
 * This class is just to convert the data set provided by the "machine
 * translation" paper to the format we can use
 *
 * P. Duygulu, K. Barnard, N. de Freitas, and D. Forsyth. Object
 * recognition as machine translation: Learning a lexicon for a
 * fixed image vocabulary. In ECCV’02, pages 97-112, 2002.
 *
 * We thank Kobus Barnard for making the Corel dataset available at
 * http://www.cs.arizona.edu/people/kobus/research/data/eccv 2002
 */

public class MachineTranslation {

  public final static String PATH = "H:/images/eccv_2002/";

  /**
   * Convert "document_words" and "test_1_document_words" to desc
   */
  public static class GenerateDescription {
    public static void main(String[] args) {
      try {
        double[][] document_words = yi.util.io.ReadArgu.loadMatlab(PATH +
            "document_words");
        int file_index=1;
        for (int i=0; i<document_words.length; i++, file_index++) {
          System.out.print(file_index);
          System.out.print(":: ");
          for (int j=0; j<document_words[i].length; j++) {
            if (document_words[i][j] <= 0) break;
            if (j>0) System.out.print(", ");
            System.out.print((int)document_words[i][j]);
          }
          System.out.println();
        }

        document_words = yi.util.io.ReadArgu.loadMatlab(PATH +
            "test_1_document_words");
        for (int i = 0; i < document_words.length; i++, file_index++) {
          System.out.print(file_index);
          System.out.print(":: ");
          for (int j = 0; j < document_words[i].length; j++) {
            if (document_words[i][j] <= 0)
              break;
            if (j > 0)
              System.out.print(", ");
            System.out.print( (int) document_words[i][j]);
          }
          System.out.println();
        }

      }catch (Exception e) {
        e.printStackTrace();
      }

    }
  }

  /**
   * Convert "blobs" and "test_1_blobs" to image data center
   */
  public static class GenerateImgDataCenter {
    public static void main(String[] args) {
      try {
        int file_index = 0;
        FeatureRepresentationList[] ftl_list = new FeatureRepresentationList[5000];

        double[][] blobs = yi.util.io.ReadArgu.loadMatlab(PATH +
            "blobs");

        double[][] document_blobs_double = yi.util.io.ReadArgu.loadMatlab(PATH +
            "document_blobs");
        int[][] document_blobs = yi.util.data.Conversion.toInt(document_blobs_double);

        //     test_1_document_blobs
        // the 15-17 are lab average
        for (int i=0; i<document_blobs.length; i++, file_index++) {
          System.out.println("file " + (file_index+1));
          FeatureRepresentationList ftl = new FeatureRepresentationList(12);
          for (int j = 0; j < document_blobs[i].length; j++) {
            if (document_blobs[i][j] <= 0) {
              break;
            }
            double[] data = blobs[document_blobs[i][j] - 1];
//            System.out.println("" + data[0] + ", " + data[15]);
//            data = new double[] {
//                data[15], data[16], data[17]
//            };
            data = new double[] {
                data[24], data[25], data[26],
                data[27], data[28], data[29],
                data[30], data[31], data[32],
                data[33], data[34], data[35],
            };
            ftl.addFeatureRepresentation(new FeatureRepresentation(data));
          }
          ftl_list[file_index] = ftl;
        }


        blobs = yi.util.io.ReadArgu.loadMatlab(PATH +
            "test_1_blobs");

        document_blobs_double = yi.util.io.ReadArgu.loadMatlab(PATH +
            "test_1_document_blobs");
        document_blobs = yi.util.data.Conversion.toInt(document_blobs_double);

        // the 15-17 are lab average
        for (int i=0; i<document_blobs.length; i++, file_index++) {
          System.out.println("file " + (file_index+1));
          FeatureRepresentationList ftl = new FeatureRepresentationList(12);
          for (int j = 0; j < document_blobs[i].length; j++) {
            if (document_blobs[i][j] <= 0) {
              break;
            }
            double[] data = blobs[document_blobs[i][j] - 1];
//            System.out.println("" + data[0] + ", " + data[15]);
//            data = new double[] {
//                data[15], data[16], data[17]
//            };
            data = new double[] {
                data[24], data[25], data[26],
                data[27], data[28], data[29],
                data[30], data[31], data[32],
                data[33], data[34], data[35],
            };
            ftl.addFeatureRepresentation(new FeatureRepresentation(data));
          }
          ftl_list[file_index] = ftl;
        }

        LinkedList img_list = new LinkedList();
        for (int i=1; i<=5000; i++) {
          img_list.add(new String(Integer.toString(i)));
        }

        String name = "mt.50";
        ImgDataCenter idc = new ImgDataCenter(img_list, null, name + ".desc", "eng", ftl_list);
        idc.writeToFile(name + ".eng.idc");

      }catch (Exception e) {
        e.printStackTrace();
      }

    }
  }

}