Example #1
0
indri::api::SnippetBuilder::Region indri::api::SnippetBuilder::_bestRegion(
  const std::vector< std::pair<indri::index::Extent, int> >& extents,
  const std::vector< indri::api::SnippetBuilder::Region >& skipRegions,
  int positionCount, int windowWidth ) {
  // try to find as many unique occurrences as possible
  Region best;
  size_t bestUnique = 0;
  std::set<int> bestSet;
  best.begin = 0;
  best.end = 0;

  std::vector< Region >::const_iterator skipIter = skipRegions.begin();

  for( size_t i=0; i<extents.size(); i++ ) {
    if( should_skip( skipRegions, extents[i].first.begin, extents[i].first.end ) )
      continue;

    // if this extent is past the end, it doesn't count
    if( extents[i].first.begin >= positionCount )
      break;

    // okay, now let's really look for a nice extent
    Region region;
    region.begin = extents[i].first.begin;
    region.end = extents[i].first.end;
    region.extents.push_back( extents[i].first );

    std::set<int> nodes;
    nodes.insert( extents[i].second );
    size_t j;

    for( j=i; j<extents.size(); j++ ) {
      int newEnd = std::max( extents[j].first.end, region.end );

      if( newEnd - region.begin > windowWidth || should_skip( skipRegions, extents[j].first.begin, extents[j].first.end ) )
        break;

      // remove duplicate and/or overlapping extents
      if( region.extents.back().end < extents[j].first.begin ) {
        region.extents.push_back( extents[j].first );
      } else {
        region.extents.back().end = extents[j].first.end;
      }

      nodes.insert( extents[j].second );
      region.end = newEnd;
    }

    if( bestUnique < nodes.size() ) {
      best = region;
      bestUnique = nodes.size();
      bestSet = nodes;
    }
  }

  return best;
}
Example #2
0
int
varnam_detect_lang(varnam *handle, const char *input)
{
    strbuf *word;
    utf8_decoder decoder;
    int codepoint, language = VARNAM_LANG_CODE_UNKNOWN, prev_language = 0;
    
    if (handle == NULL || input == NULL) {
        return VARNAM_LANG_CODE_UNKNOWN;
    }

    word = get_pooled_string (handle);
    strbuf_add (word, input);

    if (strbuf_is_blank (word)) {
        return VARNAM_LANG_CODE_UNKNOWN;
    }

    utf8_decode_init (word->buffer, (int) word->length, &decoder);

    for (;;)
    {
        codepoint = utf8_decode_next (&decoder);
        if (codepoint == UTF8_END || codepoint == UTF8_ERROR)
            break;

        if (should_skip(codepoint))
            continue;

        language = get_language (codepoint);

        if (language == VARNAM_LANG_CODE_UNKNOWN)
            return VARNAM_LANG_CODE_UNKNOWN;
        
        if (prev_language != 0 && language != prev_language) {
            /* Looks like characters from multiple languages are mixed */
            return VARNAM_LANG_CODE_UNKNOWN;
        }
        prev_language = language;
    }

    return language;
}
Example #3
0
/* Read input from the gln_filter co-process and
 * enqueue files to be indexed.
 * Returns <0 on error. */
