对于我的程序,我正在使用来自 Twitter 提要的数据创建倒排索引,但是,在解析并将它们放入 mongodb 时,会出现一些有趣的问题。
通常的条目类型应该是这样的:
{"ax"=>1, "easyjet"=>1, "from"=>2}
然而,当解析一些推文时,它们在数据库中的结果是这样的:
{""=>{""=>{""=>{""=>{""=>{"giants"=>{"dhem"=>1, "giants"=>1, "giantss"=>1}}}}
我有这些行将推文拆分并递增数据库中的值:
def pull_hash_tags(tweet, lang)
hash_tags = tweet.split.find_all { |word| /^#.+/.match word }
t = tweet.gsub(/https?:\/\/[\S]+/,"") # removing urls
t = t.gsub(/#\w+/,"") # removing hash tags
t = t.gsub(/[^0-9a-z ]/i, '') # removing non-alphanumerics and keeping spaces
t = t.gsub(/\r/," ")
t = t.gsub(/\n/," ")
hash_tags.each { |tag| add_to_hash(lang, tag, t) }
end
def add_to_hash(lang, tag, t)
t.gsub(/\W+/, ' ').split.each { |word| @db.collection.update({"_id" => lang}, {"$inc" => {"#{tag}.#{word}" => 1}}, { :upsert => true }) }
end
我正在尝试获取普通单词(仅包含字母数字字符)并且没有双空格,也没有回车符等。
最佳答案
您应该添加 t.strip!
,因为看起来问题可能出在前导/尾随空格上。
关于带有倒排索引的 Ruby & Mongodb 带来了一些有趣的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212710/