Skip to content

Instantly share code, notes, and snippets.

@min50250
Created September 17, 2023 16:23
Show Gist options
  • Save min50250/2faa1d87f19d440fe1cb4ce75a27b0d6 to your computer and use it in GitHub Desktop.
Save min50250/2faa1d87f19d440fe1cb4ce75a27b0d6 to your computer and use it in GitHub Desktop.
step5
> [!wordcloud]
> ```dataviewjs
>
> let html = ""
> let keywords = dv.current().file.outlinks.values.filter(link => link.display.startsWith(')')).map(link => link.display)
>
> keywords = keywords.filter(p => !p.includes(dv.current().file.name))
>
> let keywordsArr = countDuplicates(keywords)
> let table = valueToLevel(keywordsArr)
>
> for(let j = 0 ; j < table.length ; j++){
> html = html + "<span class=\"" + table[j][2] + " cloud\" >" + table[j][0] + "</span>"
> }
>
> dv.paragraph(html)
>
> function countDuplicates(array) {
> const counts = {};
> const result = [];
> // 중복된 요소의 개수를 세기
> array.forEach((item) => {
> counts[item] = (counts[item] || 0) + 1;
> });
> // 중복이 제거된 2차원 배열 생성
> for (const key in counts) {
> result.push(["[[" + key + "]]", counts[key]]);
> }
> // 중복 개수에 따라 내림차순으로 정렬
> result.sort((a, b) => b[1] - a[1]);
> return result;
> }
>
> // 주어진 배열(arr)의 요소들을 레벨로 분류하는 함수
> // 각 요소는 [이름, 값] 형태를 가짐
> function valueToLevel(arr) {
> arr = shuffleArray(arr)
> const values = arr.map((item) => item[1]);
> const minValue = Math.min(...values);
> const maxValue = Math.max(...values);
> const totalLevels = 7;
> const levelSize = (maxValue - minValue) / totalLevels;
>
> return arr.map((item) => {
> const name = item[0];
> const value = item[1];
> const level = Math.ceil((value - minValue) / levelSize);
> const adjustedLevel = Math.min(totalLevels, Math.max(1, level));
> return [name, value, `cloud${adjustedLevel}`];
> });
> }
>
> // 주어진 배열(array)을 랜덤하게 섞는 함수
> function shuffleArray(array) {
> for (let i = array.length - 1; i > 0; i--) {
> const j = Math.floor(Math.random() * (i + 1));
> [array[i], array[j]] = [array[j], array[i]];
> }
> return array;
> }
> ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment