活用類別

Python 的軟體工程原則

Adam Spannbauer

Machine Learning Engineer at Eastman

擴充 Document 類別

class Document:
    def __init__(self, text):
        self.text = text

斷詞文件

Python 的軟體工程原則

目前的 Document 類別

class Document:
    def __init__(self, text):
        self.text = text

init 中的斷詞

Python 的軟體工程原則

調整 __init__

class Document:
    def __init__(self, text):
        self.text = text
        self.tokens = self._tokenize()

doc = Document('test doc') print(doc.tokens)
['test', 'doc']
Python 的軟體工程原則

加入 _tokenize() 方法

# Import function to perform tokenization
from .token_utils import tokenize

class Document: def __init__(self, text, token_regex=r'[a-zA-Z]+'): self.text = text self.tokens = self._tokenize()
def _tokenize(self): return tokenize(self.text)
Python 的軟體工程原則

非公開方法

init 中的斷詞

非公開財產標誌

Python 的軟體工程原則

非公開方法的風險

  • 文件不足

  • 行為難以預期

非公開財產標誌

Python 的軟體工程原則

一起來練習吧!

Python 的軟體工程原則

Preparing Video For Download...