MediaWiki

LexemeQueriesGenerator.js

Revision as of 15:14, 16 December 2021 by Yug (talk | contribs)

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* TODO
1) List of languages with recordings AND number of speakers
See base : https://lingualibre.org/wiki/LinguaLibre:List_of_languages
See nbSp : https://w.wiki/4ZB3

Finish hacking based on https://jsfiddle.net/hugolpz/rygo9s5b/
[x] getData from elements
[x] Identify suitable query
[x] Edit suitable query
[x] Run/Open Query

Improve Query with federate SPARQL including LinguaLibre
1) Explore

Add to LinguaLibre
1) Add to [[User:Yug/OOUI]]
2) Migrate to general space.

Translate
1) Find a what to translate ?

MediaWiki Extension ?
1) Explore
*/


// Toolbox

/* ********************************************************************** */
/* QUERIES BASIS ******************************************************** */
// Current: Wikidata, Dbnary.
 var queries = {
 // Queries for Wikidata
  wikidata : 
    { 
      values :`VALUES ?word {'book'@GLWSSA}`,
      basic: `SELECT * WHERE {
      VALUESTOBEADDEDHERE
      ?l a ontolex:LexicalEntry ;
        dct:language wd:LNGCDE ;
        ontolex:lexicalForm ?form ; #test
        wikibase:lexicalCategory wd:POSTAG ;
        wikibase:lemma ?lemma .
      ?form ontolex:representation ?word .
    }`,
    senses: `SELECT * WHERE {
      VALUESTOBEADDEDHERE
      ?l a ontolex:LexicalEntry ;
        dct:language wd:LNGCDE ;
        ontolex:lexicalForm ?form ;
        wikibase:lexicalCategory wd:POSTAG ;
        wikibase:lemma ?lemma ;
        ontolex:sense ?sense .
      ?form ontolex:representation ?word .
    }`,
  sensesDefinitions : `SELECT * WHERE {
  VALUESTOBEADDEDHERE
	?l a ontolex:LexicalEntry ;
		dct:language wd:LNGCDE ;
		wikibase:lemma ?lemma ;
		ontolex:lexicalForm ?form ;
		wikibase:lexicalCategory ?category ;
		ontolex:sense ?sense .
	?form ontolex:representation ?word .
	?language wdt:P218 "GLWSSA" .
	?sense skos:definition ?gloss .
	FILTER EXISTS {?l ontolex:sense ?sense }
	FILTER(LANG(?gloss) = "GLWSSA")
}`,
  sensesDefinitionsExamples: `SELECT * WHERE {
  VALUESTOBEADDEDHERE
	?l a ontolex:LexicalEntry ;
		dct:language wd:LNGCDE ;
		wikibase:lemma ?lemma ;
		ontolex:lexicalForm ?form ;
		wikibase:lexicalCategory ?category ;
		ontolex:sense ?sense .
		?language wdt:P218 "GLWSSA" .
	?form ontolex:representation ?word .
	?sense skos:definition ?gloss .
	OPTIONAL{
		?l p:P5831 ?statement .
		?statement ps:P5831 ?example .
	}
	FILTER EXISTS {?l ontolex:sense ?sense }
	FILTER(LANG(?gloss) = "GLWSSA")
}`,
  translation: `SELECT DISTINCT * WHERE {   // queryTranslationWiki
	?source dct:language wd:LNGCDE;
		wikibase:lemma ?sourceLemma;
		ontolex:sense [ wdt:P5137 ?sense ].
	?target dct:language wd:LNGCDETRG;
		wikibase:lemma ?targetLemma;
		ontolex:sense [ wdt:P5137 ?sense ].
} ORDER BY ASC(UCASE(str(?sourceLemma)))
LIMIT 100`,
  translationLemma : `SELECT DISTINCT * WHERE {
  VALUESTOBEADDEDHERE
	?source dct:language wd:LNGCDE;
		wikibase:lemma ?sourceLemma;
		ontolex:lexicalForm ?form ;
		wikibase:lexicalCategory wd:POSTAG ;
		ontolex:sense [ wdt:P5137 ?sense ].
	?target dct:language wd:LNGCDETRG;
		wikibase:lemma ?targetLemma;
		ontolex:sense [ wdt:P5137 ?sense ].
	?form ontolex:representation ?word .
} ORDER BY ASC(UCASE(str(?sourceLemma)))`
},
 // Queries for Dbnary
dbnary : { 
 values: `VALUES ?label {'book'@GLWSSA}
 VALUES ?pos {<http://www.lexinfo.net/ontology/2.0/lexinfo#POSTAGNM>}`,
 basic: `SELECT * WHERE {VALUESTOBEADDEDHERE
   ?lexeme a ontolex:LexicalEntry ;
     rdfs:label ?label ;
     ontolex:canonicalForm ?form ;
     lime:language ?lang ;
     lexinfo:partOfSpeech   ?pos .
  FILTER(?lang = "GLWSSA")
}`,
  senses: `SELECT * WHERE {
  VALUESTOBEADDEDHERE
   ?lexeme a ontolex:LexicalEntry ;
     rdfs:label ?label ;
     ontolex:canonicalForm ?form ;
     lime:language ?lang ;
     lexinfo:partOfSpeech   ?pos ;
     ontolex:sense  ?sense .
 FILTER(?lang = "GLWSSA")
}`,
  sensesDefinitions: `SELECT ?lexeme ?label ?pos ?sense ?definition
WHERE {
   ?sense a ontolex:LexicalSense ;
     skos:definition ?def .
   ?def rdf:value ?definition .
   FILTER(lang(?definition) = "GLWSSA")
   {
      SELECT * WHERE {VALUESTOBEADDEDHERE
         ?lexeme a ontolex:LexicalEntry ;
         rdfs:label ?label ;
         ontolex:canonicalForm ?form ;
         lime:language ?lang ;
         lexinfo:partOfSpeech   ?pos ;
         ontolex:sense  ?sense .
      FILTER(?lang = "GLWSSA")
      } 
   }
}`,
  sensesDefinitionsExamples: ``,
  translation: ``,
  translationLemma: ``
  }
 };
 
/* ********************************************************************** */
/* OOJS / OOUI ********************************************************** */
// Data & options
var baseLanguages = [ //Major languages: cmn, spa, eng, fra, ara, rus, hin, swa
      // data https://w.wiki/4ZB3
      { data:'Q13955', ll:'', label:'ara — Arabic' },
      { data:'Q9610', ll:'', label:'ben — Bengali' },
      { data:'Q9192', ll:'Q113', label:'cmn — MandarinChinese' } ,
      { data:'Q188', ll:'', label:'deu — German' },
      { data:'Q1860', ll:'', label:'eng — English' },
      { data:'Q150', ll:'', label:'fra — French' },
      { data:'Q1568', ll:'', label:'hin — Hindi' },
      { data:'Q5287', ll:'', label:'jpn — Japanese' },
      { data:'Q1571', ll:'Q34', label:'mar — Marathi' },
      { data:'Q58635', ll:'', label:'pan — Punjabi' },
      { data:'Q5146', ll:'', label:'por — Portuguese' },
      { data:'Q7737', ll:'', label:'rus — Russian' },
      { data:'Q1321', ll:'', label:'spa — Spanish' },
    ],
    availableLanguages = [
      { data:'Q13955', ll:'', label:'ara — Arabic' },
      { data:'Q9610', ll:'', label:'ben — Bengali' },
      { data:'Q12107', ll:'Q209', label: 'bre: Breton' }, // lili 
      { data:'Q9192', ll:'Q113', label:'cmn — MandarinChinese' } ,
      { data:'Q188', ll:'', label:'deu — German' },
      { data:'Q1860', ll:'', label:'eng — English' },
      { data:'Q33298', ll:'', label:'fil — Filipino' },
      { data:'Q150', ll:'', label:'fra — French' },
      { data:'Q33454', ll:'', label:'ful — Fula' },
      { data:'Q56475', ll:'', label:'hau — Hausa' },
      { data:'Q1568', ll:'', label:'hin — Hindi' },
      { data:'Q9240', ll:'', label:'ind — Indonesian' },
      { data:'Q652', ll:'', label:'ita — Italian' },
      { data:'Q33549', ll:'', label:'jav — Javanese' },
      { data:'Q5287', ll:'', label:'jpn — Japanese' },
      { data:'Q9176', ll:'', label:'kor — Korean' },
      { data:'Q1571', ll:'Q34', label:'mar — Marathi' },
      { data:'Q9237', ll:'', label:'msa — Malay' },
      { data:'Q58635', ll:'', label:'pan — Punjabi' },
      { data:'Q5146', ll:'', label:'por — Portuguese' },
      { data:'Q7737', ll:'', label:'rus — Russian' },
      { data:'Q1321', ll:'', label:'spa — Spanish' },
      { data:'Q5885', ll:'', label:'tam — Tamil' },
      { data:'Q8097', ll:'', label:'tel — Telugu' },
      { data:'Q256', ll:'', label:'tur — Turkish' },
      { data:'Q1617', ll:'', label:'urd — Urdu' },
      { data:'Q9199', ll:'', label:'vie — Vietnamese' },
    ],
    languagesArray = [ ...availableLanguages ],
    posArray = [
      { data: 'Q1084', label: 'noun' },
      { data: 'Q36224', label: 'pronoun' },
      { data: 'Q24905', label: 'verb' },
      { data: 'Q380057', label: 'adverb' },
      { data: 'Q34698', label: 'adjective' },
      { data: 'Q21087400', label: 'quantitative'},
      { data: 'Q83034', label: 'interjection' },
      { data: 'Q4833830', label: 'preposition' },
      { data: 'Q103184', label: 'article' },
      { data: 'Q36484', label: 'conjunction' },
    ].map(o => new OO.ui.MenuOptionWidget({ data: o.data, label: o.label })),
    infoTypesArray = [
      { data: '1', label: 'Just look !' },
      { data: '2', label: 'Sense' },
      { data: '3', label: 'Sense, definition' },
      { data: '4', label: 'Sense, definition and examples'},
    ].map(o => new OO.ui.MenuOptionWidget({ data: o.data, label: o.label })),
    languagesArrayT = [ ...baseLanguages, ],
    endpointsArray = [
      { data: 'wikidata', label: 'Wikidata', selected: true },
      { data: 'Dbnary', label: 'Dbnary' },
      //{ data: 'LinguaLibre', label: 'LinguaLibre' },
    ];

// Elements
var lemma = new OO.ui.TextInputWidget({
    id: 'lqg-lemma',
    icon: 'search',
    placeholder: 'books',
    value: 'books',
		label: 'Word to search',
  }),
  languages = new OO.ui.ComboBoxInputWidget({ // ComboBox
    id: 'lqg-languages',
		placeholder : 'Target language',
    options: languagesArray,
    menu: {	filterFromInput: true, filterMode : 'substring'	}
  	// value: 'Q34',
	}),
  pos = new OO.ui.DropdownWidget( {
    id: 'lqg-pos',
		label: 'Part-of-speech',
		menu: { items: posArray }
	}),
  infoTypes = new OO.ui.DropdownWidget({
    id: 'lqg-infoTypes',
		label: 'Information to fetch ... (if any)',
		menu: { items: infoTypesArray }
	}),
  translations = new OO.ui.ComboBoxInputWidget({ // ComboBox
    id: 'lqg-translations',
		placeholder : 'Translations to fetch ... (if any)',
    options: languagesArrayT,
    menu: {	filterFromInput: true, filterMode : 'substring'	}
	}),
  endpoints = new OO.ui.RadioSelectInputWidget( { 
    id: 'lqg-endpoints',
  	label: 'Radios buttons',
    options: endpointsArray,
} ),
  limit = new OO.ui.CheckboxInputWidget({ id: 'lqg-limit', selected: true, }),
  limitLabel = new OO.ui.LabelWidget( { label: 'Limit to 100 (faster)' }),
  queryDisplay = new OO.ui.MultilineTextInputWidget( { 
    id: 'lqg-queryDisplay',
		//value: `# Query will appear here.\n\n\n\n\n\n`,
    placeholder: `# Query will appear here.\n\n\n\n\n\n`,
		multiline: true,
		autosize: true,
    minRows: 10,
		maxRows: 20
	} ),
  generate = new OO.ui.ButtonWidget({ 
  	id: 'lqg-generate', 
    label: 'Generate',
  }),
  run = new OO.ui.ButtonWidget({ 
    id: 'lqg-run',
    label:'Run !',
  });
  
// An example of a fieldset with horizontal layout.
var fieldset = new OO.ui.FieldsetLayout( {
		label: 'Queries Generator for Wikidata Lexemes'
	} );
