有没有办法查看 Jupyter Notebook 中的 NLTK 和 Keras 函数?
Is there a way to look inside the NLTK or Keras functions in Jupiter Notebook?
正在寻找一种方法来 select 一个函数并“打开”它以在 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)
]
正在寻找一种方法来 select 一个函数并“打开”它以在 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)
]