Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 基于Ruby中的标记获取相关文章_Arrays_Ruby_Tags - Fatal编程技术网

Arrays 基于Ruby中的标记获取相关文章

Arrays 基于Ruby中的标记获取相关文章,arrays,ruby,tags,Arrays,Ruby,Tags,我正试图根据文章的标签显示相关部分。应显示具有类似标签的任何文章 这样做的目的是迭代文章的标签,看看是否有其他文章有这些标签。 如果是,则将该文章添加到我以后可以检索的相关=[]文章数组中 文章A:标签:[chris,mark,scott] 第B条:标签:[马克,斯科特] C条:标签:[alex,mike,john] 第A条与第B条相关,反之亦然 代码如下: files = Dir[ROOT + 'articles/*'] # parse file def parse(fn) res

我正试图根据文章的标签显示相关部分。应显示具有类似标签的任何文章

这样做的目的是迭代文章的标签,看看是否有其他文章有这些标签。
如果是,则将该文章添加到我以后可以检索的
相关=[]
文章数组中


文章A:
标签:[chris,mark,scott]

第B条:
标签:[马克,斯科特]

C条:
标签:[alex,mike,john]

第A条与第B条相关,反之亦然


代码如下:

files = Dir[ROOT + 'articles/*']

# parse file
def parse(fn)
  res = meta(fn)
  res[:body] = PandocRuby.new(body(fn), from: 'markdown').to_html
  res[:pagedescription] = res[:description]
  res[:taglist] = []
  if res[:tags]
    res[:tags] = res[:tags].map do |x|
      res[:taglist] << '<a href="/%s">%s</a>' % [x, x]
      '<a href="/%s">%s</a>' % [x, x]
    end.join(', ')
  end
  res
end

# get related articles
def related_articles(articles)
  related = []
    articles[:tags].each do |tag|
      articles.each do |item|
        if item[:tags] != nil && item[:tags].include?(tag)
          related << item unless articles.include?(item)
        end
      end
    end
  related
end

articles = files.map {|fn| parse(fn)}.sort_by {|x| x[:date]}

articles = related_articles(articles)


我试过的另一件事是:

# To generate related articles
def related_articles(articles)
  related = []
  articles.each do |article|
    article[:tags].each do |tag|
      articles.each do |item|
        if item[:tags] != nil && item[:tags].include?(tag)
          related << item unless articles.include?(item)
        end
      end
    end
  end
  related
end

在您的第一次尝试中,问题出现在
文章[:tags]
articles
是一个数组,因此不能使用符号键访问它

第二次尝试失败,因为
article[:tags]
是一个字符串(从
parse
函数中,您获得原始标记,转换为HTML,然后加入)。
:taglist
键包含一个数组,您可以使用它

最后,“相关”数组应该是每篇文章一个数组,这样两个实现都不可能解决您的问题,因为它们都为您的所有文章集返回一个数组

您可能需要两个通行证:

def parse(fn)
  res = meta(fn)
  res[:body] = PandocRuby.new(body(fn), from: 'markdown').to_html
  res[:pagedescription] = res[:description]
  res[:tags] ||= []  # and don't touch it
  res[:tags_as_links] = res[:tags].map { |x| "<a href=\"/#{x}\">#{x}</a>" }
  res[:tags_as_string] = res[:tags_as_links].join(', ')
  res
end

articles = files.map { |fn| parse(fn) }

# convert each article into a hash like
# {tag1 => [self], tag2 => [self]}
# and then reduce by merge
taggings = articles
           .map { |a| a[:tags].product([[a]]).to_h }
           .reduce { |a, b| a.merge(b) { |_, v1, v2| v1 | v2 } }


# now read them back into the articles
articles.each do |article|
  article[:related] = article[:tags]
                      .flat_map { |tag| taggings[tag] }
                      .uniq
  # remove the article itself
  article[:related] -= [article]
end
def解析(fn)
res=元(fn)
res[:body]=PandocRuby.new(body(fn),from:'markdown')。to_html
res[:pagedescription]=res[:description]
res[:tags]| |=[]#,不要触摸它
res[:tags_as_links]=res[:tags].map{
res[:tags_as_string]=res[:tags_as_links]。连接(','))
物件
结束
articles=files.map{| fn | parse(fn)}
#将每个项目转换为类似
#{tag1=>[self],tag2=>[self]}
#然后通过合并来减少
标记=物品
.map{a|a[:标记].product([[a]]).to_h}
减少{a,b{a.merge(b){u,v1,v2{v1}
#现在把它们读回文章
物品。每件物品|
第[:相关]=第[:标签]
.flat|u map{| tag | taggings[tag]}
.uniq
#删除文章本身
第[:相关]-=[文章]
结束

图块中的双方括号是设计的。使用一个方括号将无法得到正确的结果。此外,要合并的块似乎有太多的垂直条,情况并非如此。第三个方法就是返回数组并集的
|
方法。哇!你是个巫师!它可以工作…感谢代码,我永远都不会把它弄对。在
.uniq
之后添加
.map{a | a.slice(:title,:uri)}
重新读取它们。另外,如果使用ruby>=2.7,大多数块在使用未命名的参数时读取效果更好:
.map{u 1.slice(:title,:uri)}
etc-但是你也应该在非常简单的块上使用它们,并且永远不要嵌套它们,例如
.flat\u map{taggings[\u 1]}
a.merge(b){u 2 | u 3}
,但不要在更复杂的情况下或者在其中有另一个块的情况下使用,否则它会变得混乱。
 undefined method `each' for "<a href=\\"/tagname\\">tagname</a>":String (NoMethodError)
  res[:taglist] = []
  if res[:tags]
    res[:tags] = res[:tags].map do |x|
      res[:taglist] << '<a href="/' + x + '">' + x + '</a>'
      '<a href="/' + x + '">' + x + '</a>'
    end.join(', ')
  end
def parse(fn)
  res = meta(fn)
  res[:body] = PandocRuby.new(body(fn), from: 'markdown').to_html
  res[:pagedescription] = res[:description]
  res[:tags] ||= []  # and don't touch it
  res[:tags_as_links] = res[:tags].map { |x| "<a href=\"/#{x}\">#{x}</a>" }
  res[:tags_as_string] = res[:tags_as_links].join(', ')
  res
end

articles = files.map { |fn| parse(fn) }

# convert each article into a hash like
# {tag1 => [self], tag2 => [self]}
# and then reduce by merge
taggings = articles
           .map { |a| a[:tags].product([[a]]).to_h }
           .reduce { |a, b| a.merge(b) { |_, v1, v2| v1 | v2 } }


# now read them back into the articles
articles.each do |article|
  article[:related] = article[:tags]
                      .flat_map { |tag| taggings[tag] }
                      .uniq
  # remove the article itself
  article[:related] -= [article]
end