Skip to content

Commit

Permalink
Version 0.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
frianasoa committed Apr 22, 2024
1 parent 98c8be1 commit a77d3fd
Show file tree
Hide file tree
Showing 13 changed files with 854 additions and 15 deletions.
5 changes: 5 additions & 0 deletions content/lib/d3-layout-cloud/d3.js

Large diffs are not rendered by default.

505 changes: 505 additions & 0 deletions content/lib/d3-layout-cloud/d3.layout.cloud.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions content/notes/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ Menus = {
"editannotationcomment": {name: "Edit annotation comments", icon: "fa-edit"},
"showannotation": {name: "Show annotation", icon: "fa-edit"},
"sep": "---------",

/*
"word-cloud": {name: "Show word cloud", icon:"fa-cloud",
items: {
"word-cloud-all": {name: "All"},
"word-cloud-quote": {name: "Quote"},
"word-cloud-comment": {name: "Reader comment"},
"word-cloud-paraphrase": {name: "Paraphrase"},
}
},
*/
"sep-word-cloud": "---------",
}

var items_ai = {}
Expand Down Expand Up @@ -305,6 +317,13 @@ Menus = {
Actions.showannotation(parseInt(attachmentid), parseInt(annotationpage), annotationkey)
}

else if(key=="word-cloud-all")
{
let data = Table.tabledata(td.closest("tr"));
Zotero.ZeNotes.cache["word-cloud-data"] = data;
Zotero.ZeNotes.Ui.opencustomtab("chrome:https://ze-notes/content/wordcloud/wordcloud.xhtml", "Word Cloud", "word-cloud-tab", data);
}