fieldset.addItems( [ 
  new OO.ui.FieldLayout(
    new OO.ui.Widget( {
      content: [ 
        new OO.ui.HorizontalLayout( { items: [
          lemma,
          pos,
          languages,
          infoTypes,
          translations,
        ]}),
        new OO.ui.HorizontalLayout( { items: [
          generate,
        	run,
          limit,
          limitLabel,
          endpoints,
        ]}),
        queryDisplay
      ]
    }),{
			label: 'Select the suitable values to build your Wikidata Lexemes Query.',
			align: 'top'
		})
	]
);
$( "#lqg" ).append( fieldset.$element );

// Check data
var checkAvailableData = function(identity,element){
	// console.log('2',languages)
	console.log(identity+'a getElementId(): ',element.getElementId())
	console.log(identity+'b getData(): ',element.getData())
  console.log(identity+'c getValue(): ',element.getValue())
}

$('#lqg-lemma').on('focusout',function() {checkAvailableData(1,lemma);})
$('#lqg-languages').on('focusout',function() { checkAvailableData(2,languages);});
checkAvailableData(2,languages);



/* ********************************************************************** */
/* GENERATE QUERY STRING ************************************************ */
// Current: Wikidata, Dbnary.
 var generateQuery = function () {
   // getData from elements
   var selectedDropdown = function (group){
    var items = group.getMenu().items.filter(item=> item.selected==true )
    return items[0]?items[0].data:'';
   }
   var selectedRadio = function (group){
    var items = group.getMenu().items.filter(item=> item.selected==true )
    return items[0]?items[0].data:'empty';
   }
   var form = {
     lemma: lemma.getValue(),
     pos : selectedDropdown(pos),
     languages: languages.getValue(),
     infoTypes: selectedDropdown(infoTypes),
     translations: translations.getValue(),
     limit: limit.isSelected( ) ,
     endpoints: endpoints.getValue(),
     queryDisplay: queryDisplay.getValue(),
   };
   //  console.log(form);
   var lemmaTag = form.lemma;
   var posTag = 'noun' || form.pos;
   var sourceLanguage = 'Q1860' || form.languages;
   var translationLanguage = 'Q150' || form.translations;
   var endpointLabel = form.endpoints;
   var typeLabel = 'basic' || form.infoTypes;
   var limitSet = form.limit;
   var query = form.queryDisplay;
   console.log(form)

// Identify suitable query
   if ( endpointLabel== 'wikidata' && translationLanguage != "null") {
     typeLabel = lemmaTag? 'translation': 'translationLemma';
   }
   if (endpointLabel== 'dbnary' && typeLabel == 'sensesDefinitionsExamples') {
     typeLabel = 'sensesDefinitions';
   }
   query = queries[endpointLabel][typeLabel];
  
   // Edit suitable query
   val = lemmaTag ? queries[endpointLabel].values : '# no lemma provided';
   
   query = query.replace("VALUESTOBEADDEDHERE", val.replace("book", lemmaTag))
   		.replace("WORD", lemmaTag)
   		.replace("LNGCDE", sourceLanguage)
   		.replaceAll("LNGCDETRG", translationLanguage)
   		.replaceAll("GLWSSA", sourceLanguage)
   		.replace("POSTAGNM", posTag.toLowerCase())
   		.replace("POSTAG", posTag)
      .concat(limitSet?" LIMIT 100":'');

   // Inject query
   console.log(lemma);
   console.log(queryDisplay);
   queryDisplay.setValue(query);
 }

/* ********************************************************************** */
/* OPENS EXTERNAL QUERY SERVICE ***************************************** */
// Current: Wikidata, Dbnary. Broken: lingualibre.
var runQuery = function () {
		var endpointLabel =  endpoints.getValue(),
    queryText = queryDisplay.getValue(),
    queryEncoded = encodeURIComponent(queryText),
    baseEndpointUrl = 
    	endpointLabel == 'wikidata'? 'https://query.wikidata.org/#'
      :'http://kaiko.getalp.org/sparql?default-graph-uri=&query=';
   if (queryText) {
       window.open(baseEndpointUrl.concat(queryEncoded), '_blank');
   }
 }
 $('#lqg-run').on('click',function(){ runQuery(); });
 $('#lqg-generate').on('click',function(){ generateQuery(); });