示例#1
0
void dictionary_delete(Dictionary dict)
{
	if (!dict) return;

	if (verbosity > 0) {
		prt_error("Info: Freeing dictionary %s", dict->name);
	}

#ifdef USE_CORPUS
	lg_corpus_delete(dict->corpus);
#endif

	if (dict->affix_table != NULL) {
		affix_list_delete(dict->affix_table);
		dictionary_delete(dict->affix_table);
	}
	spellcheck_destroy(dict->spell_checker);

	connector_set_delete(dict->unlimited_connector_set);

	if (dict->close) dict->close(dict);

	pp_knowledge_close(dict->base_knowledge);
	pp_knowledge_close(dict->hpsg_knowledge);
	string_set_delete(dict->string_set);
	free_regexs(dict->regex_root);
#ifdef USE_ANYSPLIT
	free_anysplit(dict);
#endif
	free_dictionary(dict);
	xfree(dict, sizeof(struct Dictionary_s));
	object_open(NULL, NULL, NULL); /* Free the directory path cache */
}
示例#2
0
文件: commit.c 项目: spolu/ur
int 
commit_read (state_t *ur, struct commit *commit, const unsigned char sha1[20])
{
  int fd = -1;
  char * buf = NULL;
  int len;
  struct tm tm;
  char *cp;

  commit->msg = NULL;

  if (commit->alive) goto error;

  if ((fd = object_open (ur, sha1)) < -1) goto error;

  buf = readline (fd);  
  if (buf == NULL) goto error;
  cp = strptime (buf, "%a %b %d %T %Y", &tm);
  commit->ctime = mktime (&tm);
  free (buf); buf = NULL;

  buf = readline (fd);  
  if (buf == NULL) goto error;
  hex_to_sha1 (buf, commit->parent_sha1_1);
  free (buf); buf = NULL;

  buf = readline (fd);  
  if (buf == NULL) goto error;
  hex_to_sha1 (buf, commit->parent_sha1_2);
  free (buf); buf = NULL;

  buf = readline (fd);
  if (buf == NULL) goto error;
  commit->object_type = atoi (buf);
  free (buf); buf = NULL;

  buf = readline (fd);  
  if (buf == NULL) goto error;
  hex_to_sha1 (buf, commit->object_sha1);
  free (buf); buf = NULL;
  
  buf = readline (fd);
  if (buf == NULL) goto error;    
  len = atoi (buf);
  free (buf); buf = NULL;

  commit->msg = (char *) malloc (len);
  readn (fd, commit->msg, len);

  close (fd);

  commit->alive = true;

  return 0;

 error:  
 if (commit->msg != NULL) free (commit->msg);

  return -1;
}
示例#3
0
Dictionary dictionary_create_from_db(const char *lang)
{
	char *dbname;
	const char * t;
	Dictionary dict;
	Dict_node *dict_node;

	dict = (Dictionary) xalloc(sizeof(struct Dictionary_s));
	memset(dict, 0, sizeof(struct Dictionary_s));

	dict->version = NULL;
	dict->num_entries = 0;
	dict->affix_table = NULL;
	dict->regex_root = NULL;

	/* Language and file-name stuff */
	dict->string_set = string_set_create();
	dict->lang = lang;
	t = strrchr (lang, '/');
	if (t) dict->lang = string_set_add(t+1, dict->string_set);

	/* To disable spell-checking, just set the checker to NULL */
	dict->spell_checker = spellcheck_create(dict->lang);
	dict->base_knowledge = NULL;
	dict->hpsg_knowledge = NULL;

	dbname = join_path (lang, "dict.db");
	dict->name = string_set_add(dbname, dict->string_set);
	free(dbname);

	/* Set up the database */
	dict->db_handle = object_open(dict->name, db_open, NULL);

	dict->lookup_list = db_lookup_list;
	dict->free_lookup = db_free_llist;
	dict->lookup = db_lookup;
	dict->close = db_close;

	/* Misc remaining common (generic) dict setup work */
	dict->left_wall_defined  = boolean_dictionary_lookup(dict, LEFT_WALL_WORD);
	dict->right_wall_defined = boolean_dictionary_lookup(dict, RIGHT_WALL_WORD);

	dict->empty_word_defined = boolean_dictionary_lookup(dict, EMPTY_WORD_MARK);

	dict->unknown_word_defined = boolean_dictionary_lookup(dict, UNKNOWN_WORD);
	dict->use_unknown_word = true;

	dict_node = dictionary_lookup_list(dict, UNLIMITED_CONNECTORS_WORD);
	if (dict_node != NULL) {
		dict->unlimited_connector_set = connector_set_create(dict_node->exp);
	} else {
		dict->unlimited_connector_set = NULL;
	}
	free_lookup_list(dict, dict_node);

	return dict;
}
示例#4
0
Dictionary dictionary_create_from_db(const char *lang)
{
	char *dbname;
	const char * t;
	Dictionary dict;
	Dict_node *dict_node;

	dict = (Dictionary) xalloc(sizeof(struct Dictionary_s));
	memset(dict, 0, sizeof(struct Dictionary_s));

	/* Language and file-name stuff */
	dict->string_set = string_set_create();
	t = strrchr (lang, '/');
	t = (NULL == t) ? lang : t+1;
	dict->lang = string_set_add(t, dict->string_set);
	lgdebug(D_USER_FILES, "Debug: Language: %s\n", dict->lang);

	/* To disable spell-checking, just set the checker to NULL */
	dict->spell_checker = spellcheck_create(dict->lang);
#if defined HAVE_HUNSPELL || defined HAVE_ASPELL
	if (NULL == dict->spell_checker)
		prt_error("Info: Spell checker disabled.");
#endif
	dict->base_knowledge = NULL;
	dict->hpsg_knowledge = NULL;

	dbname = join_path (lang, "dict.db");
	dict->name = string_set_add(dbname, dict->string_set);
	free(dbname);

	/* Set up the database */
	dict->db_handle = object_open(dict->name, db_open, NULL);

	dict->lookup_list = db_lookup_list;
	dict->free_lookup = db_free_llist;
	dict->lookup = db_lookup;
	dict->close = db_close;

	/* Misc remaining common (generic) dict setup work */
	dict->left_wall_defined  = boolean_dictionary_lookup(dict, LEFT_WALL_WORD);
	dict->right_wall_defined = boolean_dictionary_lookup(dict, RIGHT_WALL_WORD);

	dict->empty_word_defined = boolean_dictionary_lookup(dict, EMPTY_WORD_MARK);

	dict->unknown_word_defined = boolean_dictionary_lookup(dict, UNKNOWN_WORD);
	dict->use_unknown_word = true;

	dict_node = dictionary_lookup_list(dict, UNLIMITED_CONNECTORS_WORD);
	if (dict_node != NULL)
		dict->unlimited_connector_set = connector_set_create(dict_node->exp);

	free_lookup_list(dict, dict_node);

	return dict;
}
示例#5
0
文件: execfile.c 项目: wodz/open21xx
void add_one_object( const char *name )
{
    OBJECT_FILE *file;
    OBJECT_FILE **save;
    int available;

    file = object_open( name );
    if (file)
    {
        save = array_alloc( &link_array, &available );
        *save = file;
        array_update( &link_array, 1 );
    }
}
示例#6
0
Dictionary dictionary_create_lang(const char * lang)
{
	Dictionary dictionary = NULL;

	object_open(NULL, NULL, NULL); /* Invalidate the directory path cache */

	/* If an sql database exists, try to read that. */
	if (check_db(lang))
	{
		dictionary = dictionary_create_from_db(lang);
	}

	/* Fallback to a plain-text dictionary */
	if (NULL == dictionary)
	{
		dictionary = dictionary_create_from_file(lang);
	}

	return dictionary;
}
示例#7
0
FILE *dictopen(const char *filename, const char *how)
{
	return object_open(filename, dict_file_open, how);
}
示例#8
0
文件: tree.c 项目: spolu/ur
int 
tree_read (state_t *ur, struct tree *tree, unsigned char sha1[20])
{
  int fd = -1, i;
  int blob_cnt = 0;
  int branch_cnt = 0;
  char * buf = NULL;
  char *name = NULL, *branch = NULL;
  unsigned char commit[20];
  struct blob_tree_entry *blob_entry = NULL;
  struct branch_tree_entry *branch_entry = NULL;

  tree_init (tree);

  if ((fd = object_open (ur, sha1)) < -1)
    goto error;
  
  buf = readline (fd);
  if (buf == NULL) goto error;
  blob_cnt = atoi (buf);
  free (buf); buf = NULL;

  for (i = 0; i < blob_cnt; i ++) 
    {        
      buf = readline (fd);
      if (buf == NULL) goto error;
      name = buf;
      buf = NULL;

      buf = readline (fd);
      if (buf == NULL) goto error;
      hex_to_sha1 (buf, commit);
      free (buf); buf = NULL;
      
      blob_entry = (struct blob_tree_entry *) malloc (sizeof (struct blob_tree_entry));
      if (blob_entry == NULL)
	goto error;
      
      blob_entry->name = name;
      memcpy (blob_entry->commit, commit, 20);

      list_push_back (&tree->blob_entries, &blob_entry->elem);

      name = NULL;
      blob_entry = NULL;
  }
  

  buf = readline (fd);
  if (buf == NULL) goto error;
  branch_cnt = atoi (buf);
  free (buf); buf = NULL;

  for (i = 0; i < branch_cnt; i ++) 
    {        
      buf = readline (fd);
      if (buf == NULL) goto error;
      name = buf;
      buf = NULL;

      buf = readline (fd);
      if (buf == NULL) goto error;
      branch = buf;
      buf = NULL;

      buf = readline (fd);
      if (buf == NULL) goto error;
      hex_to_sha1 (buf, commit);
      free (buf); buf = NULL;
      
      branch_entry = (struct branch_tree_entry *) malloc (sizeof (struct branch_tree_entry));
      if (branch_entry == NULL)
	goto error;
      
      branch_entry->name = name;
      branch_entry->branch = branch;
      memcpy (branch_entry->commit, commit, 20);

      list_push_back (&tree->branch_entries, &branch_entry->elem);

      name = NULL;
      branch = NULL;
      branch_entry = NULL;
  }

  close (fd);
  
  return 0;

 error:
  if (name != NULL) free (name);
  if (branch != NULL) free (branch);
  if (blob_entry != NULL) free (blob_entry);
  if (branch_entry != NULL) free (branch_entry);
  
  tree_destroy (tree);
  
  return -1;
}
示例#9
0
文件: corpus.c 项目: dyne/AutOrg
/**
 * Initialize the corpus statistics subsystem.
 */
