Exemplo n.º 1
0
void
do_cqi_cqp_list_subcorpora(void)
{
  char *corpus;
  CorpusList *cl, *mother;
  int n = 0;
  
  corpus = cqi_read_string();
  if (server_debug)
   Rprintf( "CQi: CQI_CQP_LIST_SUBCORPORA(%s)\n", corpus);
  mother = cqi_find_corpus(corpus);
  if (!check_corpus_name(corpus) || mother == NULL) 
    cqi_command(cqi_errno);
  else {

    /* ugly, but it's easiest ... first count corpora, then return names one by one */
    for (cl = FirstCorpusFromList(); cl != NULL; cl = NextCorpusFromList(cl)) {
      if (cl->type == SUB && cl->corpus == mother->corpus)
        n++;
    }
    cqi_send_word(CQI_DATA_STRING_LIST);
    cqi_send_int(n);
    for (cl = FirstCorpusFromList(); cl != NULL; cl = NextCorpusFromList(cl)) {
      if (cl->type == SUB && cl->corpus == mother->corpus)
        cqi_send_string(cl->name);
    }
    cqi_flush();

  }
  free(corpus);
}
Exemplo n.º 2
0
void
do_cqi_cl_struc2str(void)
{
  int *struclist;
  int len, i;
  char *a, *str;
  Attribute *attribute;

  a = cqi_read_string();
  len = cqi_read_int_list(&struclist);
  if (server_debug) {
   Rprintf( "CQi: CQI_CL_STRUC2STR('%s', [", a);
    for (i=0; i<len; i++)
     Rprintf( "%d ", struclist[i]);
   Rprintf( "])\n");
  }
  attribute = cqi_lookup_attribute(a, ATT_STRUC);
  if (attribute == NULL) {
    cqi_command(cqi_errno);
  }
  else {
    /* we assemble the CQI_DATA_STRING_LIST() return command by hand,
       so we don't have to allocate a temporary list */
    cqi_send_word(CQI_DATA_STRING_LIST);
    cqi_send_int(len);          /* list size */
    for (i=0; i<len; i++) {
      str = cl_struc2str(attribute, struclist[i]);
      cqi_send_string(str);     /* sends "" if str == NULL (wrong alignment number) */
    }
  }
  cqi_flush();
  if (struclist != NULL)
    free(struclist);                    /* don't forget to free allocated memory */
  free(a);
}
Exemplo n.º 3
0
void
do_cqi_cl_idlist2cpos(void)
{
  int *idlist, *cposlist;
  int i, len, cposlen;
  char *a;
  Attribute *attribute;

  a = cqi_read_string();
  len = cqi_read_int_list(&idlist);
  if (server_debug) {
   Rprintf( "CQi: CQI_CL_IDLIST2CPOS('%s', [", a);
    for (i=0; i<len; i++)
     Rprintf( "%d ", idlist[i]);
   Rprintf( "])\n");
  }
  attribute = cqi_lookup_attribute(a, ATT_POS);
  if (attribute == NULL) {
    cqi_command(cqi_errno);
  }
  else {
    cposlist = cl_idlist2cpos(attribute, idlist, len, 1, &cposlen);
    if (cposlist == NULL) 
      send_cl_error();
    else {
      cqi_data_int_list(cposlist, cposlen);
      free(cposlist);
    }
  }
  cqi_flush();
  if (idlist != NULL)
    free(idlist);               /* don't forget to free allocated memory */
  free(a);
}
Exemplo n.º 4
0
void
do_cqi_cl_cpos2str(void)
{
  int *cposlist;
  int len, i;
  char *a, *str;
  Attribute *attribute;

  a = cqi_read_string();
  len = cqi_read_int_list(&cposlist);
  if (server_debug) {
    fprintf(stderr, "CQi: CQI_CL_CPOS2STR('%s', [", a);
    for (i=0; i<len; i++)
      fprintf(stderr, "%d ", cposlist[i]);
    fprintf(stderr, "])\n");
  }
  attribute = cqi_lookup_attribute(a, ATT_POS);
  if (attribute == NULL) {
    cqi_command(cqi_errno);
  }
  else {
    /* we assemble the CQI_DATA_STRING_LIST() return command by hand,
       so we don't have to allocate a temporary list */
    cqi_send_word(CQI_DATA_STRING_LIST);
    cqi_send_int(len);          /* list size */
    for (i=0; i<len; i++) {
      str = cl_cpos2str(attribute, cposlist[i]);
      cqi_send_string(str);     /* sends "" if str == NULL (cpos out of range) */
    }
  }
  cqi_flush();
  if (cposlist != NULL)
    free(cposlist);             /* don't forget to free allocated memory */
  free(a);
}
Exemplo n.º 5
0
void
do_cqi_cqp_subcorpus_has_field(void)
{
  char *subcorpus;
  CorpusList *cl;
  cqi_byte field;
  char *fieldname;
  int field_ok = 1;             /* field valid? */

  subcorpus = cqi_read_string();
  field = cqi_read_byte();

  fieldname = cqi_field_name(field);
  if (fieldname == NULL) {
    fieldname = "<invalid field>";
    field_ok = 0;
  }
  if (server_debug) 
   Rprintf( "CQi: CQI_CQP_SUBCORPUS_HAS_FIELD('%s', %s)\n", 
            subcorpus, fieldname);

  cl = cqi_find_corpus(subcorpus);
  if (cl == NULL) 
    cqi_command(cqi_errno);
  else if (!field_ok)
    cqi_command(CQI_CQP_ERROR_INVALID_FIELD);
  else {
    switch (field) {
    case CQI_CONST_FIELD_MATCH:
      cqi_data_bool(CQI_CONST_YES);
      break;
    case CQI_CONST_FIELD_MATCHEND:
      cqi_data_bool(CQI_CONST_YES);
      break;
    case CQI_CONST_FIELD_TARGET:
      if (cl->targets == NULL) 
        cqi_data_bool(CQI_CONST_NO);
      else 
        cqi_data_bool(CQI_CONST_YES);
      break;
    case CQI_CONST_FIELD_KEYWORD:
      if (cl->keywords == NULL) 
        cqi_data_bool(CQI_CONST_NO);
      else 
        cqi_data_bool(CQI_CONST_YES);
      break;
    default:
      cqiserver_internal_error("do_cqi_cqp_subcorpus_has_field", "Can't identify requested field.");
    }
    cqi_flush();
  }

  free(subcorpus);
}
Exemplo n.º 6
0
/* this part sends attributes of a certain type as a STRING[] to the client */
void
send_cqi_corpus_attributes(Corpus *c, int type)
{
  Attribute *a;
  int len;
  
  cqi_send_word(CQI_DATA_STRING_LIST);
  len = 0;
  for (a = first_corpus_attribute(c); a != NULL; a = next_corpus_attribute())
    if (a->type == type)
      len++;
  cqi_send_int(len);

  for (a = first_corpus_attribute(c); a != NULL; a = next_corpus_attribute())
    if (a->type == type)
      cqi_send_string(a->any.name);
  cqi_flush();
}
Exemplo n.º 7
0
void
do_cqi_cl_cpos2rbound(void)
{
  int *cposlist;
  int len, i, struc, lb, rb;
  char *a;
  Attribute *attribute;

  a = cqi_read_string();
  len = cqi_read_int_list(&cposlist);
  if (server_debug) {
   Rprintf( "CQi: CQI_CL_CPOS2RBOUND('%s', [", a);
    for (i=0; i<len; i++)
     Rprintf( "%d ", cposlist[i]);
   Rprintf( "])\n");
  }
  attribute = cqi_lookup_attribute(a, ATT_STRUC);
  if (attribute == NULL) {
    cqi_command(cqi_errno);
  }
  else {
    /* we assemble the CQI_DATA_INT_LIST() return command by hand,
       so we don't have to allocate a temporary list */
    cqi_send_word(CQI_DATA_INT_LIST);
    cqi_send_int(len);          /* list size */
    for (i=0; i<len; i++) {
      struc = cl_cpos2struc(attribute, cposlist[i]);
      if (struc < 0) {
        cqi_send_int(-1);                       /* return -1 if cpos is not in region */
      }
      else {
        if (cl_struc2cpos(attribute, struc, &lb, &rb))
          cqi_send_int(rb);
        else
          cqi_send_int(-1);     /* cannot return error within list, so send -1 */
      }
    }
  }
  cqi_flush();
  cl_free(cposlist);                    /* don't forget to free allocated memory */
  free(a);
}
Exemplo n.º 8
0
void
do_cqi_corpus_list_corpora(void)
{
  CorpusList *cl;
  int n = 0;
  
  if (server_debug)
   Rprintf( "CQi: CQI_CORPUS_LIST_CORPORA()\n");
  /* ugly, but it's easiest ... first count corpora, then return names one by one */
  for (cl = FirstCorpusFromList(); cl != NULL; cl = NextCorpusFromList(cl)) {
    if (cl->type == SYSTEM)
      n++;
  }
  cqi_send_word(CQI_DATA_STRING_LIST);
  cqi_send_int(n);
  for (cl = FirstCorpusFromList(); cl != NULL; cl = NextCorpusFromList(cl)) {
    if (cl->type == SYSTEM)
      cqi_send_string(cl->name);
  }
  cqi_flush();
}
Exemplo n.º 9
0
void
do_cqi_cl_cpos2alg(void)
{
  int *cposlist;
  int len, i, alg;
  char *a;
  Attribute *attribute;

  a = cqi_read_string();
  len = cqi_read_int_list(&cposlist);
  if (server_debug) {
   Rprintf( "CQi: CQI_CL_CPOS2ALG('%s', [", a);
    for (i=0; i<len; i++)
     Rprintf( "%d ", cposlist[i]);
   Rprintf( "])\n");
  }
  attribute = cqi_lookup_attribute(a, ATT_ALIGN);
  if (attribute == NULL) {
    cqi_command(cqi_errno);
  }
  else {
    /* we assemble the CQI_DATA_INT_LIST() return command by hand,
       so we don't have to allocate a temporary list */
    cqi_send_word(CQI_DATA_INT_LIST);
    cqi_send_int(len);          /* list size */
    for (i=0; i<len; i++) {
      alg = cl_cpos2alg(attribute, cposlist[i]);
      if (alg < 0) 
        alg = -1;                       /* return -1 if cpos is out of range */
      cqi_send_int(alg);
    }
  }
  cqi_flush();
  if (cposlist != NULL)
    free(cposlist);                     /* don't forget to free allocated memory */
  free(a);
}
Exemplo n.º 10
0
void
do_cqi_cl_id2freq(void)
{
  int *idlist;
  int len, i, f;
  char *a;
  Attribute *attribute;

  a = cqi_read_string();
  len = cqi_read_int_list(&idlist);
  if (server_debug) {
   Rprintf( "CQi: CQI_CL_ID2FREQ('%s', [", a);
    for (i=0; i<len; i++)
     Rprintf( "%d ", idlist[i]);
   Rprintf( "])\n");
  }
  attribute = cqi_lookup_attribute(a, ATT_POS);
  if (attribute == NULL) {
    cqi_command(cqi_errno);
  }
  else {
    /* we assemble the CQI_DATA_INT_LIST() return command by hand,
       so we don't have to allocate a temporary list */
    cqi_send_word(CQI_DATA_INT_LIST);
    cqi_send_int(len);          /* list size */
    for (i=0; i<len; i++) {
      f = cl_id2freq(attribute, idlist[i]);
      if (f < 0) 
        f = 0;                  /* return 0 if ID is out of range */
      cqi_send_int(f);
    }
  }
  cqi_flush();
  if (idlist != NULL)
    free(idlist);               /* don't forget to free allocated memory */
  free(a);
}
Exemplo n.º 11
0
/* one might wish to add extensive error checking to all the CL functions,
   but that will need a LOT of code! */
