hojichar.filters.tokenization

 1from hojichar.core.filter_interface import Filter
 2from hojichar.core.models import Document
 3
 4
 5class BlankCharTokenizer(Filter):
 6    """
 7    Tokenizer の実装例です.
 8    ドキュメントを空白文字でトークンに分割します.
 9    """
10
11    def apply(self, document: Document) -> Document:
12        tokens = self.tokenize(document.text)
13        document.set_tokens(tokens)
14        return document
15
16    def tokenize(self, text: str) -> list[str]:
17        """
18        >>> BlankCharTokenizer().tokenize("hello world")
19        ['hello', 'world']
20        """
21        return text.split()
22
23
24class MergeTokens(Filter):
25    """
26    Merger の実装例です.
27    破棄されていないトークンを結合し, Document を更新します.
28    """
29
30    def merge(self, tokens: list[str]) -> str:
31        """
32        >>> MergeTokens().merge(["hoo", "bar"])
33        'hoobar'
34        """
35        return "".join(tokens)
36
37    def apply(self, document: Document) -> Document:
38        remained_tokens = [token.text for token in document.tokens if not token.is_rejected]
39        document.text = self.merge(remained_tokens)
40        return document
41
42
43class SentenceTokenizer(Filter):
44    """
45    日本語を想定したセンテンス単位のトーカナイザです.
46    句点`。`で文章を区切ります. これだけでは実際の日本語テキストで不十分な例が多くある
47    (句点にピリオドが用いられる, 会話のカギカッコ内で句点が用いられるなど)ため,
48    将来的には適切なセンテンス単位のトーカナイザに置き換えられるべきです.
49    """
50
51    def apply(self, document: Document) -> Document:
52        tokens = self.tokenize(document.text)
53        document.set_tokens(tokens)
54        return document
55
56    def tokenize(self, text: str) -> list[str]:
57        """
58        >>> SentenceTokenizer().tokenize("おはよう。おやすみ。ありがとう。さよなら。")
59        ['おはよう。', 'おやすみ。', 'ありがとう。', 'さよなら。']
60
61        >>> SentenceTokenizer().tokenize("さよなら。また来週")
62        ['さよなら。', 'また来週']
63        """
64        tokens = text.split("。")
65        if len(tokens) > 1:
66            if text.endswith("。"):
67                tokens = [token + "。" for token in tokens[:-1]]
68            else:
69                last = tokens[-1]
70                tokens = [token + "。" for token in tokens]
71                tokens[-1] = last
72
73        return tokens
class BlankCharTokenizer(hojichar.core.filter_interface.Filter):
 7class BlankCharTokenizer(Filter):
 8    """
 9    Tokenizer の実装例です.
10    ドキュメントを空白文字でトークンに分割します.
11    """
12
13    def apply(self, document: Document) -> Document:
14        tokens = self.tokenize(document.text)
15        document.set_tokens(tokens)
16        return document
17
18    def tokenize(self, text: str) -> list[str]:
19        """
20        >>> BlankCharTokenizer().tokenize("hello world")
21        ['hello', 'world']
22        """
23        return text.split()

Tokenizer の実装例です. ドキュメントを空白文字でトークンに分割します.

def apply( self, document: hojichar.core.models.Document) -> hojichar.core.models.Document:
13    def apply(self, document: Document) -> Document:
14        tokens = self.tokenize(document.text)
15        document.set_tokens(tokens)
16        return document

Definition of filter behavior.

The document must have a protocol TextContent, and mostly used hojichar.Document class.

In this method, the filter will modify document.text or document.extras and set document.is_rejected = True to discard the document.

Parameters

document : Document Input document

Returns

Document Processed Document

def tokenize(self, text: str) -> list[str]:
18    def tokenize(self, text: str) -> list[str]:
19        """
20        >>> BlankCharTokenizer().tokenize("hello world")
21        ['hello', 'world']
22        """
23        return text.split()
>>> BlankCharTokenizer().tokenize("hello world")
['hello', 'world']
class MergeTokens(hojichar.core.filter_interface.Filter):
26class MergeTokens(Filter):
27    """
28    Merger の実装例です.
29    破棄されていないトークンを結合し, Document を更新します.
30    """
31
32    def merge(self, tokens: list[str]) -> str:
33        """
34        >>> MergeTokens().merge(["hoo", "bar"])
35        'hoobar'
36        """
37        return "".join(tokens)
38
39    def apply(self, document: Document) -> Document:
40        remained_tokens = [token.text for token in document.tokens if not token.is_rejected]
41        document.text = self.merge(remained_tokens)
42        return document

