package commons.collections;
import commons.collections.interfaces.Sized;
import basics.utl.NumUtl;
//
public class Classification implements Sized
{

public String   id                  = "";
// set of weighted or counted terms classifiying this Category:
// public TermTable classifiers = new TermTable();
// how appopriate is this classification for the Container (usally a doc):
public double[] termVector          = new double[0];
private double  termVectorMagnitude = 0D;

private String unifyingAssociation = "";

public String getUnifyingAssociation ()
{
   return unifyingAssociation;
}


public void setUnifyingAssociation (String unifyingAssociation)
{
   this.unifyingAssociation = unifyingAssociation;
}

public void setTermVectorMagnitude (double termVectorMagnitude)
{
   // OsUtl.trace("Setting termVectorMagnitude " + id + ": " +
   // termVectorMagnitude);
   this.termVectorMagnitude = termVectorMagnitude;
}

public double getTermVectorMagnitude ()
{
   return this.termVectorMagnitude;
}

public int size ()
{
   return termVector.length;
}

/**
 * Calculated with an arbitrary value of 8 signs per double.
 * @return
 */
public long signs ()
{
   return termVector.length * 8;
}

public Classification()
{}

// public void add(String t) {
// classifiers.add(t, 1D);
// }
//
// public void add(String t, double w) {
// classifiers.add(t, w);
// }
//
// public boolean hasTerm(String t) {
// return classifiers.hasTerm(t);
// }
//	
// public void removeRareTerms() {
// classifiers.remove(new TermTableCalcCondition(){
// public boolean isTrue (String term, double value)
// {
// return value < 2;
// }
// });
// }
// public void writeObject(ObjectOutputStream out) throws IOException
// {
// out.writeInt(id.length());
// out.writeChars(id);
// // classifiers.writeObject(out);
// out.writeInt(termVector.length);
// for (int i = 0; i < termVector.length; i++)
// out.writeDouble(termVector[i]);
// out.writeDouble(termVectorMagnitude);
// }
//
// public void readObject(ObjectInputStream in) throws IOException,
// ClassNotFoundException
// {
// id = ExtendedSerializer.readString(in, in.readInt());
// // classifiers.readObject(in);
// int hvlen = in.readInt();
// termVector = new double[hvlen];
// for(int i = 0; i < hvlen; i++) {
// termVector[i] = in.readDouble();
// }
// termVectorMagnitude = in.readDouble();
// }
//
// public void writeObject(LineWriter out)
// {
// out.write(id);
// // classifiers.writeObject(out);
// out.write("----------vector------------");
// out.write("Vector: " + termVector.length);
// for (int i = 0; i < termVector.length; i++)
// out.write(termVector[i]+"");
// out.write("VSize:" + termVectorMagnitude);
// }
//
// public void readObject(LineReader reader)
// {
// id = reader.nextLine();
// // classifiers.readObject(reader);
// reader.nextLine();
// int hvlen = StrUtl.toInt(StrUtl.split(reader.nextLine(), ":")[1], 0);
// termVector = new double[hvlen];
// for(int i = 0; i < hvlen; i++) {
// termVector[i] = StrUtl.toDouble(reader.nextLine(), 0D);
// }
// termVectorMagnitude = StrUtl.toDouble(StrUtl.split(reader.nextLine(),
// ":")[1], 0);
// }
public String toString ()
{
   StringBuilder s = new StringBuilder();
   s.append(this.getClass().getSimpleName());
   s.append(":");
   s.append(id);
   s.append(":");
   s.append(this.unifyingAssociation);
   s.append("{M:");
   s.append(NumUtl.round(termVectorMagnitude, 1));
   
   s.append(", L:");
   s.append(termVector.length);
   s.append(" [ ");
   for (int i = 0; i < termVector.length; i++)
   {
      if (termVector[i] > 0)
      {
         s.append(i);
         s.append(":");
         s.append(NumUtl.round(termVector[i], 1));
         s.append(" ");
      }
   }
   s.append("]");
   return s.toString();
}

public String vectorToString ()
{
   StringBuilder s = new StringBuilder();
   s.append("C:" + id);
   s.append(" [ ");
   for (int i = 0; i < termVector.length; i++)
   {
      if (termVector[i] > 0) s.append(i + "=" + NumUtl.round(termVector[i], 1) + " ");
   }
   s.append("] = ");
   s.append(NumUtl.round(termVectorMagnitude, 1));
   return s.toString();
}

public String getId ()
{
   return id;
}

public void setId (String id)
{
   this.id = id;
}

public double[] getTermVector ()
{
   return termVector;
}

public void setTermVector (double[] termVector)
{
   this.termVector = termVector;
}
}
