Skip to content

Commit

Permalink
OpenAlex first working version
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-src-client-literature.js,AUTO-COMMIT-src-client-protocols-alex.js,AUTO-COMMIT-src-client-protocols-scholar.js,AUTO-COMMIT-src-components-tools-literature-paper.js,AUTO-COMMIT-src-components-tools-literature-search.js,AUTO-COMMIT-test-client-literature-test.js,
  • Loading branch information
JensLincke committed Mar 5, 2025
1 parent 71de294 commit 0063ac4
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 26 deletions.
23 changes: 19 additions & 4 deletions src/client/literature.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export class Paper {
}

static async getId(id, optionalEntity) {
if (Preferences.get("UseOpenAlex")) {
var json = await fetch("alex://data/" + id).then(r => r.json())
return new AlexPaper(json)
}

var paper = this.byId(id)
if (paper) return paper
if (optionalEntity) {
Expand Down Expand Up @@ -237,11 +242,13 @@ export class Paper {
entryTags: {
author: this.authors.map(author => author.name).join(" and "),
title: this.title,
year: this.year,
scholarid: this.scholarid,
year: this.year
},
entryType: this.bibtexType
}

if (this.scholarid) entry.entryTags.scholarid = this.scholarid
if (this.alexid) entry.entryTags.alexid = this.alexid
if (this.booktitle) { entry.entryTags.booktitle = this.booktitle }
if (this.doi) { entry.entryTags.doi = this.doi }

Expand Down Expand Up @@ -452,6 +459,10 @@ export class AlexAuthor {

export class AlexPaper extends Paper {

get alexid() {
return this.value.id.replace("https://openalex.org/","")
}


get authors() {
return (this.value.authorships || []).map(ea => new AlexAuthor(ea))
Expand All @@ -463,7 +474,7 @@ export class AlexPaper extends Paper {
}

get doi() {
return this.value.doi
return this.value.doi && this.value.doi.replace("https://doi.org/","")
}


Expand Down Expand Up @@ -495,7 +506,11 @@ export class AlexPaper extends Paper {
}

get booktitle() {
return this.value.primary_location.source.display_name
var source = this.value.primary_location.source
if (source && source.display_name) {
return source.display_name
}
return ""
}

get keywords() {
Expand Down
14 changes: 9 additions & 5 deletions src/client/protocols/alex.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ export default class OpenAlexScheme extends Scheme {
if (query.length < 2) return this.response(`{"error": "query to short"}`);

if (mode === "browse") {
if (query.match("work/")) {
let id = query.replace(/paper\//,"")
return this.response(`<literature-paper openalexid="${id}"><literature-paper>`);
} else {
return this.response(`query not supported: ` + query);
if (query.match(/W.*/)) {
let id = query.replace(/.*\//,"")
return this.response(`<literature-paper alexid="${id}"><literature-paper>`);
}
}


var url = this.baseURL + query

var headers = new Headers({})
Expand All @@ -72,6 +71,11 @@ export default class OpenAlexScheme extends Scheme {
headers: headers
}).then(r => r.text())

if (mode === "browse") {
var json = JSON.parse(content)
content = JSON.stringify(json, undefined, 2)
}

return this.response(content);
}

Expand Down
9 changes: 6 additions & 3 deletions src/client/protocols/scholar.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ export default class SemanticScholarScheme extends Scheme {
} else if (query.match("author/")) {
var authorId = query.replace(/.*author\//,"")
return this.response(`<literature-paper authorid="${authorId}"><literature-paper>`);
} else {
return this.response(`query not supported: ` + query);
}
}


}
Expand All @@ -137,6 +135,11 @@ export default class SemanticScholarScheme extends Scheme {
headers: headers
}).then(r => r.text())

if (mode === "browse") {
var json = JSON.parse(content)
content = JSON.stringify(json, undefined, 2)
}

return this.response(content);
}

Expand Down
33 changes: 28 additions & 5 deletions src/components/tools/literature-paper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"enable aexpr";

import Morph from 'src/components/widgets/lively-morph.js';
import {Author, Paper, Scholar} from "src/client/literature.js"
import {AlexPaper, Author, Paper, Scholar} from "src/client/literature.js"
import Literature from "src/client/literature.js"
/*MD # Literature Paper
Expand Down Expand Up @@ -46,6 +46,17 @@ export default class LiteraturePaper extends Morph {
}


get alexId() {
return this.getAttribute("alexId")
}

set alexId(id) {
this.data = null
this.setAttribute("alexId", id)
this.updateView()
}


get mode() {
return this.getAttribute("mode")
}
Expand Down Expand Up @@ -81,7 +92,10 @@ export default class LiteraturePaper extends Morph {
// #important
async ensureData() {
if (this.data) return this.data
if (this.scholarId || this.scholarPaper) {

if (this.alexId) {
this.url = `alex://data/${this.getAttribute("alexId")}`
} else if (this.scholarId || this.scholarPaper) {
// cached://
var id = this.scholarId || this.scholarPaper
this.url = `scholar://data/paper/${id}?fields=${this.fields()}` // cached://
Expand Down Expand Up @@ -109,6 +123,10 @@ export default class LiteraturePaper extends Morph {

async ensurePaper() {
if (!this.paper) {
if (this.alexId) {
await this.ensureData()
this.paper = new AlexPaper(this.data)
}
if (this.scholarId) {
this.paper = await Paper.getId(this.scholarId)
}
Expand Down Expand Up @@ -154,7 +172,7 @@ export default class LiteraturePaper extends Morph {
return
}
await this.renderAuthor(data)
} else if (this.scholarId || this.scholarPaper) {
} else if (this.scholarId || this.scholarPaper || this.alexId) {
var paper = await this.ensurePaper()
await this.renderPaper(paper)
} else {
Expand Down Expand Up @@ -600,8 +618,13 @@ export default class LiteraturePaper extends Morph {
// this.scholarPaper = "MAG:2087784813"

// this.searchQuery = "Toward Multi Language And Multi Environment Framework For Live Programming"
this.scholarId = "f24887f1cb1f1783c9a4481067453790b96f0752"
this.mode = "short"


// this.scholarId = "f24887f1cb1f1783c9a4481067453790b96f0752"
// this.mode = "short"

this.alexId = "W2741809807"
// this.mode = "short"

// this.searchQuery = "Smalltalk 80"
}
Expand Down
40 changes: 31 additions & 9 deletions src/components/tools/literature-search.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Bibliography from "src/client/bibliography.js"
import FileIndex from "src/client/fileindex.js";
import moment from "src/external/moment.js"
import {Paper} from "src/client/literature.js"
import {Paper, AlexPaper} from "src/client/literature.js"
import Morph from 'src/components/widgets/lively-morph.js';
import Preferences from 'src/client/preferences.js';


/*MD # Literature Search
Expand Down Expand Up @@ -112,10 +113,12 @@ export default class LiteratureSearch extends Morph {
var rows = []
let allBibtexEntries = await FileIndex.current().db.bibliography.toArray()
for(let bib of bibEntries) {
let id = bib.value.entryTags.scholarid
let id = bib.value.entryTags.scholarid || bib.value.entryTags.alexid
debugger
let existing = allBibtexEntries
.filter(ea => ea.key == bib.value.citationKey)
.filter(ea => !this.baseURL || ea.url.startsWith(this.baseURL))

let rename = <a title="rename file" class="method"
click={async () => {
await this.literatureListing.renameFile(this.renameURL, bib.generateFilename() + ".pdf")
Expand Down Expand Up @@ -168,19 +171,33 @@ export default class LiteratureSearch extends Morph {
return bibEntries
}

async findBibtexEntriesAlex(queryString, div) {
var json = await fetch("alex://data/works?search=" + queryString).then(r => r.json())
if (json.error) return [];
var papers = json.results.map(ea => new AlexPaper(ea))
var bibEntries = []
for(let ea of papers) {
bibEntries.push(await this.bibtexComponentForEntry(ea.toBibtexEntry(), ea))
}
return bibEntries
}

async findBibtexEntriesFuzzy(queryString, div) {
var bibEntries = []

var json
if (Preferences.get("UseOpenAlex")) {
return this.findBibtexEntriesAlex(queryString, div);
}
var fields = "externalIds,url,title,year,referenceCount,citationCount,fieldsOfStudy,s2FieldsOfStudy,authors"
var json = await fetch("scholar://data/paper/search?query=" + queryString + `&fields=${fields}`).then(r => r.json())
json = await fetch("scholar://data/paper/search?query=" + queryString + `&fields=${fields}`).then(r => r.json())

if (!json || !json.data) {
div.innerHTML = "nothing found"
return []
}

for(let ea of json.data) {
let bib = await (<lively-bibtex-entry mode="readonly"> </lively-bibtex-entry>)
let entry = {
entryTags: {
author: ea.authors.map(author => author.name.replace(/<\/?[a-z]+>/g,"")).join(" and "),
Expand All @@ -194,19 +211,24 @@ export default class LiteratureSearch extends Morph {
entryType: "article"
}
entry.citationKey = Bibliography.generateCitationKey(entry)
bibEntries.push(this.bibtexComponentForEntry(entry, ea))
}
return bibEntries
}

async bibtexComponentForEntry(entry, data) {
let bib = await (<lively-bibtex-entry mode="readonly"> </lively-bibtex-entry>)
bib.value = entry
bib.updateView()
bib.addEventListener("click", evt => {
if (evt.shiftKey) {
lively.openInspector(ea)
lively.openInspector(data)
}
})

bibEntries.push(bib)
}
return bibEntries
return bib
}


livelyMigrate(other) {
this.literatureListing = other.literatureListing
}
Expand Down
6 changes: 6 additions & 0 deletions test/client/literature-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ describe('Literature', () => {
expect(paper.key).to.be.a("string")
expect(paper.key).to.equal("Piwowar2018SOL")
});


it('has an id', () => {
expect(paper.alexid).to.be.a("string")
expect(paper.alexid).to.equal("W2741809807")
});


})
});

0 comments on commit 0063ac4

Please sign in to comment.