I am trying to get a reference to a post given its file name, for example:
If I have a post with name post.md, it is possible with something like .Ref "post.md" get a reference to that page, so I can then use its variables, (.Title, .Permalink) and so on?
You can do this with ref and relref shortcodes, but this is usually used in content.
Yes, but this is more at the templating layer. You can pull file variables as well.
Can you be a bit more specific about what you’re trying to accomplish? Maybe a repo or templating? I don’t have enough right now to answer your question in its entirety…
I’ve build a system that recommends similar posts using kmeans, right now I have a CSV with the following structure:
ID | assigned cluster | post.file.md
So, in my template I want to get the cluster the current post is being assigned to, and show 5 similar posts, so far I have:
{{ $url := "static/labels.csv" }}
{{ $sep := "," }}
{{ $file := string .File.LogicalName }}
{{ range $i, $r := getCSV $sep $url }}
// Store in which cluster the current post is
{{ if in $r (string $file) }}
{{ $.Scratch.Add "cluster" (index . 1) }}
{{ end }}
// Get the cluster for the current post
{{ $cluster := $.Scratch.Get "cluster" }}
// Store all post in the same cluster that the current post
{{ if in $r (string $cluster) }}
{{ $.Scratch.Add "related" (slice (index . 2)) }}
{{ end }}
{{ end }}
// Iterate over all related/ similar posts and print the first 5 (Not working)
{{ $related := $.Scratch.Get "related" }}
{{ range $related }}
{{ if ne . "title"}}
<p>{{ $.Ref . }}</p>
{{ end }}
{{ end }}
With the above code, all I can get is the URI of the post:
I would like to be able to get a reference to each related post in order to build a list of links to each related post, something like this:
{{ $related := $.Scratch.Get "related" }}
{{ range $related }}
{{ if ne . "title"}}
<p>{{ $.Ref . }}</p> // Here I'd like to access to .Title, .Permalink etc of each related post
{{ end }}
{{ end }}
I see what you’re trying to do, but that raises the question of why (seemingly) you are trying to introduce your own content organization. I might use tags or categories for this purpose.
I am using categories and tags, but I was experimenting with document clustering and decided to build a model with kmeans that analyzes the content of each post and group together similar ones, independently of what category and tags they have.
Indeed, I found that the model group some posts that are related in content, but that I have not assigned the same category/tags, for example, a post talking about something related to Android is cluster together with some post about Android books I have in my blog.
Did it!, here is the code, if someone is interested, I’m planning to release this “related/similar” posts on my site in a week:
{{ $url := "static/labels.csv" }}
{{ $sep := "," }}
{{ $file := string .File.LogicalName }}
{{/* First iterate thought csv to get post cluster */}}
{{ range $i, $r := getCSV $sep $url }}
{{ if in $r (string $file) }}
{{ $.Scratch.Set "cluster" (index . 1) }}
{{ end }}
{{ end }}
{{ $cluster := $.Scratch.Get "cluster" }}
{{/* loop csv again to store post in the same cluster */}}
{{ range $i, $r := getCSV $sep $url }}
{{ if in $r $cluster }}
{{ $.Scratch.Add "posts" (slice $r) }}
{{ end }}
{{ end }}
{{ $post := $.Scratch.Get "posts" }}
{{/* Finally, show 5 randomly related posts */}}
{{ range first 5 (shuffle $post) }}
<p><a href="{{$.Ref (index . 2)}}" title="{{ index . 3}}">{{ index . 3 }}</a></p>
{{ end }}