int filter_enqueue_files(context *c) {
    char *buf;
    size_t len;
    fname *fn;
    uint ct=0, sp = (c->verbose || c->show_progress);
    int skip = 0;
    if (c->find == NULL) {
        fprintf(stderr, "Failed to start 'find' process.\n");
        return -1;
    }
    
    if (sp) fprintf(stderr, "-- Enqueueing all files to index...\n");
    
    while ((buf = nextline(c->find, &len)) != NULL) {
        skip = should_skip(c->filter_fd, buf, len);
        if (buf[len - 1] == '\n') buf[len - 1] = '\0';
        if (skip) {
            if (c->verbose || DEBUG)
                fprintf(stderr, "Ignoring: %s\n", buf);
        } else {
            fn = fname_new(buf, len);
            v_array_append(c->fnames, fn);
            if (c->verbose > 1) fprintf(stderr, "Appended: %d %s\n",
                v_array_length(c->fnames), fn->name);
            ct++;
            if (ct % ENQUEUE_PROGRESS_CT == 0)
                fprintf(stderr, "%u files enqueued so far...\n", ct);
        }
    }
    
    /* clean up find & filter processes */
    if (close(c->filter_fd) == -1) err(1, "close");
    if ((kill(c->filter_pid, SIGKILL)) == -1) err(1, "kill");
    if ((wait(NULL) == -1)) err(1, "wait");
    c->find = NULL;
    
    c->tick_max = v_array_length(c->fnames) / 100;
    
    if (sp) fprintf(stderr, "-- %u files enqueued.\n", ct);
    return 0;
}
Example #4
0
File: adc.c Project: Pagten/lab-psu
static inline adc*
find_next_adc_to_queue(void)
{
  static uint8_t period = 0;
  adc* adc = next_adc_to_consider;
  while(adcs != NULL) {
    while(adc != NULL && should_skip(adc, period)) {
      adc = adc->next;
    }
    
    if (adc != NULL) {
      // Found next adc to queue
      next_adc_to_consider = adc->next;
      return adc;
    }

    // Finished current period
    adc = adcs;
    period += 1;
  }
  return NULL;
}
Example #5
0
ssize_t ELM_convert(char * reply, char * buffer, size_t buffer_size)
{
   size_t i;
   for (i = 0; i < sizeof(errors) / sizeof(const char *); ++i)
   {
      if (strstr(reply, errors[i]))
      {
         snprintf(buffer, buffer_size, "elm error (%s)", errors[i]);
         return -1;
      }
   }

   /* 7F means not supported by the ECU. NO DATA means the ECU didn't reply */
   if (strncmp(reply, "7F ", 3) == 0)
   {
      snprintf(buffer, buffer_size, "Unknown command (ECU doesn't known)");
      return -1;
   }

   if (*reply == '?')
   {
      snprintf(buffer, buffer_size, "Unknown command (no reply)");
      return -1;
   }

   char * saveptr1;
   char * saveptr2;
   char * str1;
   char * str2;

   int ml = 0;
   size_t offset = 0;

   for (str1 = reply; ; str1 = NULL)
   {
      char * line = strtok_r(str1, "\n\r", &saveptr1);
      if (!line)
         break;

      /* check for foo lines */
      if (should_skip(line))
         continue;

      /* check that line is multiline sign */
      if (!ml && strlen(line) == 4 && isxdigit(line[0]) && isxdigit(line[1]) && isxdigit(line[2]) && line[3] == ' ')
      {
         ml = 1;
         continue;
      }

      /* parse numbers */
      for (str2 = line; ; str2 = NULL)
      {
         char * num = strtok_r(str2, " ", &saveptr2);
         if (!num)
            break;

         if (ml && strchr(num, ':'))
            continue;

         if (offset < buffer_size)
         {
            char * eptr;
            int ret = strtoll(num, &eptr, 16);
            if (eptr != num + 2)
            {
               snprintf(buffer, buffer_size, "Failed to convert hex-str to binary (%s)", num);
               return -1;
            }

            buffer[offset++] = (char) ret;
         }
         else
         {
            snprintf(buffer, buffer_size, "Buffer is small");
            return -1;
         }
      } /* for (str2 = line; ; str2 = NULL) */
   } /*  for (str1 = data; ; str1 = NULL) */

   return offset;
}
Example #6
0
Term AtomicTo1(Term t, Term ind)
	{
	List rl;
	Term ret;
	Term t1;
	Atom ttype;
	
	if(is_compound(t) && CompoundName(t)==A_ALG1)
	{
		List l;
		ttype=A_ALG1;
		rl=CopyTerm(CompoundArg2(t));
		for(l=rl;l;l=ListTail(l))
			SetCompoundArg(ListFirst(l),2,0);
		goto cnt;
	}
	
	if(is_compound(t) && CompoundName(t)==A_FBRACET)
		{
		t=alg1_mk_wild(t,&rl,&ind);
		ttype=OPR_WILD;
		goto cnt;
		}
	/*
	if(is_compound(t) && CompoundName(t)==A_CC)
		{
		t=cc_particle(t,&rl);
		ttype=OPR_FIELD;
		goto cnt;
		}
	*/
	
	if(is_let(t,&rl))
		{
		ttype=OPR_LET;
		goto cnt;
		}
	
	if(is_parameter(t) || (is_compound(t) && (CompoundName(t)==A_COS ||
						CompoundName(t)==A_SIN)) )
		{
		rl=NewList();
		ttype=OPR_PARAMETER;
		goto cnt;
		}
		
	if(is_particle(t,&rl))
		{
		ttype=OPR_FIELD;
		goto cnt;
		}
		
	if(is_special(t,&rl))
		{
		ttype=OPR_SPECIAL;
		goto cnt;
		}
	
	ErrorInfo(301);
	printf(" \'%s\' undefined object.\n",AtomValue(t));
	FreeAtomic(ind);
	longjmp(alg1_jmp_buf,1);
cnt:
	ret=MakeCompound(A_MTERM,4);
	SetCompoundArg(ret,1,NewInteger(1));
	SetCompoundArg(ret,2,NewInteger(1));
	t1=0;
	if(!is_empty_list(rl))
		{
		Term ttt;
		int skipped;
		rl=CopyTerm(rl);
		if(is_empty_list(ind))
			{
			SetCompoundArg(ret,3,AppendFirst(NewList(),MakeCompound2(ttype,rl,t)));
			return AppendFirst(NewList(),ret);
			}
		skipped=must_skip(ListLength(ind),rl);
		if(skipped==-1)
			{
			ErrorInfo(302);
			printf(" can not set indices ");
			WriteTerm(ind);
			printf(" to \'%s\'.\n",AtomValue(t));
			FreeAtomic(ind); FreeAtomic(rl);
			longjmp(alg1_jmp_buf,1);
			}
		
		t1=rl;	
		ttt=ind;
		while(!is_empty_list(rl))
			{
			if(skipped!=0 && should_skip(skipped,CompoundArg1(ListFirst(rl))))
				{ rl=ListTail(rl); continue; }
			SetCompoundArg(ListFirst(rl),2,ListFirst(ttt));
			rl=ListTail(rl);
			ttt=ListTail(ttt);
			}
		FreeAtomic(ind);
		}
		
	if(ttype==OPR_PARAMETER && is_compound(t))
	{
	Term mmm=MakeCompound(ttype,CompoundArity(t)+2);
	SetCompoundArg(mmm,1,t1);
	SetCompoundArg(mmm,2,CompoundName(t));
	SetCompoundArg(mmm,3,CompoundArg1(t));
	if(CompoundArity(t)==2)
		{
		Term m=ExprTo1(ConsumeCompoundArg(t,2));
		m=CompoundArg1(m);
		if(ListLength(m)!=1)
			{
			ErrorInfo(0);
			puts("bad expression in sin/cos.");
			longjmp(alg1_jmp_buf,1);
			}
		m=ListFirst(m);
		SetCompoundArg(mmm,4,m);
		}
	SetCompoundArg(ret,3,AppendFirst(NewList(),mmm));
	}
	else
	SetCompoundArg(ret,3,AppendFirst(NewList(),MakeCompound2(ttype,t1,t)));
	return AppendFirst(NewList(),ret);
	
	}