Skip to content

Commit

Permalink
Merge pull request #45 from mbroedl/citations-by-count
Browse files Browse the repository at this point in the history
  • Loading branch information
rmzelle authored Sep 12, 2024
2 parents 436f908 + b7812c9 commit 7171d4d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 79 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Reference extractor allows you to:

*Scenario*: You wish to create a collection for the items you've cited in a manuscript.
* **Count** the number of times each item has been cited.

*Scenario*: You wish to find and remove references cited once or twice to reduce your manuscript’s word count.
* **Identify** the [Citation Style Language](https://citationstyles.org/) citation style used in the document.

## Tips for use
Expand Down
120 changes: 64 additions & 56 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,68 +54,68 @@
References must have been inserted with the Zotero or Mendeley word processor plugins and must not have been converted to plain text.
The tool runs entirely on your own computer, keeping your documents and citations private and secure.</p>
<hr>
<h4 class="text-center">Input</h4>
<br/>
<div class="container">
<div class="col-lg-11 offset-lg-1">
<div class="col-lg-11 text-center">
<h5>Step 1.</h5>
<p>Select your Word (.docx) or LibreOffice (.odt) file</p>
<form autocomplete="off">
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="add_citation_counts_toggle">
<label class="form-check-label" for="add_citation_counts_toggle">Store cite counts</label>
<small id="add_citation_counts_toggle_help" class="form-text text-muted">
Select this option to store the cite count (the number of times each item has been cited in the document) in the "note" field of the CSL JSON output. This field matches the "Extra" field in the Zotero user interface.
</small>
</div>
<div class="form-group">
<input id="file_upload" type="file">
<small id="file_upload_help" class="form-text text-muted">
Select your .docx (Word) or .odt (LibreOffice) file
</small>
</div>
</form>
</div>
</div>
<h4 class="text-center">Results</h4>
<br/>
<div class="container">
<div class="col-lg-11 offset-lg-1">
<form autocomplete="off">
<div class="form-group row" style="margin-bottom:0px;">
<label for="extract_count" class="col-lg-4 col-form-label col-form-label-sm">Number of extracted references</label>
<div class="col-lg-8">
<input type="text" readonly class="form-control-plaintext form-control-sm" id="extract_count" value="0">
</div>
</div>
<div class="form-group row" style="margin-bottom:0px;">
<label for="selected_style" class="col-lg-4 col-form-label col-form-label-sm">CSL style selected in document</label>
<div class="col-lg-8">
<input type="text" readonly class="form-control-plaintext form-control-sm" id="selected_style" value="-">
</div>
</div>
<div class="form-group row" style="margin-bottom:0px;">
<label for="output_format" class="col-lg-4 control-label col-form-label-sm">Output format</label>
<select id="output_format" class="col-lg-4 custom-select custom-select-sm">
<option value="data" selected>CSL JSON</option>
<option value="bibtex">BibTeX</option>
<option value="ris">RIS</option>
<option value="bibliography">Formatted citations (APA style)</option>
</select>
</div>
<div class="form-group row">
<label for="textArea" class="col-lg-4 control-label col-form-label-sm">Extracted references</label>
<div class="col-lg-8" style="padding-left:0px;">
<button id="download" type="button" class="btn btn-success btn-sm" disabled>Download</button>
<button id="copy_to_clipboard" data-clipboard-text="" type="button" class="btn btn-success btn-sm" disabled>Copy to clipboard</button>
<div class="btn-group">
<button id="zotero_item_selection_button" type="button" class="btn btn-outline-success btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" disabled>
Select in Zotero <span class="caret"></span>
</button>
<ul class="dropdown-menu" id="zotero_item_selection_link_list"></ul>
<div class="col-lg-11">
<h5 class="text-center">Step 2.</h5>
<p class="text-center">Save the extracted citations in your preferred format, or select them in your Zotero library.</p>
<div class="container">
<div class="offset-lg-1">
<form autocomplete="off">
<div class="form-group row" style="margin-bottom:0px;">
<label for="extract_count" class="col-lg-4 col-form-label col-form-label-sm">Number of extracted references</label>
<div class="col-lg-8">
<input type="text" readonly class="form-control-plaintext form-control-sm" id="extract_count" value="0">
</div>
</div>
</div>
<div class="col-lg-12" style="padding-left:0px;">
<textarea id="textArea" class="form-control form-control-sm" rows="3" readonly style="margin-top: 10px;"></textarea>
</div>
<div class="form-group row" style="margin-bottom:0px;">
<label for="selected_style" class="col-lg-4 col-form-label col-form-label-sm">CSL style selected in document</label>
<div class="col-lg-8">
<input type="text" readonly class="form-control-plaintext form-control-sm" id="selected_style" value="-">
</div>
</div>
<div class="form-group row" style="margin-bottom:0px;">
<label for="output_format" class="col-lg-4 control-label col-form-label-sm">Output format</label>
<select id="output_format" class="col-lg-4 custom-select custom-select-sm">
<option value="data" selected>CSL JSON</option>
<option value="data-with-counts">CSL JSON (with cite counts)</option>
<option value="bibtex">BibTeX</option>
<option value="ris">RIS</option>
<option value="bibliography">APA-style references</option>
<option value="bibliography-with-counts">APA-style references (with cite counts, tab-separated)</option>
</select>
</div>
<div class="form-group row">
<label for="textArea" class="col-lg-4 control-label col-form-label-sm">Extracted references</label>
<div class="col-lg-8" style="padding-left:0px;">
<button id="download" type="button" class="btn btn-success btn-sm" disabled>Download</button>
<button id="copy_to_clipboard" data-clipboard-text="" type="button" class="btn btn-success btn-sm" disabled>Copy to clipboard</button>
<div class="btn-group">
<button id="zotero_item_selection_button" type="button" class="btn btn-outline-success btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" disabled>
Select in Zotero <span class="caret"></span>
</button>
<ul class="dropdown-menu" id="zotero_item_selection_link_list"></ul>
</div>
</div>
<div class="col-lg-12" style="padding-left:0px;">
<textarea id="textArea" class="form-control form-control-sm" rows="3" readonly style="margin-top: 10px;"></textarea>
</div>
</div>
</form>
</div>
</form>
</div>
</div>
</div>
<hr>
Expand All @@ -124,15 +124,23 @@ <h4>About Reference Extractor</h4>
<ul>
<li>
<p><strong>Extract</strong> Zotero and Mendeley references and save them to CSL JSON, BibTeX, or RIS format, or as a rendered bibliography in APA style.</p>
<p><em>Scenario 1</em>: You lost your Zotero/Mendeley library but still have your documents.
Extraction allows you to recover the items you cited in your documents and import them back into your reference manager.
Note that imported items won’t be linked to the items in the document you extracted them from.</p>
<p><em>Scenario 2</em>: Somebody sent you a document and you would like to get the cited items into your own reference manager library.</p>
<ul>
<li>
<p><em>Scenario 1</em>: You lost your Zotero/Mendeley library but still have your documents.
Extraction allows you to recover the items you cited in your documents and import them back into your reference manager.
Note that imported items won’t be linked to the items in the document you extracted them from.</p>
</li>
<li>
<p><em>Scenario 2</em>: Somebody sent you a document and you would like to get the cited items into your own reference manager library.</p>
</li>
</ul>
</li>
<li>
<p><strong>Select</strong> the original cited items in your existing Zotero libraries <em>[only available for Zotero]</em>.
Once items are selected in Zotero, you can drag the items into a new collection or apply a tag.</p>
<p><em>Scenario</em>: You wish to create a collection for the items you’ve cited in a manuscript.</p>
<ul>
<li><p><em>Scenario</em>: You wish to create a collection for the items you’ve cited in a manuscript.</p></li>
</ul>
</li>
<li>
<p><strong>Count</strong> the number of times each item has been cited.</p>
Expand Down
75 changes: 52 additions & 23 deletions libraries/ref-extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ window.savedZoteroLibrarySelectors = {};

var Cite = require('citation-js');

var citationCountCheckboxElement = document.getElementById("add_citation_counts_toggle");
citationCountCheckboxElement.addEventListener("change", function(){
document.getElementById("file_upload").value = null;
pageReset();
}, false);

var inputElement = document.getElementById("file_upload");
inputElement.addEventListener("change", handleFileSelect, false);

Expand Down Expand Up @@ -283,12 +277,7 @@ function processExtractedFields(fields) {
}
}

