【发布时间】:2015-06-02 03:15:33
【问题描述】:
我对 Python (2.7) 还很陌生,如果这是一个非常简单直接的问题,请原谅我。我希望 (i) 从已使用 NLTK 库标记的文本中提取所有以 -ing 结尾的单词,以及 (ii) 提取每个提取的单词之前的 10 个单词。然后我希望 (iii) 将这些保存为文件作为两列的数据框,可能看起来像:
Word PreviousContext
starting stood a moment, as if in a troubled reverie; then
seeming of it retraced our steps. But Elijah passed on, without
purchasing a sharp look-out upon the hands: Bildad did all the
我知道如何做 (i),但不确定如何去做 (ii)-(iii)。任何帮助将不胜感激和承认。到目前为止,我有:
>>> import bs4
>>> import nltk
>>> from nltk import word_tokenize
>>> url = "http://www.gutenberg.org/files/766/766-h/766-h.htm"
>>> import urllib
>>> response = urllib.urlopen(url)
>>> raw = response.read().decode('utf8')
>>> tokens = word_tokenize(raw)
>>> for w in tokens:
... if w.endswith("ing"):
... print(w)
...
padding
padding
encoding
having
heading
wearying
dismissing
going
nothing
reading etc etc etc..
【问题讨论】:
-
我刚刚添加了我到目前为止所做的工作以达到 (i)。 :)
-
提示:调查
enumerate