Ejemplo n.º 1
1
/*
 * This function checks to see if the current YAML::Node contains only keys
 * that we care about. Unknown keys should cause PLFS to spit out an error
 * rather than being silently ignored.
 * This is a bit nasty as it drills through the entire tree recursively
 * but it will catch any unknowns in one pass
 *
 * Returns true if all keys are valid
 *
 * Returns false if unknown keys are found and sets bad_key to an error
 * message that points out what key is invalid
 */
bool
is_valid_node(const YAML::Node node, string** bad_key) {
    set<string> key_list(Valid_Keys, 
                         Valid_Keys + 
                         (sizeof(Valid_Keys) / sizeof(Valid_Keys[0]))
                        );
    string key;
    string err = "\nBad key or value in plfsrc: ";
    if(node.IsMap()) {
        for(YAML::const_iterator it=node.begin();it!=node.end();it++) {
            if(!it->first.IsNull()) {
                key = it->first.as<string>();
                if(!is_valid_node(node[key],bad_key)) // recurse
                    return false;
                if(key_list.find(key) == key_list.end()) {
                    err.append(key);
                    *bad_key = new string (err);
                    return false; // this is an unknown key
                }
            }
        }
    }
    else if (node.IsSequence()) {
        for(unsigned int i = 0; i < node.size(); i++)
            if(!is_valid_node(node[i],bad_key)) // recurse
                return false;
    }
    else if (node.IsScalar() && node.as<string>().find(" ") != string::npos) {
        err.append(node.as<string>());
        *bad_key = new string (err);
        return false; // no spaces in values allowed
    }
    return true; // all keys are valid
}
Ejemplo n.º 2
0
float Grid_2D :: get_edge(pair<uint32_t, uint32_t> start, cell_adjacent neighbor)
{
	pair<uint32_t, uint32_t> neighbor_node = neighbor_to_pair(start, neighbor);
	if(!is_valid_node(start) || !is_neighbor(start, neighbor_node))
	{
		return INFINITY;
	}

	uint8_t start_weight = get_weight(start);
	uint8_t neighbor_weight = get_weight(get_neighbor(start, neighbor));

	if(start_weight == INF || neighbor_weight == INF)
	{
		return INFINITY;
	}

	float edge = start_weight + neighbor_weight;
	edge /= 2;
	if(neighbor == TOP_LEFT || neighbor == TOP_RIGHT
	   || neighbor == BOTTOM_LEFT || neighbor == BOTTOM_RIGHT)
	{
		edge *= sqrt(2);
	}
	return edge;
}
Ejemplo n.º 3
0
void Grid_2D :: place_path(pair<uint32_t, uint32_t> node)
{
	uint32_t index = to_index(node.first, node.second);
	if(!is_valid_node(node))
	{
		this->path[index] = TRAVELED;
	}
	return;
}
Ejemplo n.º 4
0
pair<uint32_t, uint32_t> Grid_2D :: get_neighbor(pair<uint32_t,uint32_t> node, cell_adjacent neighbor)
{
	pair<uint32_t, uint32_t> neighbor_node = neighbor_to_pair(node, neighbor);
	if(!is_valid_node(node) || !is_neighbor(node, neighbor_node))
	{
		return node;
	}
	return neighbor_node;
}
Ejemplo n.º 5
0
void Grid_2D :: set_display(pair<uint32_t, uint32_t> node, char display)
{
	if(!is_valid_node(node))
	{
		uint32_t index = to_index(node.first, node.second);
		if(display != '.' && display != '#')
		{
			this->displays[index] = (uint8_t)display;
		}
	}
	return;
}
Ejemplo n.º 6
0
bool Grid_2D :: is_neighbor(pair<uint32_t, uint32_t> node, pair<uint32_t, uint32_t> neighbor)
{
	if(!is_valid_node(node))
	{
		return false;
	}

	int64_t i = node.first;
	int64_t j = node.second;

	bool neighbor_possible[MAX_NEIGHBORS];
	int64_t neighbors[MAX_NEIGHBORS];

	neighbors[TOP] 		    = to_index(i + 1, j);
	neighbors[TOP_LEFT] 	= to_index(i + 1, j - 1);
	neighbors[TOP_RIGHT] 	= to_index(i + 1, j + 1);
	neighbors[LEFT]		    = to_index(i, j - 1);
	neighbors[RIGHT] 		= to_index(i, j + 1);
	neighbors[BOTTOM] 		= to_index(i - 1, j);
	neighbors[BOTTOM_LEFT]  = to_index(i - 1, j - 1);
	neighbors[BOTTOM_RIGHT] = to_index(i - 1, j + 1);

	neighbor_possible[TOP] 		    = i < height;
	neighbor_possible[TOP_LEFT] 	= i < height && j != 0;
	neighbor_possible[TOP_RIGHT] 	= i < height && j < width;
	neighbor_possible[LEFT]		    = j != 0;
	neighbor_possible[RIGHT] 		= j < width;;
	neighbor_possible[BOTTOM] 		= i != 0;
	neighbor_possible[BOTTOM_LEFT]  = i != 0 && j != 0;
	neighbor_possible[BOTTOM_RIGHT] = i != 0 && j < width;

	uint32_t index = to_index(neighbor.first, neighbor.second);
	uint32_t max_index = this->width*this->height;

	for(uint32_t k = 0; k < MAX_NEIGHBORS; k ++)
	{
		if(index == neighbors[k] && neighbor_possible[k])
		{
			return true;
		}
	}
	return false;
}
Ejemplo n.º 7
0
N *Tokenizer<N, P>::lookup(const char *begin, const char *end,
                           Allocator<N, P> *allocator, Lattice *lattice) const {
  CharInfo cinfo;
  N *result_node = 0;
  size_t mblen = 0;
  size_t clen = 0;

  end = static_cast<size_t>(end - begin) >= 65535 ? begin + 65535 : end;

  if (isPartial) {
    const size_t begin_pos = begin - lattice->sentence();
    for (size_t n = begin_pos + 1; n < lattice->size(); ++n) {
      if (lattice->boundary_constraint(n) == MECAB_TOKEN_BOUNDARY) {
        end = lattice->sentence() + n;
        break;
      }
    }
  }

  const char *begin2 = property_.seekToOtherType(begin, end, space_,
                                                 &cinfo, &mblen, &clen);

  Dictionary::result_type *daresults = allocator->mutable_results();
  const size_t results_size = allocator->results_size();

  for (std::vector<Dictionary *>::const_iterator it = dic_.begin();
       it != dic_.end(); ++it) {
    const size_t n = (*it)->commonPrefixSearch(
        begin2,
        static_cast<size_t>(end - begin2),
        daresults, results_size);

    for (size_t i = 0; i < n; ++i) {
      size_t size = (*it)->token_size(daresults[i]);
      const Token *token = (*it)->token(daresults[i]);
      for (size_t j = 0; j < size; ++j) {
        N *new_node = allocator->newNode();
        read_node_info(**it, *(token + j), &new_node);
        new_node->length = daresults[i].length;
        new_node->rlength = begin2 - begin + new_node->length;
        new_node->surface = begin2;
        new_node->stat = MECAB_NOR_NODE;
        new_node->char_type = cinfo.default_type;
        if (isPartial && !is_valid_node(lattice, new_node)) {
          continue;
        }
        new_node->bnext = result_node;
        result_node = new_node;
      }
    }
  }

  if (result_node && !cinfo.invoke) {
    return result_node;
  }

  const char *begin3 = begin2 + mblen;
  const char *group_begin3 = 0;

  if (begin3 > end) {
    ADDUNKNWON;
    if (result_node) {
    return result_node;
  }
  }

  if (cinfo.group) {
    const char *tmp = begin3;
    CharInfo fail;
    begin3 = property_.seekToOtherType(begin3, end, cinfo,
                                       &fail, &mblen, &clen);
    if (clen <= max_grouping_size_) {
      ADDUNKNWON;
    }
    group_begin3 = begin3;
    begin3 = tmp;
  }

  for (size_t i = 1; i <= cinfo.length; ++i) {
    if (begin3 > end) {
      break;
    }
    if (begin3 == group_begin3) {
      continue;
    }
    clen = i;
    ADDUNKNWON;
    if (!cinfo.isKindOf(property_.getCharInfo(begin3, end, &mblen))) {
      break;
    }
    begin3 += mblen;
  }

  if (!result_node) {
    ADDUNKNWON;
  }

  if (isPartial && !result_node) {
    begin3 = begin2;
    while (true) {
      cinfo = property_.getCharInfo(begin3, end, &mblen);
      begin3 += mblen;
      if (begin3 > end ||
          lattice->boundary_constraint(begin3 - lattice->sentence())
          != MECAB_INSIDE_TOKEN) {
        break;
      }
    }
    ADDUNKNWON;

    if (!result_node) {
      N *new_node = allocator->newNode();
      new_node->char_type = cinfo.default_type;
      new_node->surface = begin2;
      new_node->length = begin3 - begin2;
      new_node->rlength = begin3 - begin;
      new_node->stat = MECAB_UNK_NODE;
      new_node->bnext = result_node;
      new_node->feature =
          lattice->feature_constraint(begin - lattice->sentence());
      CHECK_DIE(new_node->feature);
      result_node = new_node;
    }
  }

  return result_node;
}