Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Add getters for latticenode attributes (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorami Hisamoto authored Jun 18, 2020
1 parent 6276fa9 commit 3c8df53
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
11 changes: 6 additions & 5 deletions sudachipy/lattice.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ cdef class Lattice:
for r_node in r_nodes:
surface = '(null)'
pos = 'BOS/EOS'
if r_node.is_defined:

if r_node.is_defined():
wi = r_node.get_word_info()
surface = wi.surface
pos_id = wi.pos_id
Expand All @@ -139,12 +140,12 @@ cdef class Lattice:
pos = ','.join(self.grammar.get_part_of_speech_string(pos_id))

costs = []
for l_node in self.end_lists[r_node.begin]:
cost = self.grammar.get_connect_cost(l_node.right_id, r_node.left_id)
for l_node in self.end_lists[r_node.get_begin()]:
cost = self.grammar.get_connect_cost(l_node.get_right_id(), r_node.get_left_id())
costs.append(str(cost))
index += 1

logger.info('%d: %d %d %s(%d) %s %d %d %d: %s' %
(index, r_node.get_begin(), r_node.get_end(),
surface, r_node.word_id, pos, r_node.left_id,
r_node.right_id, r_node.cost, ' '.join(costs)))
surface, r_node.get_word_id(), pos, r_node.get_left_id(),
r_node.get_right_id(), r_node.get_path_cost(), ' '.join(costs)))
2 changes: 1 addition & 1 deletion sudachipy/latticenode.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cdef class LatticeNode:
cdef bint is_connected_to_bos
cdef object extra_word_info
cdef object undefined_word_info
cdef bint is_defined
cdef bint _is_defined
cdef object lexicon
cdef int left_id
cdef int right_id
Expand Down
24 changes: 18 additions & 6 deletions sudachipy/latticenode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ cdef class LatticeNode:
self.is_connected_to_bos = False
self.extra_word_info = None

self.is_defined = True
self._is_defined = True
if lexicon is left_id is right_id is cost is word_id is None:
self.is_defined = False
self._is_defined = False
return
self.lexicon = lexicon
self.left_id = left_id
Expand Down Expand Up @@ -69,26 +69,38 @@ cdef class LatticeNode:

def set_oov(self):
self._is_oov = True


def is_defined(self):
return self._is_defined

def set_defined(self):
self._is_defined = True

def get_word_info(self) -> WordInfo:
if not self.is_defined:
if not self._is_defined:
return UNK
if self.extra_word_info:
return self.extra_word_info
return self.lexicon.get_word_info(self.word_id)

def set_word_info(self, word_info: WordInfo) -> None:
self.extra_word_info = word_info
self.is_defined = True
self._is_defined = True

def get_path_cost(self) -> int:
return self.cost

def get_left_id(self) -> int:
return self.left_id

def get_right_id(self) -> int:
return self.right_id

def get_word_id(self) -> int:
return self.word_id

def get_dictionary_id(self) -> int:
if not self.is_defined or self.extra_word_info:
if not self._is_defined or self.extra_word_info:
return -1
return self.lexicon.get_dictionary_id(self.word_id) # self.word_id >> 28

Expand Down

0 comments on commit 3c8df53

Please sign in to comment.