HIT-SCIR / pyltp

pyltp: the python extension for LTP
1.53k stars 352 forks source link

[Feature] Support context manager #213

Closed Vimos closed 4 years ago

Vimos commented 4 years ago

"Context managers allow you to allocate and release resources precisely when you want to. "

当前文档使用方法

from pyltp import Segmentor
segmentor = Segmentor()  # 初始化实例
segmentor.load(cws_model_path)  # 加载模型
words = segmentor.segment('元芳你怎么看')  # 分词
print '\t'.join(words)
segmentor.release()  # 释放模型

可否支持Context Manager,可以节省释放模型的过程

from pyltp import Segmentor
with Segmentor() as segmentor: # 初始化实例
    segmentor.load(cws_model_path)  # 加载模型
    words = segmentor.segment('元芳你怎么看')  # 分词
    print('\t'.join(words))