Skip to content

Commit

Permalink
[fix] Use relative import and fix docstrings
Browse files Browse the repository at this point in the history
- Change module import to relative import
- Fix and add some docstrings
  • Loading branch information
m-yoshinaka committed Oct 14, 2020
1 parent 5b0a3e3 commit 0fe718c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
6 changes: 3 additions & 3 deletions sapphire/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sapphire.sapphire import Sapphire
from sapphire.word_alignment import (
from .sapphire import Sapphire
from .word_alignment import (
WordEmbedding, FastTextVectorize, WordAlign, get_similarity_matrix
)
from sapphire.phrase_alignment import PhraseExtract, PhraseAlign
from .phrase_alignment import PhraseExtract, PhraseAlign
19 changes: 13 additions & 6 deletions sapphire/phrase_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ def set_params(self, delta, alpha):
def _no_additional_point(ss, se, ts, te, matrix):
"""
Check if there are any more adjacent points to be added.
ss: the index of the start of source phrase
se: the index of the end of source phrase
ts: the index of the start of target phrase
te: the index of the end of target phrase
Parameters
----------
ss : int
the index of the start of source phrase
se : int
the index of the end of source phrase
ts : int
the index of the start of target phrase
te : int
the index of the end of target phrase
"""
len_src, len_trg = matrix.shape

Expand Down Expand Up @@ -123,8 +130,8 @@ def search_for_lattice(phrase_pairs, len_src: int, len_trg: int):
len_src, len_trg : int
Length of sentence.
Return
------
Returns
-------
list
List of tuples consisting of indexes of phrase pairs
= one of the phrase alignments
Expand Down
19 changes: 16 additions & 3 deletions sapphire/sapphire.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sapphire.word_alignment import (
from .word_alignment import (
FastTextVectorize, WordAlign, get_similarity_matrix
)
from sapphire.phrase_alignment import PhraseExtract, PhraseAlign
from .phrase_alignment import PhraseExtract, PhraseAlign


class Sapphire(object):
Expand All @@ -26,7 +26,6 @@ class Sapphire(object):
Set hyper-parameters of SAPPHIRE.
align(tokens_src, tokens_trg)
Get word alignment and phrase alignment.
"""

def __init__(self, model):
Expand All @@ -45,6 +44,7 @@ def __call__(self, tokens_src, tokens_trg):
def set_params(self, lambda_=0.6, delta=0.6, alpha=0.01, hungarian=False):
"""
Set hyper-parameters of SAPPHIRE.
Details are discussed in the following paper:
https://www.aclweb.org/anthology/2020.lrec-1.847/ .
Expand All @@ -67,6 +67,19 @@ def set_params(self, lambda_=0.6, delta=0.6, alpha=0.01, hungarian=False):
self.extractor.set_params(self.delta, self.alpha)

def align(self, tokens_src: list, tokens_trg: list):
"""
Align phrase pairs in two sentences.
Parameters
----------
tokens_src, tokens_trg : list
A tokenized sentence represented by a list of words.
Returns
-------
tuple
(word_alignment, phrase_alignment)
"""
len_src = len(tokens_src)
len_trg = len(tokens_trg)

Expand Down

0 comments on commit 0fe718c

Please sign in to comment.