void
do_cqi_cl_str2id(void)
{
  char **strlist;
  int len, i, id;
  char *a;
  Attribute *attribute;

  a = cqi_read_string();
  len = cqi_read_string_list(&strlist);
  if (server_debug) {
   Rprintf( "CQi: CQI_CL_STR2ID('%s', [", a);
    for (i=0; i<len; i++)
     Rprintf( "'%s' ", strlist[i]);
   Rprintf( "])\n");
  }
  attribute = cqi_lookup_attribute(a, ATT_POS);
  if (attribute == NULL) {
    cqi_command(cqi_errno);
  }
  else {
    /* we assemble the CQI_DATA_INT_LIST() return command by hand,
       so we don't have to allocate a temporary list */
    cqi_send_word(CQI_DATA_INT_LIST);
    cqi_send_int(len);          /* list size */
    for (i=0; i<len; i++) {
      id = cl_str2id(attribute, strlist[i]);
      if (id < 0) 
        id = -1;                /* -1 => string not found in lexicon */
      cqi_send_int(id);
    }
  }
  cqi_flush();
  if (strlist != NULL)
    free(strlist);              /* don't forget to free allocated memory */
  free(a);
}
Exemplo n.º 12
0
void
do_cqi_cl_cpos2struc(void)
{
  int *cposlist;
  int len, i, struc;
  char *a;
  Attribute *attribute;

  a = cqi_read_string();
  len = cqi_read_int_list(&cposlist);
  if (server_debug) {
    fprintf(stderr, "CQi: CQI_CL_CPOS2STRUC('%s', [", a);
    for (i=0; i<len; i++)
      fprintf(stderr, "%d ", cposlist[i]);
    fprintf(stderr, "])\n");
  }
  attribute = cqi_lookup_attribute(a, ATT_STRUC);
  if (attribute == NULL) {
    cqi_command(cqi_errno);
  }
  else {
    /* we assemble the CQI_DATA_INT_LIST() return command by hand,
       so we don't have to allocate a temporary list */
    cqi_send_word(CQI_DATA_INT_LIST);
    cqi_send_int(len);          /* list size */
    for (i=0; i<len; i++) {
      struc = cl_cpos2struc(attribute, cposlist[i]);
      if (struc < 0) 
        struc = -1;                     /* return -1 if cpos is out of range */
      cqi_send_int(struc);
    }
  }
  cqi_flush();
  cl_free(cposlist);                    /* don't forget to free allocated memory */
  free(a);
}
Exemplo n.º 13
0
void
do_cqi_cqp_fdist_2(void)
{
  char *subcorpus;
  CorpusList *cl;
  int cutoff;
  cqi_byte field1, field2;
  char *att1, *att2;
  Group *table;
  int i, size;
  char *fieldname1, *fieldname2;
  FieldType fieldtype1 = NoField, fieldtype2 = NoField;
  int fields_ok = 1;            /* (both) fields valid? */

  subcorpus = cqi_read_string();
  cutoff = cqi_read_int();
  field1 = cqi_read_byte();
  att1 = cqi_read_string();
  field2 = cqi_read_byte();
  att2 = cqi_read_string();

  /* not exactly the fastest way to do it ... */
  fieldname1 = cqi_field_name(field1);
  if (fieldname1 == NULL) {
    fieldname1 = "<invalid field>";
    fields_ok = 0;
  }
  else {
    fieldtype1 = field_name_to_type(fieldname1);
  }
  fieldname2 = cqi_field_name(field2);
  if (fieldname2 == NULL) {
    fieldname2 = "<invalid field>";
    fields_ok = 0;
  }
  else {
    fieldtype2 = field_name_to_type(fieldname2);
  }
  if (server_debug) 
   Rprintf( "CQi: CQI_CQP_FDIST_2('%s', %d, %s, %s, %s, %s)\n", 
            subcorpus, cutoff, fieldname1, att1, fieldname2, att2);
  
  cl = cqi_find_corpus(subcorpus);
  if (cl == NULL) 
    cqi_command(cqi_errno);
  else if (!fields_ok)
    cqi_command(CQI_CQP_ERROR_INVALID_FIELD);
  else {
    /* compute_grouping() returns tokens with f > cutoff, but CQi specifies f >= cutoff */
    cutoff = (cutoff > 0) ? cutoff - 1 : 0;
    table = compute_grouping(cl, fieldtype1, 0, att1, fieldtype2, 0, att2, cutoff);
    if (table == NULL) {
      cqi_command(CQI_CQP_ERROR_GENERAL);
    }
    else {
      size = table->nr_cells;
      cqi_send_word(CQI_DATA_INT_TABLE);        /* return table with 3 columns & <size> rows */
      cqi_send_int(size);
      cqi_send_int(3);
      for (i=0; i < size; i++) {
        cqi_send_int(table->count_cells[i].s);
        cqi_send_int(table->count_cells[i].t);
        cqi_send_int(table->count_cells[i].freq);
      }
      cqi_flush();
      free_group(&table);
    }
  }

  cl_free(subcorpus);
  cl_free(att1);
  cl_free(att2);
}
Exemplo n.º 14
0
void
do_cqi_cqp_dump_subcorpus(void)
{
  char *subcorpus;
  CorpusList *cl;
  cqi_byte field;
  int i, first, last, size;
  char *fieldname;
  int field_ok = 1;             /* field valid? */

  subcorpus = cqi_read_string();
  field = cqi_read_byte();
  first = cqi_read_int();
  last = cqi_read_int();

  fieldname = cqi_field_name(field);
  if (fieldname == NULL) {
    fieldname = "<invalid field>";
    field_ok = 0;
  }
  if (server_debug) 
   Rprintf( "CQi: CQI_CQP_DUMP_SUBCORPUS('%s', %s, %d, %d)\n", 
            subcorpus, fieldname, first, last);

  cl = cqi_find_corpus(subcorpus);
  if (cl == NULL) 
    cqi_command(cqi_errno);
  else if (!field_ok)
    cqi_command(CQI_CQP_ERROR_INVALID_FIELD);
  else if ((last < first) || (first < 0) || (last >= cl->size)) 
    cqi_command(CQI_CQP_ERROR_OUT_OF_RANGE);
  else {
      cqi_send_word(CQI_DATA_INT_LIST); /* assemble by hand, so we don't have to allocate a temporary list */
      size = last - first + 1;
      cqi_send_int(size);
      switch (field) {
      case CQI_CONST_FIELD_MATCH:
        for (i=first; i<=last; i++)
          cqi_send_int(cl->range[i].start);
        break;
      case CQI_CONST_FIELD_MATCHEND:
        for (i=first; i<=last; i++)
          cqi_send_int(cl->range[i].end);
        break;
      case CQI_CONST_FIELD_TARGET:
        if (cl->targets == NULL) 
          do_cqi_send_minus_one_list(size);
        else 
          for (i=first; i<=last; i++)
            cqi_send_int(cl->targets[i]);
        break;
      case CQI_CONST_FIELD_KEYWORD:
        if (cl->keywords == NULL) 
          do_cqi_send_minus_one_list(size);
        else 
          for (i=first; i<=last; i++)
            cqi_send_int(cl->keywords[i]);
        break;
      default:
        cqiserver_internal_error("do_cqi_cqp_dump_subcorpus", "No handler for requested field.");
      }
      cqi_flush();
  }

  free(subcorpus);
}