Skip to content

Instantly share code, notes, and snippets.

@angrytoast
Created February 5, 2012 05:21
Show Gist options
  • Save angrytoast/1743043 to your computer and use it in GitHub Desktop.
Save angrytoast/1743043 to your computer and use it in GitHub Desktop.
Category suggestion for Wikipeda
/*This function adds a jQuery include, this adds the latest version, instead of the version on wikipedia, which is outdated */
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
/*this function is the main segment of code that runs via the callback from addJQuery() */
function main() {
var selArray = [];
var txtSelection = "";
var catList = ["Living people","Windows games","English-language albums","English-language films","Grammy Award winners","Fungi of North America","Year of birth unknown","Fungi of Asia","Fungi of Europe","Animals described in 1758","American film actors","Wisden Cricketers of the Year","Video games developed in the United States","PlayStation 2 games","Australian aviators","Article Feedback 5","Knights of the Garter","Australian cricketers","Article Feedback Blacklist","American people of English descent","Military history of Japan during World War II","Deaths from myocardial infarction","Conflicts in 1942","Birds of Europe","Australia Test cricketers","Interactive Achievement Award winners","Companions of the Distinguished Service Order","Battles and operations of World War II involving the Solomon Islands","Xbox 360 games","Video game sequels","Royal Australian Air Force personnel of World War II","Governors of Kentucky","Commanders of the Order of the British Empire","Oryzomyini","Musical quartets","Monotypic mammal genera","Knights Bachelor","Cretaceous dinosaurs","1942 in the Solomon Islands","Rock and Roll Hall of Fame inductees","PlayStation games","Officers of the Order of the British Empire","Fellows of the Royal Society","English-language songs","Edible fungi","Companions of the Order of the Bath","Chemical elements","Birds of South Australia","Birds of Asia","American Presbyterians","1942 in Japan","Royal Australian Air Force air marshals","Protestant monarchs","People of the Tudor period","Mac OS X games","Hurricanes in Florida","Football clubs in England","English Roman Catholics","Dinosaurs of North America","Birds of Western Australia","Australian military personnel of World War I","Australian knights","American television actors","The Invincibles (cricket)","Presidential Medal of Freedom recipients","PlayStation Network games","PlayStation 3 games","Pacific Ocean theater of World War II","Kentucky Democrats","First-person shooters","Defunct science fiction magazines of the United States","Burials at Westminster Abbey","Battles involving France","Australian Army officers","Archbishops of Canterbury","Anglo-Normans","2005 video games","United States Marine Corps in World War II","Operas","Nintendo GameCube games","Members of the Privy Council of the United Kingdom","Knights Commander of the Order of the British Empire","Harvard University alumni","Greek loanwords","Eudicots of Western Australia","Battleship classes","American people of Scottish descent","17th-century English people","World War I warships scuttled in Scapa Flow","Wii games","Virtual Console games","Royal Australian Air Force officers","Recipients of the Distinguished Flying Cross (United Kingdom)","Planemos","People of the Edwardian era","New South Wales cricketers","National Historic Persons of Canada","English-language television series","English-language singers","Converts to Roman Catholicism","Coins of the United States","Battles of World War II involving the United States","Anglican saints","American films","20th-century classical composers","2007 video games","1894 births","16th-century English people","Universal Pictures films","Stanley Cup champions","Science fiction video games","Royal Fellows of the Royal Society","Recipients of the Croix de Guerre (France)","Olympic swimmers of Australia","Nintendo 64 games","Member states of the United Nations","Fungi of South America","English pretenders to the French throne","English cricketers","Deaths from tuberculosis","Deaths from stroke","Cooperative video games","Brit Award winners","Birds of Pakistan","Australian cricket captains","American people of Irish descent","American male singers","Action-adventure games","2006 video games","World War I battleships of Germany","World Music Awards winners","The Invincibles (cricket)","Sequel films","Royal Philharmonic Society Gold Medallists","Recipients of the Military Cross","Real-time strategy video games","People of the Victorian era","Olympic medalists in swimming","Olympic medalists in ice hockey","Olympic gold medalists for Australia","Naturalized citizens of the United States","National Hockey League All-Stars","Mercian monarchs","Kentucky lawyers","Heroic Age of Antarctic Exploration","Fungi of Australia","Former Football League clubs","First-person adventures","England Test cricketers","Directorial debut films","British Army personnel of World War I","Billboard Hot 100 number-one singles","Australian military personnel of World War II","Australian Army soldiers","Atlantic tropical storms","American military personnel of World War II","7th-century rulers in Europe","1997 television episodes","1941 deaths","1913 ships","Xbox games","Video games with expansion packs","Video games developed in Japan","United States Navy officers","Towns in Greater Manchester","Ships built in Pennsylvania","Science fiction magazines established in the 1950s","Princes of Wales","Princes of the United Kingdom","Presidents of the United States","Premier League clubs","People from Westminster","People from Victoria (Australia)","Number-one singles in Australia","National Hockey League first round draft picks","Multiplayer online games","Monotypic bird genera","Members of the Order of the British Empire","Hurricanes in South Carolina","Grammy Hall of Fame Award recipients","Fungi of Africa","Flora of New South Wales","Final Fantasy games","FA Cup winners","Explorers of Antarctica","English women writers","English people of Scottish descent","English Anglicans","Democratic Party United States Senators","Concept albums","Burials at Frankfort Cemetery","Birds of Turkey","Birds of Tasmania","Battles of World War II involving Japan","Battles of World War II involving Australia","Australian World War I flying aces","Australian swimmers","Art games","American child actors","Albums produced by Rick Rubin","Research","Library science","Reference works","Almanacs","Atlases","Biographical dictionaries","Dictionaries","Online dictionaries","Directories","Web directories","Encyclopedias","Online encyclopedias","Glossaries","Handbooks and manuals","Lists","Medical manuals","Reference book stubs","Reference works in the public domain","Style guides","Trivia books","Websites","Academic disciplines","Archives","Books","Clients (computing)","Colleges","Curricula","Databases","Online databases","Distance education","Grammar","Government agencies","Indexes","Information","Knowledge","Libraries","Digital libraries","Library cataloging and classification","News agencies","Periodic table","Prefixes","Reading","Research","Internet search engines","Suffixes","Fundamental categories","Universities","Writing","Culture","Arts","Culture","Humanities","Classical studies","Critical theory","Cultural anthropology","Folklore","Food culture","Gastronomy","Languages","Literature","Museology","Mythology","Philosophy","Popular culture","Science and culture","Traditions","Arts","Entertainment","Arts and crafts","Celebrity","Censorship in the arts","Festivals","Humor","Literature","Museums","Parties","Poetry","Performing arts","Dance","Film","Music","Opera","Storytelling","Theatre","Visual arts","Architecture","Comics","Crafts","Design","Drawing","Film","Animation","Painting","Photography","Sculpture","Games","Toys","Board games","Card games","Dolls","Puppetry","Puzzles","Role-playing games","Video games","Mass media","Broadcasting","Film","Internet","Magazines","Newspapers","Publications","Publishing","Radio","Television","Sports","Recreation","Air sports","American football","Association football","Auto racing","Baseball","Basketball","Boating","Boxing","Canoeing","Cricket","Cycling","Exercise","Fishing","Golf","Gymnastics","Hobbies","Horse racing","Ice hockey","Lacrosse","Olympics","Rugby league","Rugby union","Sailing","Skiing","Swimming","Tennis","Track and field","Walking","Water sports","Whitewater sports","Geography","Places","Cities","Communities","Continents","Countries","Deserts","Earth","Lakes","Landforms","Maps","Mountains","Oceans","Protected areas","Regions","Rivers","Subterranea","Territories","Towns","Villages","Health","Self care","Healthcare occupations","Self care","Health promotion","Life extension","Prevention","Sexual health","Nutrition","Dietary supplements","Dietetics","Nutrients","Amino acids","Dietary minerals","Nootropics","Phytochemicals","Vitamins","Nutritional advice pyramids","Exercise","Aerobics","Bodyweight exercise","Cycling","Exercise equipment","Exercise instructors","Dance","Exercise physiology","Hiking","Pilates","Running","Sports","Swimming","T%27ai Chi Ch%27uan","Walking","Weight training","Weight training exercises","Yoga","Hygiene","Cleaning","Oral hygiene","Positive psychology","Mental health","Psychotherapy","Public health","Health by country","Healthcare","Health law","Health promotion","Health standards","Hospitals","Occupational safety and health","Pharmaceutical industry","Safety","Health sciences","Clinical research","Diseases and disorders","Epidemiology","Midwifery","Nursing","Nutrition","Optometry","Pharmacy","Public health","Medicine","Veterinary medicine","Dentistry","Oral hygiene","Orthodontics","Pharmaceuticals policy","Medicine","Alternative medicine","Cardiology","Endocrinology","Forensics","Gastroenterology","Genetics","Geriatrics","Gerontology","Gynecology","Hematology","Nephrology","Neurology","Obstetrics","Oncology","Ophthalmology","Orthopedic surgery","Pathology","Pediatrics","Psychiatry","Rheumatology","Sleep","Surgery","Urology","History","Events","History by period","History by topic","History of science","History of religion","Historiography","Timelines","History by location","History by region","History of Africa","History of Asia","History of Europe","History of North America","History of the Middle East","History of Oceania","History of South America","History by country","History by city","Empires","Mathematics","Abstraction","Algebra","Mathematical analysis","Arithmetic","Mathematics education","Equations","Geometry","Logic","Measurement","Numbers","Theorems","Trigonometry","Statistics","Analysis of variance","Bayesian statistics","Categorical data","Covariance and correlation","Data analysis","Decision theory","Design of experiments","Logic and statistics","Multivariate statistics","Non-parametric statistics","Parametric statistics","Regression analysis","Sampling (statistics)","Statistical theory","Stochastic processes","Summary statistics","Survival analysis","Time series analysis","Uncertainty of numbers","Science","Natural sciences","Nature","Biology","Ecology","Health sciences","Medicine","Neuroscience","Earth sciences","Atmospheric sciences","Geography","Geology","Geophysics","Oceanography","Nature","Animals","Environment","Humans","Life","Natural resources","Plants","Pollution","Physical sciences","Astronomy","Chemistry","Climate","Physics","Space","Scientific method","Scientists","People","Personal life","Self","Surnames","People","Beginners and newcomers","Biography","Children","Heads of state","Humans","Legal categories of people","Men","Old age","Political people","Rivalry","Social groups","Subcultures","Women","People by city","People by company","People by continent","People by educational institution","People by ethnicity","People by ethnic or national descent","People by ethnic or national origin","People by medical or psychological condition","People by nationality","People by occupation","People by political orientation","People by religion","People by revolution","People by status","Births by year","Deaths by year","Lists of people","Personal timelines","Terms for females","Terms for males","Activists","Actors","Astronauts","Billionaires","Chief executives","Colonial people","Composers","Cyborgs","Defectors","Generals","Humanitarians","Innovators","Inventors","LGBT people","Monarchs","Musicians","Musical groups","Philosophers","Photographers","Politicians","Presidents","Princes","Princesses","Revolutionaries","Scientists","Settlers","Singers","Slaves","Victims","People associated with war","Writers","Self","Alter egos","Consciousness studies","Gender","Personality","Human sexuality","Sexual orientation","Personal life","Clothing","Employment","Entertainment","Food and drink","Games","Health","Hobbies","Home","Income","Interpersonal relationships","Leisure","Love","Motivation","Personal development","Pets","Philosophy","Thought","Philosophy","Branches of philosophy","Philosophical schools and traditions","Philosophical movements","Philosophical theories","Philosophical arguments","Philosophers","Philosophical literature","History of philosophy","Philosophy by era","Philosophy by region","Aesthetics","Epistemology","Ethics","Logic","Metaphysics","Thought","Attention","Cognition","Cognitive biases","Creativity","Decision theory","Emotion","Error","Imagination","Intelligence","Intelligence researchers","Learning","Memory","Memory biases","Mnemonics","Nootropics","Strategic management","Perception","Problem solving","Psychological adjustment","Psychometrics","Qualities of thought","Religion","Belief","Allah","Bible","Buddhas","Deities","Demons","Exorcism","God","Jesus","Mythology","Occult","Prayer","Prophecy","Quran","Religious ethics","Religious law","Ritual","Spirituality","Theology","Religious faiths, traditions, and movements","Agnosticism","Animism","Atheism","Deism","Determinism","Esotericism","Gnosticism","Humanism","Monism","Monotheism","Mysticism","New Age","Paganism","Pantheism","Polytheism","Shamanism","Skepticism","Spiritualism","Theosophy","Transcendentalism","Unitarianism","Agnosticism","Atheism","Ayyavazhi","Bah%C3%A1%27%C3%AD Faith","Buddhism","Cao Dai","Chinese folk religion","Christianity","Confucianism","Druidry","Falun Gong","Hinduism","Islam","Jainism","Judaism","Mormonism","Neopaganism","Rastafarianism","Satanism","Scientology","Sikhism","Spiritism","Shinto","Taoism","Tenrikyo","Unitarian Universalism","Wicca","Zoroastrianism","Mythology","Mythology by culture","Abrahamic mythology","Buddhist mythology","Christian mythology","Hindu mythology","Islamic mythology","Jewish mythology","Criticism of religion","Science","Social sciences","Society","Scientific method","Scientists","Social sciences","Anthropology","Archaeology","Cultural studies","Demographics","Economics","Heuristics","Information science","International relations","Linguistics","Psychology","Media studies","Political science","Social scientists","Sociology","Sexology","Systems theory","Society","Activism","Business","Communication","Crime","Education","Ethnic groups","Family","Finance","Government","Health","Home","Industries","Infrastructure","Labor","Law","Mass media","Military","Money","Organizations","Peace","Politics","Real Estate","Rights","War","Technology","Applied sciences","Agriculture","Agronomy","Architecture","Automation","Biotechnology","Blu-ray","Cartography","Chemical engineering","Communication","Media studies","Telecommunications","Construction","Design","Digital divide","Earthquake engineering","Electronics","Energy","Ergonomics","Firefighting","Fire prevention","Forensics","Forestry","Industry","Information science","Information technology","Internet","Management","Manufacturing","Marketing","Medicine","Unsolved problems in neuroscience","Metalworking","Microtechnology","Military science","Mining","Nanotechnology","Nuclear technology","Nutrition","Optics","Plumbing","Robotics","Sound technology","Technology forecasting","Tools","Computing","Artificial intelligence","Classes of computers","Computing by company","Computer architecture","Computing by computer model","Computer science","Computer security","Computing and society","Computer data","Embedded systems","Free software","Human%E2%80%93computer interaction","Information systems","Internet","Mobile Web","Computer languages","Multimedia","Computer networks","Computer networks","Operating systems","Computing platforms","Product lifecycle management","Computer programming","Real-time computing","Software","Software engineering","Unsolved problems in computer science","Computing","Electronics","Avionics","Integrated circuits","Electronic circuits","Electronics companies","Electrical connectors","Consumer electronics","Digital electronics","Digital media","Electrical components","Electronic design","Electronics manufacturing","Embedded systems","Integrated circuits","Microwave technology","Molecular electronics","Water technology","Optoelectronics","Quantum electronics","Radio-frequency identification","Radio electronics","Semiconductors","Signal cables","Surveillance","Telecommunications","Electronics terms","Electronics","Engineering","Aerospace engineering","Bioengineering","Chemical engineering","Civil engineering","Electrical engineering","Environmental engineering","Materials science","Mechanical engineering","Nuclear technology","Software engineering","Structural engineering","Systems engineering","Transport","Transport by country","Automobiles","Aviation","Cycling","Public transport","Rail transport","Road transport","Shipping","Spaceflight","Vehicles","Water transport","Technology timelines","Technology","Technology portals","Technology-related lists","Technology by type","Automation","Communication","Technical communication","Technology companies","Technological comparison","Technology conventions","Science and technology by country","Documentary films about technology","Energy","Engineering","Equipment","Technology evangelism","Fictional technology","Films about technology","Technology folklore","High-technology business districts","History of technology","Hypothetical technology","Technology images","Industry","Information technology","Inventions","Technology literature","Science and technology by location","Medical technology","Mobile technology","Technology neologisms","Obsolete technologies","Packaging","Science parks","Philosophy of technology","Technology podcasts","Projects","Real-time technology","Technology and inventions by region","Science and engineering awards","Technology in society","Technology strategy","Science and technology studies","Technology systems","Technicians","Technological change","Technology conferences","Technology development","Technology transfer","Tools","Technology trade associations","Technical universities and colleges","Technology websites","Wireless","Wikipedia books on technology","Technology stubs"];
//add a unique value function to arrays
Array.prototype.unique = function() {
var o = {}, i, l = this.length, r = [];
for(i=0; i<l;i+=1) o[this[i]] = this[i];
for(i in o) r.push(o[i]);
return r;
};
//grab all text from the wiki edit textbox
var fulltxt = jQuery("#wpTextbox1").val();
//remove some common wikimarkups
var txtNoLinks = fulltxt.replace(/('+|\{+|\}+|\[+|\]+)/gi,"").replace(/<.+\/?>/gi,"").replace(/\{.*?\}/gi,"");
//used to map wikitext into simple html paragraphs
function parag(s){
return "<p>"+s+"</p>";
}
jQuery("head").append('<link type="text/css" rel="stylesheet" href="http://students.washington.edu/garygao/firecat.css" />');
//add the main firecat content block
jQuery("body").append("<div id='firecat'><div class='ctrl' style='text-align:right;'><b class='show' style='display:none;cursor:pointer;'>Show</b><b class='hide' style='cursor:pointer;'>Collapse</b></div> <div id='firecatCon'> <div class='maintxt txt' style='position:absolute;right:10px;width:450px;height:382px;overflow-y:auto;'></div> <div id='firecatSel'><h5>Selected Terms</h5> <div class='txt'></div> <div class='search'></div> </div> <div id='firecatResults'> <h5>Suggested Categories</h5> <div class='txt'></div></div></div><button style='position:absolute;bottom:0;left:5px;' id='subSelCats'>Add all selected categories</button></div>");
//add the confirm tooltip for adding selected terms to list
jQuery("body").append("<div id='fcConfirm' style='display:none;position:absolute;z-index:1000;padding:5px;background:#fff;border:1px solid #fc0;font-size:12px'>Add selection to list? <b style='display:inline-block;cursor:pointer;' class='y'>Yes</b> | <b style='display:inline-block;cursor:pointer;' class='n'>No</b></div>");
//bind YES and NO click functions for confirm tooltip
jQuery("#fcConfirm .y").on("click", function(){
// $("#firecatSel .txt").append("<p>" + txtSelection.toLowerCase() + "</p>"); REMOVED
selArray.push(txtSelection.toLowerCase());
printSelectedTerms(); // ADDED
findCats(selArray);
console.log(selArray);
$(this).parent().hide();
});
jQuery("#fcConfirm .n").on("click", function(){
$(this).parent().hide();
});
//dump wikitext as html into firecat
jQuery("#firecat .maintxt").html( txtNoLinks.split("\n").map(parag).join("\n") );
//toggle show hide for firecat box
jQuery("#firecat .show").click(function(){
jQuery("#firecat").css("height","400").css("width","1000");
jQuery("#firecatCon").show();
$(this).hide().siblings().show();
});
jQuery("#firecat .hide").click(function(){
jQuery("#firecat").css("height","20").css("width","60");
jQuery("#firecatCon").hide();
$(this).hide().siblings().show();
});
// get the user-selected text
function getSelected() {
var t = '';
if(window.getSelection){
t = window.getSelection();
}else if(document.getSelection){
t = document.getSelection();
}else if(document.selection){
t = document.selection.createRange().text;
}
return t;
}
//
$('#firecat .maintxt').mouseup(function(e) {
txtSelection = getSelected();
if( txtSelection != "" && (txtSelection = new String(txtSelection).replace(/^\s+|\s+$/g,'')) ) { //if selection not empty, remove starting and trailing whitespace
selText = $('<div>').html(txtSelection).attr({
id: 'selText'
});
}
//show add term confirm tooltip
jQuery("#fcConfirm").show().css("left",e.pageX+5).css("top",e.pageY-20);
});
function findCats(phrases) { //phrases is the Array of selected terms
//create a new result set
var catResults = new Array();
//delete the current list of recommended categories
$('#firecatResults .txt').empty();
for (var j=0;j<phrases.length;j++){
var words = phrases[j].split(" ");
console.time("parsePhrases");
for(var x=0;x<words.length;x++){
var c = 0;
var reg = new RegExp( words[x], "gi"); //match work delimited by white space
for(var i=0;i<catList.length;i++) {
if(catList[i].toString().toLowerCase().match(reg)){
c++;
catResults.push([catList[i]]);
//catResults.push([catList[i],c]);
}
}
//console.log(catResults);
}
console.timeEnd("parsePhrases");
}
/*
//if full term phrase exists in category, put matching category into list
for(i in catList) {
if(catList[i].toString().toLowerCase().indexOf(phrases) > -1) {
catResults.push(catList[i]);
}
}*/
//generate a big html string of all matched categories and dump it into the results box
var temp = "";
for(var x=0;x < catResults.length;x++) {
temp += "<b class='recCat' style='display:block;padding:3px 5px;background:#def;border:1px solid #aaa;margin-bottom:2px;cursor:pointer;'><input class='selbox' type='checkbox' style='vertical-align:middle;'>" + catResults[x] + "</b>";
}
$("#firecatResults .txt").html(temp);
}
// delegated event binding to trigger adding category to main edit textbox on click of category results
$("#subSelCats").on("click", function(){
selCats = [];
jQuery("#firecat .recCat").each(function(){
if ( jQuery('.selbox',this).is(':checked') ) selCats.push( '[[Category:'+jQuery(this).text()+']]' )
});
//console.log(selCats);
jQuery("#wpTextbox1").append( selCats.join('\n') );
});
// remove a term: ADDED
$("#firecatSel").on("click", ".txt p", function() {
var index = $(this).attr("id");
selArray.splice(parseInt(index),1);
printSelectedTerms();
findCats(selArray);
});
// print selArray: ADDED
function printSelectedTerms() {
var temp = "";
for(var x=0; x < selArray.length; x++) {
temp += "<p id=" + x + ">" + selArray[x] + "</p>";
}
$("#firecatSel .txt").html(temp);
}
}
/* end main() */
addJQuery(main);
@angrytoast
Copy link
Author

02/18/2012 - Updated with confirmation message with appending terms to list;

todo - adjust category recommendation to utilize all terms in list; use selArray, parse selArray[i] and create list of recommend categories, replace innerHTML of #firecatResults .txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment