1 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 # 3 # Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved. 4 # 5 # The contents of this file are subject to the terms of either the GNU Lesser 6 # General Public License Version 2.1 only ("LGPL") or the Common Development and 7 # Distribution License ("CDDL")(collectively, the "License"). You may not use this 8 # file except in compliance with the License. You can obtain a copy of the CDDL at 9 # http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at 10 # http://www.opensource.org/licenses/lgpl-license.php. See the License for the 11 # specific language governing permissions and limitations under the License. When 12 # distributing the software, include this License Header Notice in each file and 13 # include the full text of the License in the License file as well as the 14 # following notice: 15 # 16 # NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE 17 # (CDDL) 18 # For Covered Software in this distribution, this License shall be governed by the 19 # laws of the State of California (excluding conflict-of-law provisions). 20 # Any litigation relating to this License shall be subject to the jurisdiction of 21 # the Federal Courts of the Northern District of California and the state courts 22 # of the State of California, with venue lying in Santa Clara County, California. 23 # 24 # Contributor(s): 25 # 26 # If you wish your version of this file to be governed by only the CDDL or only 27 # the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to 28 # include this software in this distribution under the [CDDL or LGPL Version 2.1] 29 # license." If you don't indicate a single choice of license, a recipient has the 30 # option to distribute your version of this file under either the CDDL or the LGPL 31 # Version 2.1, or to extend the choice of license to its licensees as provided 32 # above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL 33 # Version 2 license, then the option applies only if the new code is made subject 34 # to such option by the copyright holder. 35 36 cdef extern from "slm.h": 37 38 ctypedef union CThreadSlm_TState "CThreadSlm::TState": 39 void setIdx (unsigned int idx) 40 void setLevel (unsigned int level) 41 unsigned int getLevel() 42 unsigned int getIdx() 43 44 ctypedef struct CThreadSlm "CThreadSlm": 45 bint load(char *filename, bint MMap) 46 bint isUseLogPr() 47 void free() 48 double transferNegLog (CThreadSlm_TState history, unsigned int wid, CThreadSlm_TState result) 49 double transfer (CThreadSlm_TState history, unsigned int wid, CThreadSlm_TState result) 50 CThreadSlm_TState history_state_of(CThreadSlm_TState st) 51 CThreadSlm_TState historify(CThreadSlm_TState st) 52 unsigned int lastWordId(CThreadSlm_TState st) 53 54 CThreadSlm *new_CThreadSlm "new CThreadSlm" () 55 void del_CThreadSlm "delete" (CThreadSlm *slm) 56 57 cdef class SlmState: 58 cdef public int level, idx 59 def __cinit__(self, level=0, idx=0): 60 self.level = level 61 self.idx = idx 62 63 def __richcmp__ (self, other, op): 64 equal = self.level == other.level and self.idx == other.idx 65 if op == 2: return equal 66 elif op == 3: return not equal 67 else: return NotImplemented 68 69 def __hash__ (self): 70 return ((self.level, self.idx)).__hash__() 71 72 def __str__(self): 73 return "[level=%d, idx=%d]" % (self.level, self.idx) 74 75 cdef inline pystate_to_cstate (state, CThreadSlm_TState *st): 76 st.setLevel (state.level) 77 st.setIdx (state.idx) 78 79 cdef class Slm: 80 cdef CThreadSlm *thisptr 81 82 def __cinit__(self): 83 self.thisptr = new_CThreadSlm() 84 85 def __dealloc__(self): 86 del_CThreadSlm (self.thisptr) 87 88 def load (self, fname): 89 return self.thisptr.load(fname, True) 90 91 def is_using_log_pr(self): 92 return self.thisptr.isUseLogPr() 93 94 def free(self): 95 self.thisptr.free() 96 97 def transfer_neglog(self, history, wid): 98 cdef CThreadSlm_TState his, ret 99 pystate_to_cstate (history, &his) 100 pr = self.thisptr.transferNegLog(his, wid, ret) 101 return pr, SlmState(ret.getLevel(), ret.getIdx()) 102 103 def transfer(self, history, wid): 104 cdef CThreadSlm_TState his, ret 105 pystate_to_cstate (history, &his) 106 pr = self.thisptr.transfer(his, wid, ret) 107 return pr, SlmState(ret.getLevel(), ret.getIdx()) 108 109 def history_state_of(self, state): 110 cdef CThreadSlm_TState st, ret 111 pystate_to_cstate (state, &st) 112 ret = self.thisptr.history_state_of(st) 113 return SlmState(ret.getLevel(), ret.getIdx()) 114 115 def historify (self, state): 116 cdef CThreadSlm_TState st 117 pystate_to_cstate (state, &st) 118 self.thisptr.historify(st) 119 state.level = int(st.getLevel()) 120 state.idx = int(st.getIdx()) 121 122 def last_word_id (self, state): 123 cdef CThreadSlm_TState st 124 pystate_to_cstate (state, &st) 125 return self.thisptr.lastWordId(st) 126