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