Skip to content

Commit

Permalink
Merge pull request #19 from bab2min/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
bab2min authored Nov 27, 2019
2 parents 434c4a9 + b09b898 commit fffdfd1
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 11 deletions.
5 changes: 4 additions & 1 deletion README.kr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tomotopy 란?

더 자세한 정보는 https://bab2min.github.io/tomotopy/index.kr.html 에서 확인하시길 바랍니다.

tomotopy의 가장 최신버전은 0.4.0 입니다.
tomotopy의 가장 최신버전은 0.4.1 입니다.

시작하기
---------------
Expand Down Expand Up @@ -197,6 +197,9 @@ tomotopy의 Python3 예제 코드는 https://github.com/bab2min/tomotopy/blob/ma

역사
-------
* 0.4.1 (2019-11-27)
* `tomotopy.PLDAModel` 생성자의 버그를 수정했습니다.

* 0.4.0 (2019-11-18)
* `tomotopy.PLDAModel`와 `tomotopy.HLDAModel` 토픽 모델이 새로 추가되었습니다.

Expand Down
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The current version of `tomoto` supports several major topic models including

Please visit https://bab2min.github.io/tomotopy to see more information.

The most recent version of tomotopy is 0.4.0.
The most recent version of tomotopy is 0.4.1.

Getting Started
---------------
Expand Down Expand Up @@ -202,6 +202,9 @@ meaning you can use it for any reasonable purpose and remain in complete ownersh

History
-------
* 0.4.1 (2019-11-27)
* A bug at init function of `tomotopy.PLDAModel` was fixed.

* 0.4.0 (2019-11-18)
* New models including `tomotopy.PLDAModel` and `tomotopy.HLDAModel` were added into the package.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
setup(
name='tomotopy',

version='0.4.0',
version='0.4.1',

description='Tomoto, The Topic Modeling Tool for Python',
long_description=long_description,
Expand Down
38 changes: 37 additions & 1 deletion src/python/PyUtils.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#pragma once
#include <Python.h>
#include <frameobject.h>
#include <type_traits>
#include <vector>
#include <tuple>
#include <set>
#include <limits>
#include <exception>
#include <string>
#include <iostream>

namespace py
{
Expand Down Expand Up @@ -184,4 +188,36 @@ namespace py
}
return ret;
}
}

class WarningLog
{
std::set<std::tuple<std::string, int, std::string>> printed;

WarningLog()
{
}
public:
static WarningLog& get()
{
thread_local WarningLog inst;
return inst;
}

void printOnce(std::ostream& ostr, const std::string& msg)
{
auto frame = PyEval_GetFrame();
auto key = std::make_tuple(
std::string{ PyUnicode_AsUTF8(frame->f_code->co_filename) },
PyFrame_GetLineNumber(frame),
msg);

if (!printed.count(key))
{
ostr << std::get<0>(key) << "(" << std::get<1>(key) << "): " << std::get<2>(key) << std::endl;
printed.insert(key);
}
}
};
}