Corpus * lg_corpus_new(void)
{
	int rc;

	Corpus *c = (Corpus *) malloc(sizeof(Corpus));
	c->rank_query = NULL;
	c->sense_query = NULL;
	c->errmsg = NULL;
	c->dbname = NULL;

	/* dbname = "/link-grammar/data/en/sql/disjuncts.db"; */
#ifdef _WIN32
#define DBNAME "sql\\disjuncts.db"
#else
#define DBNAME "sql/disjuncts.db"
#endif
	c->dbconn = object_open(DBNAME, db_file_open, c);
	if (NULL == c->dbconn)
	{
		if (SQLITE_CANTOPEN == c->rc)
		{
			prt_error("Warning: File not found: %s\n"
			          "\tWas looking for: " DBNAME,
				c->errmsg);
		}
		else
		{ 
			prt_error("Warning: Can't open database: %s\n"
			          "\tWas looking for: " DBNAME,
				c->errmsg);
		}
		return c;
	}

	/* Now prepare the statements we plan to use */
	rc = sqlite3_prepare_v2(c->dbconn, 	
		"SELECT log_cond_probability FROM Disjuncts "
		"WHERE inflected_word = ? AND disjunct = ?;",
		-1, &c->rank_query, NULL);
	if (rc != SQLITE_OK)
	{
		prt_error("Error: Can't prepare the ranking statment: %s\n",
			sqlite3_errmsg(c->dbconn));
	}

	/* Results are returned in sorted order .. would it be faster
	 * to sort locally? Don't know ... */
	rc = sqlite3_prepare_v2(c->dbconn, 	
		"SELECT word_sense, log_cond_probability FROM DisjunctSenses "
		"WHERE inflected_word = ? AND disjunct = ? "
		"ORDER BY log_cond_probability ASC;",
		-1, &c->sense_query, NULL);
	if (rc != SQLITE_OK)
	{
		prt_error("Error: Can't prepare the sense statment: %s\n",
			sqlite3_errmsg(c->dbconn));
	}

	prt_error("Info: Corpus statistics database found at %s\n", c->dbname);
	return c;
}
示例#10
0
/**
 * Open a file in the dictionary search path.
 * Experimental API (may be unstable).
 * @param filename Filename to be opened.
 * @return FILE pointer, or NULL if the file was no found.
 */
FILE *linkgrammar_open_data_file(const char *filename)
{
	object_open(NULL, NULL, NULL); /* Invalidate the directory path cache */
	return object_open(filename, data_file_open, "r");
}