-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.gs
200 lines (181 loc) · 5.65 KB
/
main.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
};
/**
* @OnlyCurrentDoc
*
* The above comment directs Apps Script to limit the scope of file
* access for this add-on. It specifies that this add-on will only
* attempt to read or modify the files in which the add-on is used,
* and not all of the user's files. The authorization request message
* presented to users will reflect this limited scope.
*/
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onOpen trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode.
*/
function onOpen(e) {
DocumentApp.getUi()
.createMenu("Semantically")
.addItem("Launch Semantically Workspace", "showSidebar")
.addToUi();
init();
}
/**
* Runs when the add-on is installed.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onInstall trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode. (In practice, onInstall triggers always
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or
* AuthMode.NONE.)
*/
function onInstall(e) {
onOpen(e);
}
function init() {
if (
PropertiesService.getScriptProperties().getProperty(
"automaticHighlighting"
) == null
) {
PropertiesService.getScriptProperties().setProperty(
"automaticHighlighting",
"false"
);
}
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*/
function showSidebar() {
var ui =
HtmlService.createHtmlOutputFromFile("sidebar").setTitle("Semantically");
DocumentApp.getUi().showSidebar(ui);
}
function getAnnotations(enableHighlighting) {
if (enableHighlighting === true) {
PropertiesService.getScriptProperties().setProperty(
"highlightingEnabled",
true
);
}
annotations = [];
// Getting Annotations
var url = "https://data.bioontology.org/annotator";
var result = JSON.parse(
UrlFetchApp.fetch(url, {
muteHttpExceptions: true,
method: "POST",
payload: {
text: DocumentApp.getActiveDocument().getBody().getText(),
include: "prefLabel,definition",
display_context: "false",
apikey: "89f4c54e-aee8-4af5-95b6-dd7c608f057f",
},
})
);
for (i = 0; i < result.length; i++) {
annotations.push({
id: result[i]["annotatedClass"]["@id"],
prefLabel: result[i]["annotatedClass"]["prefLabel"].capitalize(),
definition:
typeof result[i]["annotatedClass"]["definition"] !== "undefined"
? result[i]["annotatedClass"]["definition"][0]
.split(/<p>|<\/p>/)
.join("")
: "",
ontology: result[i]["annotatedClass"]["links"]["ontology"].replace(
/http[s]?:\/\/data.bioontology.org\/ontologies\//g,
""
),
link: result[i]["annotatedClass"]["links"]["self"],
annotations: result[i]["annotations"],
});
}
return JSON.stringify(annotations);
}
function getRecommenderAnnotations() {
// Getting Recommender Annotations
var url = "https://data.bioontology.org/recommender";
var result = UrlFetchApp.fetch(url, {
muteHttpExceptions: true,
method: "POST",
payload: {
input: DocumentApp.getActiveDocument().getBody().getText(),
display_context: "false",
display_links: "false",
apikey: "89f4c54e-aee8-4af5-95b6-dd7c608f057f",
},
}).getContentText();
Logger.log(result);
var ontologyId = /"ontologies":\[[\S ]*?"acronym":"([\S ]*?)"/g.exec(
result
)[1];
recommenderAnnotations = JSON.parse(
/"annotations":(\[[\S ]*?\])/g.exec(result)[1]
);
for (i = 0; i < recommenderAnnotations.length; i++) {
recommenderAnnotations[i].ontology = ontologyId;
}
highlightAnnotations();
return JSON.stringify(recommenderAnnotations);
}
function getCurrentPosition() {
var offset = DocumentApp.getActiveDocument()
.getBody()
.editAsText()
.getText()
.indexOf(
DocumentApp.getActiveDocument()
.getCursor()
.getElement()
.asText()
.getText()
);
var offsetPosition = DocumentApp.getActiveDocument()
.getCursor()
.getSurroundingTextOffset();
return offset == 0 &&
DocumentApp.getActiveDocument().getCursor().getElement().asText().getText()
.length == 0
? -1
: offset + offsetPosition;
}
function highlightAnnotations() {
//Logger.log(annotations.length);
if (
PropertiesService.getScriptProperties().getProperty("highlightingEnabled")
) {
Logger.log(recommenderAnnotations.length);
var text = DocumentApp.getActiveDocument().getBody().editAsText();
text.setBackgroundColor(0, text.getText().length - 1, null);
for (i = 0; i < recommenderAnnotations.length; i++) {
text.setBackgroundColor(
recommenderAnnotations[i]["from"] - 1,
recommenderAnnotations[i]["to"] - 1,
"#FCFC00"
);
}
}
}
function unhighlightAnnotations() {
PropertiesService.getScriptProperties().setProperty(
"highlightingEnabled",
false
);
var text = DocumentApp.getActiveDocument().getBody().editAsText();
text.setBackgroundColor(0, text.getText().length - 1, null);
}
function getHtml() {
return DocumentApp.getActiveDocument().getBody().getText();
}