#define PRINT_WARN(msg) do{ py::WarningLog::get().printOnce(std::cerr, msg); } while(0)
2 changes: 2 additions & 0 deletions src/python/py_DMR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static PyObject* DMR_addDoc(TopicModelObject* self, PyObject* args, PyObject *kw
if (!self->inst) throw runtime_error{ "inst is null" };
if (self->isPrepared) throw runtime_error{ "cannot add_doc() after train()" };
auto* inst = static_cast<tomoto::IDMRModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand Down Expand Up @@ -71,6 +72,7 @@ static PyObject* DMR_makeDoc(TopicModelObject* self, PyObject* args, PyObject *k
{
if (!self->inst) throw runtime_error{ "inst is null" };
auto* inst = static_cast<tomoto::IDMRModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand Down
2 changes: 2 additions & 0 deletions src/python/py_LDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static PyObject* LDA_addDoc(TopicModelObject* self, PyObject* args, PyObject *kw
if (!self->inst) throw runtime_error{ "inst is null" };
if (self->isPrepared) throw runtime_error{ "cannot add_doc() after train()" };
auto* inst = static_cast<tomoto::ILDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand Down Expand Up @@ -70,6 +71,7 @@ static PyObject* LDA_makeDoc(TopicModelObject* self, PyObject* args, PyObject *k
{
if (!self->inst) throw runtime_error{ "inst is null" };
auto* inst = static_cast<tomoto::ILDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand Down
8 changes: 6 additions & 2 deletions src/python/py_LLDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static PyObject* LLDA_addDoc(TopicModelObject* self, PyObject* args, PyObject *k
if (!self->inst) throw runtime_error{ "inst is null" };
if (self->isPrepared) throw runtime_error{ "cannot add_doc() after train()" };
auto* inst = static_cast<tomoto::ILLDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand All @@ -48,9 +49,10 @@ static PyObject* LLDA_addDoc(TopicModelObject* self, PyObject* args, PyObject *k
vector<string> labels;
if(argLabels)
{
if (PyUnicode_Check(argLabels)) PRINT_WARN("[warn] 'labels' should be an iterable of str.");
if (!(iter2 = PyObject_GetIter(argLabels)))
{
throw runtime_error{ "words must be an iterable of str." };
throw runtime_error{ "'labels' must be an iterable of str." };
}
py::AutoReleaser arIter2{ iter2 };
labels = py::makeIterToVector<string>(iter2);
Expand Down Expand Up @@ -78,6 +80,7 @@ static PyObject* LLDA_makeDoc(TopicModelObject* self, PyObject* args, PyObject *
{
if (!self->inst) throw runtime_error{ "inst is null" };
auto* inst = static_cast<tomoto::ILLDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand All @@ -86,9 +89,10 @@ static PyObject* LLDA_makeDoc(TopicModelObject* self, PyObject* args, PyObject *
vector<string> labels;
if (argLabels)
{
if (PyUnicode_Check(argLabels)) PRINT_WARN("[warn] 'labels' should be an iterable of str.");
if (!(iter2 = PyObject_GetIter(argLabels)))
{
throw runtime_error{ "words must be an iterable of str." };
throw runtime_error{ "'labels' must be an iterable of str." };
}
py::AutoReleaser arIter2{ iter2 };
labels = py::makeIterToVector<string>(iter2);
Expand Down
2 changes: 2 additions & 0 deletions src/python/py_MGLDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static PyObject* MGLDA_addDoc(TopicModelObject* self, PyObject* args, PyObject *
if (!self->inst) throw runtime_error{ "inst is null" };
if (self->isPrepared) throw runtime_error{ "cannot add_doc() after train()" };
auto* inst = static_cast<tomoto::IMGLDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand Down Expand Up @@ -73,6 +74,7 @@ static PyObject* MGLDA_makeDoc(TopicModelObject* self, PyObject* args, PyObject
{
if (!self->inst) throw runtime_error{ "inst is null" };
auto* inst = static_cast<tomoto::IMGLDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand Down
10 changes: 7 additions & 3 deletions src/python/py_PLDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static int PLDA_init(TopicModelObject *self, PyObject *args, PyObject *kwargs)
float alpha = 0.1, eta = 0.01, sigma = 1, alphaEpsilon = 1e-10;
size_t seed = random_device{}();
static const char* kwlist[] = { "tw", "min_cf", "rm_top", "latent_topics", "topics_per_label", "alpha", "eta", "seed", nullptr };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|nnnnffffn", (char**)kwlist, &tw, &minCnt, &rmTop,
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|nnnnnffn", (char**)kwlist, &tw, &minCnt, &rmTop,
&numLatentTopics, &numTopicsPerLabel, &alpha, &eta, &seed)) return -1;
try
{
Expand Down Expand Up @@ -41,6 +41,7 @@ static PyObject* PLDA_addDoc(TopicModelObject* self, PyObject* args, PyObject *k
if (!self->inst) throw runtime_error{ "inst is null" };
if (self->isPrepared) throw runtime_error{ "cannot add_doc() after train()" };
auto* inst = static_cast<tomoto::IPLDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand All @@ -49,9 +50,10 @@ static PyObject* PLDA_addDoc(TopicModelObject* self, PyObject* args, PyObject *k
vector<string> labels;
if(argLabels)
{
if (PyUnicode_Check(argLabels)) PRINT_WARN("[warn] 'labels' should be an iterable of str.");
if (!(iter2 = PyObject_GetIter(argLabels)))
{
throw runtime_error{ "words must be an iterable of str." };
throw runtime_error{ "'labels' must be an iterable of str." };
}
py::AutoReleaser arIter2{ iter2 };
labels = py::makeIterToVector<string>(iter2);
Expand Down Expand Up @@ -79,6 +81,7 @@ static PyObject* PLDA_makeDoc(TopicModelObject* self, PyObject* args, PyObject *
{
if (!self->inst) throw runtime_error{ "inst is null" };
auto* inst = static_cast<tomoto::IPLDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand All @@ -87,9 +90,10 @@ static PyObject* PLDA_makeDoc(TopicModelObject* self, PyObject* args, PyObject *
vector<string> labels;
if (argLabels)
{
if (PyUnicode_Check(argLabels)) PRINT_WARN("[warn] 'labels' should be an iterable of str.");
if (!(iter2 = PyObject_GetIter(argLabels)))
{
throw runtime_error{ "words must be an iterable of str." };
throw runtime_error{ "'labels' must be an iterable of str." };
}
py::AutoReleaser arIter2{ iter2 };
labels = py::makeIterToVector<string>(iter2);
Expand Down
2 changes: 2 additions & 0 deletions src/python/py_SLDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ static PyObject* SLDA_addDoc(TopicModelObject* self, PyObject* args, PyObject *k
if (!self->inst) throw runtime_error{ "inst is null" };
if (self->isPrepared) throw runtime_error{ "cannot add_doc() after train()" };
auto* inst = static_cast<tomoto::ISLDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "'words' must be an iterable of str." };
Expand Down Expand Up @@ -151,6 +152,7 @@ static PyObject* SLDA_makeDoc(TopicModelObject* self, PyObject* args, PyObject *
{
if (!self->inst) throw runtime_error{ "inst is null" };
auto* inst = static_cast<tomoto::ISLDAModel*>(self->inst);
if (PyUnicode_Check(argWords)) PRINT_WARN("[warn] 'words' should be an iterable of str.");
if (!(iter = PyObject_GetIter(argWords)))
{
throw runtime_error{ "words must be an iterable of str." };
Expand Down
5 changes: 4 additions & 1 deletion tomotopy/documentation.kr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tomotopy 란?
* Hierarchical PA (`tomotopy.HPAModel`)
* Correlated Topic Model (`tomotopy.CTModel`)

tomotopy의 가장 최신버전은 0.4.0 입니다.
tomotopy의 가장 최신버전은 0.4.1 입니다.

.. image:: https://badge.fury.io/py/tomotopy.svg

Expand Down Expand Up @@ -239,6 +239,9 @@ tomotopy의 Python3 예제 코드는 https://github.com/bab2min/tomotopy/blob/ma

역사
-------
* 0.4.1 (2019-11-27)
* `tomotopy.PLDAModel` 생성자의 버그를 수정했습니다.

* 0.4.0 (2019-11-18)
* `tomotopy.PLDAModel`와 `tomotopy.HLDAModel` 토픽 모델이 새로 추가되었습니다.

Expand Down
5 changes: 4 additions & 1 deletion tomotopy/documentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The current version of `tomoto` supports several major topic models including
* Hierarchical PA (`tomotopy.HPAModel`)
* Correlated Topic Model (`tomotopy.CTModel`).

The most recent version of tomotopy is 0.4.0.
The most recent version of tomotopy is 0.4.1.

.. image:: https://badge.fury.io/py/tomotopy.svg

Expand Down Expand Up @@ -242,6 +242,9 @@ meaning you can use it for any reasonable purpose and remain in complete ownersh

History
-------
* 0.4.1 (2019-11-27)
* A bug at init function of `tomotopy.PLDAModel` was fixed.

* 0.4.0 (2019-11-18)
* New models including `tomotopy.PLDAModel` and `tomotopy.HLDAModel` were added into the package.

Expand Down

0 comments on commit fffdfd1

Please sign in to comment.