else if(key.includes("translate-"))
{
var annotation = Zotero.Items.get(annotationid);
Expand Down
8 changes: 6 additions & 2 deletions content/notes/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ document.addEventListener("wheel", function(e){

document.addEventListener("keyup", function(e){
if(e.ctrlKey){
let sign = 1;
let sign = 0;
if(e.keyCode==107)
{
sign = 1;
Expand All @@ -395,6 +395,10 @@ document.addEventListener("keyup", function(e){
{
sign=-1;
}
Notes.zoom(sign);

if(sign!=0)
{
Notes.zoom(sign);
}
}
})
27 changes: 22 additions & 5 deletions content/notes/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,28 @@ Table = {
}

td.querySelectorAll(".annotation-body").forEach(div=>{
var annotationcontent = div.querySelector(".direct-quote").innerText;
var author = div.querySelector(".direct-quote").parentNode.dataset.author;
var date = div.querySelector(".direct-quote").parentNode.dataset.date;
var tag = div.querySelector(".direct-quote").parentNode.dataset.tag;
var annotationcomments = Table.splitusernotes(div.querySelector(".annotation-comment").innerHTML);
var annotationcontent = "";
var author = "";
var date = "";
var tag = "";
var directquotediv = div.querySelector(".direct-quote")
var annotationcommentdiv = div.querySelector(".annotation-comment");
var annotationcomments = {};

if(directquotediv!=null)
{
annotationcontent = directquotediv.innerText;
author = directquotediv.parentNode.dataset.author;
date = directquotediv.parentNode.dataset.date;
tag = directquotediv.parentNode.dataset.tag;
}
author = div.parentNode.dataset.author;

if(annotationcommentdiv!=null)
{
annotationcomments = Table.splitusernotes(annotationcommentdiv.innerHTML);
}

s = annotationcomments;
s["Direct quote"] = annotationcontent;
s["Author"] = author;
Expand Down
189 changes: 189 additions & 0 deletions content/wordcloud/wordcloud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
WordCloud = {
init(selector){
this.width = 450;
this.height = 450;
WordCloud.selector = selector;
WordCloud.fill = d3.scale.category20();
WordCloud.svg = d3.select(selector).append("svg")
.attr("width", this.width)
.attr("height", this.height)
.append("g")
.attr("transform", "translate("+this.width/2+","+this.height/2+")");

WordCloud.stopwords = [
"a", "able", "about", "across", "after", "all", "almost", "also", "am", "among", "an",
"and", "any", "are", "as", "at", "be", "because", "been", "but", "by", "can", "cannot",
"could", "did", "do", "does", "either", "else", "ever", "every", "for", "from", "get",
"got", "had", "has", "have", "he", "her", "hers", "him", "his", "how", "however", "i",
"if", "in", "into", "is", "it", "its", "just", "least", "let", "like", "likely", "may",
"me", "might", "most", "must", "my", "neither", "no", "nor", "not", "of", "off", "often",
"on", "only", "or", "other", "our", "own", "rather", "said", "say", "says", "she", "should",
"since", "so", "some", "than", "that", "the", "their", "them", "then", "there", "these",
"they", "this", "tis", "to", "too", "twas", "us", "wants", "was", "we", "were", "what",
"when", "where", "which", "while", "who", "whom", "why", "will", "with", "would", "yet",
"you", "your", "–", "-"
]
},

update(sentence){
var words = WordCloud.tokenize(sentence);
d3.layout.cloud().size([this.width, this.height])
.words(words)
.padding(5)
.rotate(function() { return ~~(Math.random() * 2) * 50; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", WordCloud.draw)
.start();
},

draw(words)
{
var cloud = WordCloud.svg.selectAll("g text").data(words, function(d) { return d.text; })

cloud.enter()
.append("text")
.style("font-family", "Impact")
.style("fill", function(d, i) { return WordCloud.fill(i); })
.attr("text-anchor", "middle")
.attr('font-size', 1)
.text(function(d) { return d.text; });

cloud.transition()
.duration(600)
.style("font-size", function(d) { return d.size + "px"; })
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.style("fill-opacity", 1);

cloud.exit()
.transition()
.duration(200)
.style('fill-opacity', 1e-6)
.attr('font-size', 1)
.remove();
},

weigh(word, list){
var count = 0;
for(w of list)
{
if(w==word)
{
count+=1;
}
}
return count;
},

tokenize(sentences){
var s = sentences.replace(/[!’‘"\.,:;\?\[\]\(\)]/g, ' ').replace(/ /g, " ").split(' ');
s = s.map(function(d){
return {
text: d,
size: 10+WordCloud.weigh(d, s)*5
}
});
return [...new Map(s.map(item =>
[item["text"], item])).values()]
.filter(function(e) {
return this.indexOf(e.text.toUpperCase()) < 0;
},
WordCloud.stopwords.map(function(e){return e.toUpperCase()})
)
.sort(function(a, b){
if ( a.size > b.size ){
return -1;
}
if ( a.size < b.size ){
return 1;
}
return 0;
});
},

extract(table)
{
// return table.map(function(row){
// return row["contents"].map(function(cell){
// return cell["Selections"].map(function(selection){
// return selection["Paraphrase"]
// }).join(" ")
// }).join(" ")
// }).join(" ");
},

row2str(row){
var words = row["contents"].map(function(cell){
if(Object.keys(cell).includes("Selections"))
{
return cell["Selections"].map(function(selection){
return Object.values(selection).join(" ");
}).join(" ")
}
else
{
return "";
}
}).join(" ");
return words
},

row2info(row)
{
var title = row["info"]["title"];
var creators = row["info"]["creators"];
var date = row["info"]["date"];
return "<div style='width:100%; border-bottom: solid 1px;'><b>Title</b>: "+title+"<br/>"+"<b>Authors</b>: "+creators+"<br/><b>Date</b>: "+date+"</div>";
},

showrows(t){

var table = document.createElement("table");
table.style = "border-spacing: 0.5em;";
document.body.appendChild(table);

var tr=null ;
var i = 0;
for(row of t)
{
if(i%3==0)
{
tr = table.insertRow(-1);
}

var td = tr.insertCell(-1);
td.style = "border: solid 1px; width: 33.3333%; vertical-align: top; padding:0.3em;"


var words = this.row2str(row);
var label = this.row2info(row);

var ldiv = document.createElement("div");
ldiv.innerHTML = label;
td.appendChild(ldiv);

if(words.length>0)
{
var div = document.createElement("div");
td.appendChild(div);
div.id = "div"+i;
WordCloud.init("#"+div.id);
WordCloud.update(words);
}
i+=1;
}

}
}

window.addEventListener("load", function() {
// WordCloud.init("body");
var table = Zotero.ZeNotes.cache["word-cloud-data"];
var words = WordCloud.showrows(table);

// var words = WordCloud.extract(data);
// WordCloud.update(words);
// Zotero.ZeNotes.cache["word-cloud-data"] = "";
});
13 changes: 13 additions & 0 deletions content/wordcloud/wordcloud.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http:https://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<script src="../notes/zotero.js"></script>
<script src="../lib/d3-layout-cloud/d3.js"></script>
<script src="../lib/d3-layout-cloud/d3.layout.cloud.js"></script>
<script src="./wordcloud.js"></script>
</head>
<body>

</body>
</html>
Loading

0 comments on commit a77d3fd

Please sign in to comment.