hojichar.filters.tokenization

 1from typing import List
 2
 3from hojichar.core.filter_interface import Filter
 4from hojichar.core.models import Document
 5
 6
 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()
24
25
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
43
44
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
class BlankCharTokenizer(hojichar.core.filter_interface.Filter):
 8class BlankCharTokenizer(Filter):
 9    """
10    Tokenizer の実装例です.
11    ドキュメントを空白文字でトークンに分割します.
12    """
13
14    def apply(self, document: Document) -> Document:
15        tokens = self.tokenize(document.text)
16        document.set_tokens(tokens)
17        return document
18
19    def tokenize(self, text: str) -> List[str]:
20        """
21        >>> BlankCharTokenizer().tokenize("hello world")
22        ['hello', 'world']
23        """
24        return text.split()

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

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

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

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

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

def apply( self, document: hojichar.core.models.Document) -> hojichar.core.models.Document:
54    def apply(self, document: Document) -> Document:
55        tokens = self.tokenize(document.text)
56        document.set_tokens(tokens)
57        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]:
59    def tokenize(self, text: str) -> List[str]:
60        """
61        >>> SentenceTokenizer().tokenize("おはよう。おやすみ。ありがとう。さよなら。")
62        ['おはよう。', 'おやすみ。', 'ありがとう。', 'さよなら。']
63
64        >>> SentenceTokenizer().tokenize("さよなら。また来週")
65        ['さよなら。', 'また来週']
66        """
67        tokens = text.split("。")
68        if len(tokens) > 1:
69            if text.endswith("。"):
70                tokens = [token + "。" for token in tokens[:-1]]
71            else:
72                last = tokens[-1]
73                tokens = [token + "。" for token in tokens]
74                tokens[-1] = last
75
76        return tokens
>>> SentenceTokenizer().tokenize("おはよう。おやすみ。ありがとう。さよなら。")
['おはよう。', 'おやすみ。', 'ありがとう。', 'さよなら。']
>>> SentenceTokenizer().tokenize("さよなら。また来週")
['さよなら。', 'また来週']