/* This is -*- C -*- */
/* vim: set sw=2: */
/* $Id$ */

/*
 * pos.c
 *
 * Copyright (C) 2003 The Free Software Foundation, Inc.
 *
 * Developed by Jon Trowbridge <trow@gnu.org>
 */

/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "pos.h"

PartOfSpeech
pos_from_char (char c)
{
  switch (c) {

  case 'N': return POS_NOUN;
  case 'p': return POS_PLURAL;
  case 'h': return POS_NOUN_PHRASE;
  case 'V': return POS_VERB_PRINCIPLE;
  case 't': return POS_VERB_TRANSITIVE;
  case 'i': return POS_VERB_INTRANSITIVE;
  case 'A': return POS_ADJECTIVE;
  case 'v': return POS_ADVERB;
  case 'C': return POS_CONJUNCTION;
  case 'P': return POS_PREPOSITION;
  case '!': return POS_INTERJECTION;
  case 'r': return POS_PRONOUN;
  case 'D': return POS_ARTICLE_DEFINITE;
  case 'I': return POS_ARTICLE_INDEFINITE;
  case 'o': return POS_NOMINATIVE;

  default:
    return POS_UNKNOWN;
  }
}

char
pos_to_char (PartOfSpeech pos)
{
  switch (pos) {

  case POS_NOUN:               return 'N';
  case POS_PLURAL:             return 'p';
  case POS_NOUN_PHRASE:        return 'h';
  case POS_VERB_PRINCIPLE:     return 'V';
  case POS_VERB_TRANSITIVE:    return 't';
  case POS_VERB_INTRANSITIVE:  return 'i';
  case POS_ADJECTIVE:          return 'A';
  case POS_ADVERB:             return 'v';
  case POS_CONJUNCTION:        return 'C';
  case POS_PREPOSITION:        return 'P';
  case POS_INTERJECTION:       return '!';
  case POS_PRONOUN:            return 'r';
  case POS_ARTICLE_DEFINITE:   return 'D';
  case POS_ARTICLE_INDEFINITE: return 'I';
  case POS_NOMINATIVE:         return 'o';

  default:
    return '?';
  }
}

const char *
pos_to_string (PartOfSpeech pos)
{
  switch (pos) {

  case POS_NOUN:               return "Noun";
  case POS_PLURAL:             return "Plural";
  case POS_NOUN_PHRASE:        return "NounPhrase";
  case POS_VERB_PRINCIPLE:     return "Verb";
  case POS_VERB_TRANSITIVE:    return "VerbTransitive";
  case POS_VERB_INTRANSITIVE:  return "VerbIntransitive";
  case POS_ADJECTIVE:          return "Adjective";
  case POS_ADVERB:             return "Adverb";
  case POS_CONJUNCTION:        return "Conjunction";
  case POS_PREPOSITION:        return "Preposition";
  case POS_INTERJECTION:       return "Interjection";
  case POS_PRONOUN:            return "Pronoun";
  case POS_ARTICLE_DEFINITE:   return "ArticleDefinite";
  case POS_ARTICLE_INDEFINITE: return "ArticleIndefinite";
  case POS_NOMINATIVE:         return "Nominative";

  default:
    return "?";
  }
}

char *
pos_mask_to_string (PartOfSpeechMask pos_mask)
{
  PartOfSpeechMask x;
  int i, j, n;
  char *str;

  if (pos_mask == 0)
    return g_strdup ("?");

  /* pass one: count the bits */
  for (x = pos_mask, n = 0; x; x = x >> 1) {
    if (x & 1)
      ++n;
  }

  str  = g_new0 (char, n+1);
  
  for (i = 1, j = 0; i < POS_LAST; ++i) {
    if (pos_mask & pos_get_mask (i)) {
      str[j] = pos_to_char(i);
      ++j;
    }
  }

  return str;
}

/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */

PyObject *
py_pos_mask_to_string (PyObject *self, PyObject *args)
{
  PartOfSpeechMask pos_mask;
  char *str;
  PyObject *retval;

  if (! PyArg_ParseTuple (args, "i", &pos_mask))
    return NULL;

  str = pos_mask_to_string (pos_mask);
  retval = Py_BuildValue ("s", str);
  g_free (str);
  
  return retval;
}
