int main(int argc, char *argv[])
{
	void *gen_toks; int i;
	const char *test_strings[] = {
	"其实,工信处女干事sandy每月经过下属办公室",
	"我来到北京清华大学",
	"他来到了网易杭研大厦",
	"小明硕士毕业于中国科学院计算所,后在日本京都大学深造",
	"永和服装饰品有限公司",
	};
	const int n_test_strings = sizeof test_strings / sizeof(char*);

	jieba_init();

	for (i = 0; i < n_test_strings; i++) {
		printf("\n");
		printf("#%d: %s\n", i, test_strings[i]);

		gen_toks = jieba_cut(test_strings[i],
		                     strlen(test_strings[i]));
		if (gen_toks) {
			foreach_tok(gen_toks, &jieba_token_print);

			//PRINT_REF_CNT(gen_toks);
			Py_DECREF(gen_toks);
		}
	}

	jieba_release();
	return 0;

	py_memory_leak_demo();
}
Esempio n. 2
0
void jieba_init(void)
{
	void *res;
	Py_Initialize();

	if (!Py_IsInitialized()) {
		printf("Py_Initialize() fails.\n");
		assert(0);
	}

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('./')");

	/* Decode a string using Py_FileSystemDefaultEncoding */
	jieba_wrap_nm = PyUnicode_DecodeFSDefault(JIEBA_WRAP_NAME);

	jieba_module = PyImport_Import(jieba_wrap_nm); /* new ref */
	if (jieba_module == NULL) {
		printf("PyImport_Import() fails.\n");
		goto free;
	}

	jieba_init_func = PyObject_GetAttrString(jieba_module,
			JIEBA_WRAP_INIT_FUN); /* new ref */
	if (jieba_init_func == NULL) {
		printf("PyObject_GetAttrString() fails.\n");

		jieba_release();
		jieba_module = NULL;
	}

	assert(1 == PyCallable_Check(jieba_init_func));

	/* call jieba_init_func in Python */
	res = PyObject_CallObject(jieba_init_func, NULL); /* new ref */
	Py_DECREF(res);
	Py_DECREF(jieba_init_func);

free:
	/* should free obj returned by PyUnicode_DecodeFSDefault(), see #1.3:
	 * http://python.readthedocs.org/en/latest/extending/embedding.html */
	Py_DECREF(jieba_wrap_nm);

	assert(jieba_module != NULL);

	goto invoke_lazy_dict_load; /* uncomment if you do not want this */
	return;

invoke_lazy_dict_load:
	res = jieba_cut("初始化test", strlen("初始化test"));
	if (res) {
		foreach_tok(res, &token_donothing, NULL);

		// PRINT_REF_CNT(res);
		Py_DECREF(res);
	}
}