【发布时间】:2014-10-02 14:06:36
【问题描述】:
我正在尝试从存储在光盘上的 HTML 文档中创建一个单词列表。当我尝试拆分单词并将它们添加到我的单词向量中时,我最终会弄得一团糟。
def get_word_vector(self):
line = self.soup.get_text()
re.sub("s/(\\u[a-e0-9][a-e0-9][a-e0-9]//|\\n)","",line)
for word in line.split("\s+"):
for the_word in word.split("[,.\"\\/?!@#$%^&*\{\}\[\]]+"):
if the_word not in self.word_vector:
self.word_vector[the_word]=0
self.word_vector[the_word]+=1
self.doc_length=self.doc_length+1
for keys in self.word_vector:
print "%r: %r" % (keys, self.word_vector[keys]) #So I can see whats happening
在我得到的 wiki 页面上进行测试时(小样本):
"Horse Markings"\n"Horse and Pony Head Markings"\n"Horse and Pony Leg Markings"\n"Identifying Horse parts and markings," Adapted From: Horses For Dummies, 2nd Edition.\n\n\n\n\n\n\n[hide]
作为一个单一的“词”。该文档正在被 BS4 读取,例如:
self.soup = BeautifulSoup(open(fullpath,"r"))
我不明白为什么会这样。我猜正则表达式失败是因为它错了???
【问题讨论】:
-
所以,基本上,您需要获取网页的单词列表,对吧?
标签: python regex python-2.7 beautifulsoup