How to generate link-lists with link frequency counter..?

I want to be able to generate lists, which includes the number of links within the currentTiddler to listed tiddler titles. Like this:

!!Hasse Alfredsson
Hasse Alfredsson (1931-2017) was a Swedish comedian, writer, and actor. Together with his co-writer, the poet [[Tage Danielsson]] he started the company [[Svenska Ord AB]]. He and [[Tage|Tage Danielsson]] made a lot of collaborations with cartoonist [[Per Åhlin]]. Together with [[Per|Per Åhlin]], Hasse and [[Tage|Tage Danielsson]] made several movies such as [[Att angöra en brygga]], [[Äppelkriget]], and [[Picassos äventyr]], many of which today are considered cult classics. Several other famous swedish actors were employed to their movies, such as [[Gösta Ekman]], [[Max von Sydow]], and [[Monica Zetterlund]]. After the tragic death of [[Danielsson|Tage Danielsson]] in 1985 Hassse went on to work with, among others, the singer [[Povel Ramel]]…

Link frequency:

  • [[Att angöra en brygga]] - 1
  • [[Gösta Ekman]] - 1
  • [[Max von Sydow]] - 1
  • [[Monica Zetterlund]] - 1
  • [[Per Åhlin]] - 2
  • [[Picassos Äventyr]] - 1
  • [[Povel Ramel]] - 1
  • [[Svenska Ord AB]] - 1
  • [[Tage Danielsson]] - 4
  • [[Äppelkriget]] - 1

The list should look something like this :arrow_up:

I’m still quite new to the TW-lingo, and the associated languages - by which I mean I unfortunately really don’t know where to start looking or what keywords to search for. I’m also quite new to coding in general, but I aim to learn and so far have been able to solve any issue I stumble upon. But this one is just way out of my league. My greatest guess so far is that this has something to do with keeping and counting any duplicates in the generated list, but I don’t know how to achieve that nor if I’m even on the right path.

A link frequency counter like this would be quite useful in a recommendation system on what to read next or just for network analysis. If the list could be sorted by descending frequency value that would be a great bonus!

To create a list as you show it, you can use this code. The problem is links are only counted once. Currently there is no easy way to show the number of links to the same target in one tiddler.

<ul>
<$list filter="[<currentTiddler>links[]]" >
<li><$link/></li>
</$list>
</ul>

To see the links in a tiddler we use the links operator. All the possible operators are listed at Filter Operators

For more info see the list-widget and the link-widget

That is deduplicated, though. The OP was looking to include counts of duplicates.

As far as I can tell, there is no way to configure the links operator to skip the deduplication.

1 Like

Welcome to talk.tiddlywiki.org, @SofiaHarfagri!

I don’t know at the moment how to entirely solve this. But at one point I was working on something similar and I went as far as creating in JS a filter operator that lists all links without the deduplication of the links operator. That gives me something like this:

We would still need to figure out how to group these links into the style list you are looking for, but it should be possible atop this.

You can test this by downloading the following, dragging the resulting file to your wiki, saving, and reloading. Then the new Hasse Alfredsson - Demo should show you how the template ( 1. $:/core/modules/filters/alllinks.js) I created is applied to every tiddler with tag Person (using the template Person Links Tempate):

AllLinksDemo.json (2.4 KB)

I’ll try to think later about how to further group these results.


You might also be interested in a working, if only partially realized, plugin I created to track nearby tiddlers: Nearby (discussion at Nearby Neighbors Plugin: POC release. It sounds like it at least overlaps with your goal.

3 Likes

Welcome to TiddlyWiki and thanks for the Hasse och Tage flashback.

Building on what @pmario wrote, I think this will do what you want. Probably could be more elegant and you would want to implement it in a way that would be easier to reuse, e.g. a global procedure.

Add this below your text:

<$let linkEnd=]] >
<ul>

<$list variable=aLink filter="[<currentTiddler>links[]sort[]]" >

<li><$link to=<<aLink>> ></$link> - <$let myLink= {{{ [<aLink>addsuffix<linkEnd>] }}} > <$text text={{{ [all[current]get[text]split<myLink>count[]subtract[1]] }}} /></$let> </li>

</$list>

</ul>
</$let>

The first $let will allow finding the links using a filter that has to include “]]” without trying to actually put that into the filter explicitly, which won’t work.

The addsuffix combines the text of the link (stored in aLink) with the end marker for a link so that you will only find instances of the actual link, not random occurrences of text that matches a link but isn’t a link. The split in the next filter chops the text into pieces whenever the text stored in myLink is found, so “Per Åhlin]]” for example. The count[] will overcount by one since it is counting the sliced up bits of text not the links, so the last step is to subtract 1. There are probably edge cases where this will be off- ending the text with a link and nothing else, not even a period, might cause undercounting (I realize now that I didn’t check that case).

The sort[] won’t respect Swedish alphabetical order, so Äpplekriget will be put first, as if it starts with A, instead of last in the list where it belongs. I don’t know if there is a fix for that.

2 Likes

Lovely! This was what I was looking for! Thank you! :star:

It does, and it’s a fascinating approach.

If you can reasonably give a tag to exactly those tiddlers where you would want this, I would recommend a ViewTemplate, wrapping the code above in <% if [<currentTiddler>tag[Person]] %> ...<% endif %>, and replacing Person by your tag. Then you don’t need to remember to include this code, or to include a call to a macro or procedure that has this code.