【发布时间】:2015-03-04 04:24:45
【问题描述】:
我有一组语音文件,我需要与一组停用词进行比较以删除停用词并留下剩余的有意义的单词。
到目前为止,我有这样的事情:
stopwords = File.readlines('PATH TO TXT FILE')
speeches = []
Dir.glob('PATH TO ALL SPEECHES').each do |speech|
#code to read each speech and store into an array
f = File.readlines(speech)
speeches << f
end
lincolnSpeech = speeches[0]
def process_file(file_name)
all_words = file_name.scan(/\w+/)
meaningful_words = all_words.select { |word| !stopwords.include?(word) }
return meaningful_words
end
我将此函数的结果嵌入到我的 HTML 中,如下所示:
<ul>
<li><pre style="white-space: pre-wrap;word-wrap: break-word">#{process_file(lincolnSpeech)}</pre></li>
</ul>
但这会破坏页面并导致我的 HTML 完全消失。我已将问题缩小到函数中的行:
meaningful_words = all_words.select { |word| !stopwords.include?(word) }
而这条线是罪魁祸首。我不确定为什么它会破坏我的代码。也许其中的一部分已被弃用?谁能提供一些关于为什么这不起作用的想法,以及实现我想要的效果的替代方法?
【问题讨论】:
标签: ruby nlp stop-words