python-3.x - 有没有办法查看 Jupiter Notebook 中的 NLTK 或 Keras 函数?
问题描述
寻找一种方法来选择一个函数并“打开”它以在 Jupiter Notebook 中查看其中的代码。在此先感谢新加入的成员。
作为与该问题相关的旁注,仅查看我之前使用的给定函数的描述的方法如下:
import pydoc
pydoc.help(print) # to see the description of the 'print' function
解决方案
利用inspect.getsource
例如,查看内容nltk.word_tokenize
from nltk import word_tokenize
import inspect
lines = inspect.getsource(word_tokenize)
print(lines)
输出:
def word_tokenize(text, language="english", preserve_line=False):
"""
Return a tokenized copy of *text*,
using NLTK's recommended word tokenizer
(currently an improved :class:`.TreebankWordTokenizer`
along with :class:`.PunktSentenceTokenizer`
for the specified language).
:param text: text to split into words
:type text: str
:param language: the model name in the Punkt corpus
:type language: str
:param preserve_line: An option to keep the preserve the sentence and not sentence tokenize it.
:type preserve_line: bool
"""
sentences = [text] if preserve_line else sent_tokenize(text, language)
return [
token for sent in sentences for token in _treebank_word_tokenizer.tokenize(sent)
]
推荐阅读
- java - JooQ中两个条件之间的异或比较
- jquery - 在这种情况下,哪种方法更合适?
- kubernetes - 无法通过 kubectl 从其他主机管理 kubernetes
- python - Jinja 包含“忽略缺失”失败并出现 UndefinedError
- microservices - 如何在 Cro 中插入一些路由检查中间件?
- apache-pig - 猪拉丁语:如何尝试捕捉有关铸造的错误?
- typescript - nestjs 中的 factory-girl 库
- c++ - 如何在内核模式下读取特定内核的程序计数器/指令指针?
- laravel - 在单个事务中访问中间件中的表名
- docker - 在 Docker 容器中将文件夹从一个目录复制到另一个目录