User:Ahecht/Scripts/draft-sorter.js: Difference between revisions

Content deleted Content added
&cmnamespace=118
Use LiftWing instead of ORES, allowing it to work on other language wikis
Line 186:
function getPredicts() {
var dbNamelang = mw.config.get( "wgDBnamewgServerName").split(".wikipedia.org" );
if (lang.length == 1) return;
const liftWingExternalEndpoint = "https://api.wikimedia.org/service/lw/inference/v1/models/";
let headers = new Headers({
"Content-Type": "application/json",
"User-Agent": "draft-sorter (https://en.wikipedia.org/wiki/User:Ahecht/Scripts/draft-sorter.js)"
} );
var revID = mw.config.get( "wgCurRevisionId" );
var model = (lang[0] == "en") ? "enwiki-drafttopic" : "articletopic-outlink";
var postBody = JSON.stringify({
$.getJSON( "//ores.wikimedia.org/v3/scores/" + dbName + "/" + revID + "/drafttopic" )
"rev_id": revID,
.done( function ( data ) {
"lang": lang[0],
if(data && data[dbName] && data[dbName].scores &&
"page_title": mw.config.get("wgPageName")
data[dbName].scores[revID] &&
});
data[dbName].scores[revID].drafttopic &&
 
data[dbName].scores[revID].drafttopic.score &&
fetch(liftWingExternalEndpoint + model + ":predict", {
data[dbName].scores[revID].drafttopic.score.prediction) {
method: "POST",
headers: new Headers({
"Content-Type": "application/json",
"User-Agent": "draft-sorter (https://en.wikipedia.org/wiki/User:Ahecht/Scripts/draft-sorter.js)"
}),
body: postBody
}).then(response => response.json()).then(data => {
var prediction = [];
var dbName = mw.config.get("wgDBname");
}
if(data && data[dbName] && data[dbName].scores &&
data[dbName].scores[revID] &&
data[dbName].scores[revID].drafttopic &&
data[dbName].scores[revID].drafttopic.score &&
data[dbName].scores[revID].drafttopic.score.prediction) {
var prediction = data[dbName].scores[revID].drafttopic.score.prediction;
} else if (data && data.prediction && data.prediction.results) {
data.prediction.results.forEach( functionp (item)=> {
if (p && p.topic) prediction.push(p.topic);
} );
}
if (prediction.length) {
console.log("Got ORES response! Raw predictions:");
console.log(prediction);
prediction.doneforEach( function ( data item) {
elementvar last = item.split(".")[elementitem.split(".").length-1] != last &&;
elementvar penultimate = item.split(".")[elementitem.split(".").length-2] == penultimate;
} else if ( last.substr(-1) == "*" ) {
// Filter out redundant starred predictions
if (prediction.find(element => (
element.split(".")[element.split(".").length-1] != last &&
element.split(".")[element.split(".").length-2] == penultimate
) ) ) {
console.log("Prediction \"" + last + "\" excluded.");
last = null;
} else {
last = penultimate;
}
}
if ( wikiprojects[last] ) {
var prediction = data[dbName].scores[revID].drafttopic.score.prediction;
// WikiProject found, no need to try splitting
console.log("Got ORES response! Raw predictions:");
console predicts.logpush(predictionlast);
} else if ( last ) {
// Can't find wikiProject, try splitting
prediction.forEach( function (item) {
var lastsplitLast = itemlast.split(".")[item.split/("." & | and )/).length-1];
var penultimatefor (i= item.split(".")[item.split(".")0;i<=splitLast.length-;i+=2];) {
if splitLast[i] (= lastsplitLast[i].substrcharAt(-10) == "*" .toUpperCase() {
+ splitLast[i].slice(1);
// Filter out redundant starred predictions
if (predictionpredicts.findpush(element =>splitLast[i] ();
element.split(".")[element.split(".").length-1] != last &&
element.split(".")[element.split(".").length-2] == penultimate
) ) ) {
console.log("Prediction \"" + last + "\" excluded.");
last = null;
} else {
last = penultimate;
}
}
if ( wikiprojects[last] ) {
// WikiProject found, no need to try splitting
predicts.push(last);
} else if ( last ) {
// Can't find wikiProject, try splitting
var splitLast = last.split(/( & | and )/);
for (i=0;i<=splitLast.length;i+=2) {
splitLast[i] = splitLast[i].charAt(0).toUpperCase()
+ splitLast[i].slice(1);
predicts.push( splitLast[i] );
}
}
}
} );
console.log("Filtered predictions:");
console.log(predicts);
showPredicts();
} else {
console.errorwarn("Error finding predictions in ORES response:");
console.errorwarn(data);
}
} ).catch( e => console.errorwarn("Error retrieving ORES data: " + errore); );
} ).fail( function ( error ) {
 
console.error("Error retrieving ORES data: " + error);
} );
return;
}