簡體   English   中英

python中使用nltk.book模塊時去除初始文本

[英]Remove the initial text when using the nltk.book module in python

我正在學習 NLP 並弄亂了 nltk 但只是通過在我的程序中導入模塊,每當我運行腳本時我都會得到以下文本:

*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908

我知道這是關於模塊的信息,但現在對我來說沒有必要。 有沒有辦法讓它在我運行腳本時控制台不打印它?

無法通過nltk直接刪除 output 消息(截至 2021 年 8 月)。 如果您查看nltk/book.py的代碼,您會看到負責消息的打印語句寫在頂層(全局范圍)。 首次導入模塊時,將執行頂層的所有代碼(就像腳本一樣)。 因此,第一次從nltk.book導入內容時,這些打印語句將執行。

雖然您無法阻止這些打印語句的執行,但您可以“捕獲”output 並阻止它寫入標准輸出。 我強烈建議不要這樣做,你不希望在導入時將警告/錯誤消息打印到標准輸出的情況結束,但它會被默默地扔掉,你最終會撓頭幾個小時試圖解決一個奇怪的錯誤,如果您看到錯誤消息,這個錯誤本可以避免。 但是,如果您真的想要, 這里有一些方法可以做到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM