forked from sequitur-g2p/sequitur-g2p
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultigram.cc
65 lines (60 loc) · 2.25 KB
/
Multigram.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* $Id: Multigram.cc 1667 2007-06-02 14:32:35Z max $
*
* Copyright (c) 2004-2005 RWTH Aachen University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 (June
* 1991) as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you will find it at
* http://www.gnu.org/licenses/gpl.html, or write to the Free Software
* Foundation, Inc., 51 Franlin Street, Fifth Floor, Boston, MA 02110,
* USA.
*
* Should a provision of no. 9 and 10 of the GNU General Public License
* be invalid or become invalid, a valid provision is deemed to have been
* agreed upon which comes closest to what the parties intended
* commercially. In any case guarantee/warranty shall be limited to gross
* negligent actions or intended actions or fraudulent concealment.
*/
#include "Python.hh"
#include "Assertions.hh"
#include "Multigram.hh"
PyObject *Multigram::asPyObject() const {
u32 len = length();
PyObject *result = PyTuple_New(len);
for (u32 i = 0; i < len; ++i)
PyTuple_SET_ITEM(result, i, PyInt_FromLong(data_[i]));
return result;
}
Multigram::Multigram(PyObject *obj) {
memset(data_, 0, sizeof(data_));
PyObject *seq = PySequence_Fast(obj, "need a sequence to create a multigram");
if (!seq) throw ExistingPythonException();
int len = PySequence_Fast_GET_SIZE(seq);
if (len > (int)maximumLength) {
Py_DECREF(seq);
throw PythonException(PyExc_ValueError, "sequence too long");
}
for (int i = 0; i < len; ++i) {
PyObject *item = PySequence_Fast_GET_ITEM(seq, i);
if (!PyInt_Check(item)) {
Py_DECREF(seq);
throw PythonException(PyExc_TypeError, "not an integer");
}
long value = PyInt_AsLong(item);
if (value < 0 || value > Core::Type<Symbol>::max) {
Py_DECREF(seq);
throw PythonException(PyExc_ValueError, "symbol out of range");
}
data_[i] = Symbol(value);
}
Py_DECREF(seq);
}