function deduplicateCites(cites) {
addCitationCounts = false;
if (add_citation_counts_toggle.checked) {
addCitationCounts = true;
}

function deduplicateCites(cites) {
// create a nested array with items, their extracted uris, and their citation counts
var deduplicationArray = [];
for (let i = 0; i < cites.length; i++) {
Expand Down Expand Up @@ -355,14 +344,12 @@ function deduplicateCites(cites) {
// Store cite count
deduplicationArray[i].count = matchingCites.length;

// Add cite count to item metadata, if pref is set
if (addCitationCounts) {
if (deduplicationArray[i].hasOwnProperty("item")) {
if (!deduplicationArray[i].item.hasOwnProperty("note")) {
deduplicationArray[i].item.note = "";
}
deduplicationArray[i].item.note = "Times cited: " + deduplicationArray[i].count + "\n" + deduplicationArray[i].item.note;
// Add cite count to item metadata
if (deduplicationArray[i].hasOwnProperty("item")) {
if (!deduplicationArray[i].item.hasOwnProperty("note")) {
deduplicationArray[i].item.note = "";
}
deduplicationArray[i].item.note = "Times cited: " + deduplicationArray[i].count + "\n" + deduplicationArray[i].item.note;
}

// Mark other cites for deletion (via index property)
Expand Down Expand Up @@ -480,6 +467,9 @@ document.getElementById("download").addEventListener("click", function(){
case 'bibliography':
outputExtension = ".txt";
break;
case 'bibliography-with-counts':
outputExtension = ".tsv";
break;
default:
outputExtension = ".json";
}
Expand All @@ -495,11 +485,50 @@ var clipboard = new ClipboardJS('#copy_to_clipboard', {

function convertOutput() {
var csl_json = savedItemsString;

var outputFormat = outputElement.options[outputElement.selectedIndex].value;

let citationRender = new Cite(csl_json);

return citationRender.format(outputFormat);
var renderedOutput = "";

switch (outputFormat) {
case 'data-with-counts':
var citationRender = new Cite(csl_json);
renderedOutput = citationRender.format("data");
break;
case 'bibliography-with-counts':
// add cite count into json title
var edited_json = JSON.stringify(JSON.parse(csl_json).map(c => {
let count = c.note.match(/(?<=Times cited: )(\d+)/g) | 'NA';
c['title'] = `[${count} citations] ${c['title']}`;
return c;
}));
// format as apa and move to beginning of line
var citationRender = new Cite(edited_json);
let bibliography = citationRender.format('bibliography')
.split('\n')
.map(ref => {
let count_str = (ref.match(/\[(\d+) citations\] /) || ['', '0']);
ref = [Number(count_str[1]), count_str[1] + '\t' + ref.replace(count_str[0], '')];
return ref;
});
// sort by count
renderedOutput = 'cite_count\treference\n' + bibliography
.sort((a, b) => { return a[0] - b[0]})
.map(r => r[1])
.filter(r => r != '0\t')
.join('\n');
break;
default:
//remove cite counts
var edited_json = JSON.stringify(JSON.parse(csl_json).map(c => {
c.note = c.note.replace(/Times cited: \d+\n/g, '');
if (c.note == "") { c.note = undefined; }
return c;
}));
var citationRender = new Cite(edited_json);
renderedOutput = citationRender.format(outputFormat);
}

return renderedOutput;
}

// Provide some feedback on button click
Expand Down

0 comments on commit 7171d4d

Please sign in to comment.