Merger の実装例です. 破棄されていないトークンを結合し, Document を更新します.

def merge(self, tokens: list[str]) -> str:
32    def merge(self, tokens: list[str]) -> str:
33        """
34        >>> MergeTokens().merge(["hoo", "bar"])
35        'hoobar'
36        """
37        return "".join(tokens)
>>> MergeTokens().merge(["hoo", "bar"])
'hoobar'
def apply( self, document: hojichar.core.models.Document) -> hojichar.core.models.Document:
39    def apply(self, document: Document) -> Document:
40        remained_tokens = [token.text for token in document.tokens if not token.is_rejected]
41        document.text = self.merge(remained_tokens)
42        return document

Definition of filter behavior.

The document must have a protocol TextContent, and mostly used hojichar.Document class.

In this method, the filter will modify document.text or document.extras and set document.is_rejected = True to discard the document.

Parameters

document : Document Input document

Returns

Document Processed Document

class SentenceTokenizer(hojichar.core.filter_interface.Filter):
45class SentenceTokenizer(Filter):
46    """
47    日本語を想定したセンテンス単位のトーカナイザです.
48    句点`。`で文章を区切ります. これだけでは実際の日本語テキストで不十分な例が多くある
49    (句点にピリオドが用いられる, 会話のカギカッコ内で句点が用いられるなど)ため,
50    将来的には適切なセンテンス単位のトーカナイザに置き換えられるべきです.
51    """
52
53    def apply(self, document: Document) -> Document:
54        tokens = self.tokenize(document.text)
55        document.set_tokens(tokens)
56        return document
57
58    def tokenize(self, text: str) -> list[str]:
59        """
60        >>> SentenceTokenizer().tokenize("おはよう。おやすみ。ありがとう。さよなら。")
61        ['おはよう。', 'おやすみ。', 'ありがとう。', 'さよなら。']
62
63        >>> SentenceTokenizer().tokenize("さよなら。また来週")
64        ['さよなら。', 'また来週']
65        """
66        tokens = text.split("。")
67        if len(tokens) > 1:
68            if text.endswith("。"):
69                tokens = [token + "。" for token in tokens[:-1]]
70            else:
71                last = tokens[-1]
72                tokens = [token + "。" for token in tokens]
73                tokens[-1] = last
74
75        return tokens

日本語を想定したセンテンス単位のトーカナイザです. 句点。で文章を区切ります. これだけでは実際の日本語テキストで不十分な例が多くある (句点にピリオドが用いられる, 会話のカギカッコ内で句点が用いられるなど)ため, 将来的には適切なセンテンス単位のトーカナイザに置き換えられるべきです.

def apply( self, document: hojichar.core.models.Document) -> hojichar.core.models.Document:
53    def apply(self, document: Document) -> Document:
54        tokens = self.tokenize(document.text)
55        document.set_tokens(tokens)
56        return document

Definition of filter behavior.

The document must have a protocol TextContent, and mostly used hojichar.Document class.

In this method, the filter will modify document.text or document.extras and set document.is_rejected = True to discard the document.

Parameters

document : Document Input document

Returns

Document Processed Document

def tokenize(self, text: str) -> list[str]:
58    def tokenize(self, text: str) -> list[str]:
59        """
60        >>> SentenceTokenizer().tokenize("おはよう。おやすみ。ありがとう。さよなら。")
61        ['おはよう。', 'おやすみ。', 'ありがとう。', 'さよなら。']
62
63        >>> SentenceTokenizer().tokenize("さよなら。また来週")
64        ['さよなら。', 'また来週']
65        """
66        tokens = text.split("。")
67        if len(tokens) > 1:
68            if text.endswith("。"):
69                tokens = [token + "。" for token in tokens[:-1]]
70            else:
71                last = tokens[-1]
72                tokens = [token + "。" for token in tokens]
73                tokens[-1] = last
74
75        return tokens
>>> SentenceTokenizer().tokenize("おはよう。おやすみ。ありがとう。さよなら。")
['おはよう。', 'おやすみ。', 'ありがとう。', 'さよなら。']
>>> SentenceTokenizer().tokenize("さよなら。また来週")
['さよなら。', 'また来週']