package xWord;
use strict;
use warnings;
use xKey;
use xFeatures;

#TODO: modify the whole internals to use a hash of freatuers as opposed
# o the current "static" features
#
# so, $self = {...
#       __features => ()
#       }
#       and each entry in %{$self->{__features}} is a feature vector

sub new {
   my $self = {
      __keys => [],      #vector of the keys
      __word => [],      #vector of the characters
      __window => undef, #first window name
      __features => xFeatures->new
   };
   bless $self, 'xWord';
   return $self;
}

sub list_features {
   my ($self)=@_;
   return $self->{__features}->list_features();
}

sub get_features_vec {
   my ($self)=@_;
   my @vec;
   my %features=$self->{__features}->get_features($self->{__keys},
                                                      length($self->word));

   foreach my $f (sort(keys %features)) { push(@vec,"$f=$features{$f}"); }

   return @vec;
}

sub win {
   my ($self,$win)=@_;
   $self->{__window}=$win;
}

sub add_key {
   my ($self,$key,$ch) =@_;
   push(@{$self->{__word}},$ch || $key->key);
   push(@{$self->{__keys}},$key);
   if(!(defined $self->{__window})) {
      $self->{__window}=$key->win;
   }
}

sub word {
   my ($self) = @_;
   return join('',@{$self->{__word}});
}

sub vec {
   my ($self) = @_;
   my @tmp=();
   push(@tmp,($self->{__end} - $self->{__start}));
   push(@tmp,@{$self->{__RP}}); 
   push(@tmp,@{$self->{__PP}}); 
   push(@tmp,@{$self->{__RR}}); 
   push(@tmp,@{$self->{__PR}}); 
   return @tmp;
}

sub get_features {
   my ($self) = @_;
   my ($vec,$tmp);

   $vec="word=".$self->word."|";
   $vec.="window=".$self->{__window}."|";
   $vec.=join("|",$self->get_features_vec);

   return $vec;
}






1;


