Skip to content

Instantly share code, notes, and snippets.

@MarketingPip
Last active February 12, 2024 16:11
Show Gist options
  • Select an option

  • Save MarketingPip/4547daa2b745d620e63325c175c8d5f4 to your computer and use it in GitHub Desktop.

Select an option

Save MarketingPip/4547daa2b745d620e63325c175c8d5f4 to your computer and use it in GitHub Desktop.

Revisions

  1. MarketingPip revised this gist Nov 16, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Spencer_Help_Request.js
    Original file line number Diff line number Diff line change
    @@ -114,7 +114,7 @@ function mapToSchema(results) {
    const wordInfo = {
    word: result.word.value,
    pos: result.grammar.value.split('|$|'),
    type:null,
    tags:null,
    lemma:null,
    wikidata: result.LexIDs.value.split('|$|').map(result => result.split("/").pop()),
    forms: null,
    @@ -142,7 +142,7 @@ function mapToSchema(results) {
    const type = getKeyByValue(wordInfo.forms, wordInfo.word)
    const penn = verbFormsMapping[type]

    wordInfo.type = {wikidata:type, penn:penn, compromise:compromiseMapping[penn]}
    wordInfo.tags = {wikidata:type, penn:penn, compromise:compromiseMapping[penn]}
    // wordInfo.forms.filter(obj => obj.intent === "2017-07-12T14:41:15");

    mappedResults.words.push(wordInfo);
  2. MarketingPip revised this gist Nov 16, 2023. 1 changed file with 63 additions and 3 deletions.
    66 changes: 63 additions & 3 deletions Spencer_Help_Request.js
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,63 @@ async function getWordInfo(words, langCode = 'en') {

    const getKeyByValue = (obj, value) => Object.keys(obj).find(key => obj[key] === value);

    const compromiseMapping = {
    CC: 'Conjunction',
    CD: 'Cardinal',
    DT: 'Determiner',
    EX: 'Preposition', //Existential there
    FW: 'Expression',
    IN: 'Preposition',
    JJ: 'Adjective',
    JJR: 'Comparative',
    JJS: 'Superlative',
    MD: 'Modal',
    NN: 'Noun',
    NNS: 'Plural',
    NNP: 'Singular',
    NNPS: ' Plural',
    POS: 'Possessive',
    PRP: 'Pronoun',
    RB: 'Adverb',
    RBR: 'Comparative',
    RBS: 'Superlative',
    RP: 'PhrasalVerb',
    PDT: 'Determiner',
    SYM: 'Expression',
    TO: 'Conjunction',
    UH: 'Expression',
    VB: 'Verb',
    VBD: 'PastTense',
    VBG: 'Gerund',
    VBN: 'Participle', // past participle
    VBP: 'PresentTense', // non-3rd person singular present
    VBZ: 'PresentTense', // 3rd person singular present
    'PRP$': 'Pronoun',
    'WP$': 'Possessive',
    WDT: 'Determiner',
    WP: 'Pronoun',
    WRB: 'Adverb',
    }

    const verbFormsMapping = {
    "simple past": "VBD",
    "past participle in english": "VBN",
    "present participle": "VBG",
    "plural": "NNS",
    "singular": "NNP",
    "third person": "VBZ",
    "first person singular": "VBP",
    "second person singular": "VB",
    "third person plural": "VBP",
    "infinitive": "VB",
    "present": "VBP",
    "past": "VBD",
    "gerund": "VBG",
    "positive": "JJ",
    "comparative": "JJR",
    "superlative": "JJS",
    };

    function mapToSchema(results) {
    results = results.results.bindings;
    console.log(results)//
    @@ -82,7 +139,10 @@ function mapToSchema(results) {

    wordInfo.forms = postypes;

    wordInfo.type = getKeyByValue(wordInfo.forms, wordInfo.word)
    const type = getKeyByValue(wordInfo.forms, wordInfo.word)
    const penn = verbFormsMapping[type]

    wordInfo.type = {wikidata:type, penn:penn, compromise:compromiseMapping[penn]}
    // wordInfo.forms.filter(obj => obj.intent === "2017-07-12T14:41:15");

    mappedResults.words.push(wordInfo);
    @@ -92,5 +152,5 @@ function mapToSchema(results) {
    }

    // Example usage:
    const wordsToQuery = ['hated', 'hate'];
    getWordInfo(wordsToQuery);
    const wordsToQuery = ['hated', 'hate', 'going', 'go'];
    getWordInfo(wordsToQuery);
  3. MarketingPip revised this gist Nov 16, 2023. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions Spencer_Help_Request.js
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,8 @@ async function getWordInfo(words, langCode = 'en') {
    }
    }

    const getKeyByValue = (obj, value) => Object.keys(obj).find(key => obj[key] === value);

    function mapToSchema(results) {
    results = results.results.bindings;
    console.log(results)//
    @@ -55,6 +57,7 @@ function mapToSchema(results) {
    const wordInfo = {
    word: result.word.value,
    pos: result.grammar.value.split('|$|'),
    type:null,
    lemma:null,
    wikidata: result.LexIDs.value.split('|$|').map(result => result.split("/").pop()),
    forms: null,
    @@ -78,12 +81,16 @@ function mapToSchema(results) {
    }

    wordInfo.forms = postypes;

    wordInfo.type = getKeyByValue(wordInfo.forms, wordInfo.word)
    // wordInfo.forms.filter(obj => obj.intent === "2017-07-12T14:41:15");

    mappedResults.words.push(wordInfo);
    });

    console.log(mappedResults);
    }

    // Example usage:
    const wordsToQuery = ['hated'];
    getWordInfo(wordsToQuery);
    const wordsToQuery = ['hated', 'hate'];
    getWordInfo(wordsToQuery);
  4. MarketingPip revised this gist Nov 16, 2023. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions Spencer_Help_Request.js
    Original file line number Diff line number Diff line change
    @@ -14,20 +14,23 @@ async function getWordInfo(words, langCode = 'en') {
    dct:language wd:Q1860 ;
    wikibase:lemma ?lemma ;
    ontolex:lexicalForm ?form.
    OPTIONAL {
    ?l wikibase:lexicalCategory ?cat .
    ?cat rdfs:label ?category. FILTER(LANG(?category) = "${langCode}").
    }
    ?l ontolex:lexicalForm ?forms .
    ?forms wikibase:grammaticalFeature ?features.
    ?features rdfs:label ?feat2. FILTER(LANG(?feat2) = "${langCode}").
    ?forms ontolex:representation ?usagewords .
    ?form ontolex:representation ?word .
    ?l ontolex:sense ?sense .
    ?sense skos:definition ?gloss.
    FILTER(LANG(?gloss) = "${langCode}")
    }
    GROUP BY ?word ?lemma ?category`;
    GROUP BY ?word ?lemma`;

    const headers = { 'Accept': 'application/sparql-results+json' };
    const fullUrl = endpointUrl + '?query=' + encodeURIComponent(sparqlQuery);
    @@ -51,7 +54,7 @@ function mapToSchema(results) {
    results.forEach(result => {
    const wordInfo = {
    word: result.word.value,
    pos: result.grammar.value,
    pos: result.grammar.value.split('|$|'),
    lemma:null,
    wikidata: result.LexIDs.value.split('|$|').map(result => result.split("/").pop()),
    forms: null,
    @@ -70,7 +73,8 @@ function mapToSchema(results) {
    wordInfo.senses = [...senses] || [];

    for (let i = 0; i < uses.length; i++) {
    postypes[[uses[i]]] = sameMeaning[i]; // Need help here spencer - theses arent mapped right. Assuming we need to change SPARQL query?
    postypes[[uses[i]]] = sameMeaning[i] || sameMeaning[sameMeaning.length - 1];
    // Need help here spencer - theses arent mapped right. Assuming we need to change SPARQL query?
    }

    wordInfo.forms = postypes;
    @@ -81,8 +85,5 @@ function mapToSchema(results) {
    }

    // Example usage:

    // // Example usage:
    //// const wordsToQuery = ['information']; // maps properly due to only 2 results (singular / plural)
    const wordsToQuery = ['hate', 'going', 'go']; // where shit goes wrong. lol
    const wordsToQuery = ['hated'];
    getWordInfo(wordsToQuery);
  5. MarketingPip revised this gist Nov 16, 2023. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions Spencer_Help_Request.js
    Original file line number Diff line number Diff line change
    @@ -14,23 +14,20 @@ async function getWordInfo(words, langCode = 'en') {
    dct:language wd:Q1860 ;
    wikibase:lemma ?lemma ;
    ontolex:lexicalForm ?form.
    OPTIONAL {
    ?l wikibase:lexicalCategory ?cat .
    ?cat rdfs:label ?category. FILTER(LANG(?category) = "${langCode}").
    }
    ?l ontolex:lexicalForm ?forms .
    ?forms wikibase:grammaticalFeature ?features.
    ?features rdfs:label ?feat2. FILTER(LANG(?feat2) = "${langCode}").
    ?forms ontolex:representation ?usagewords .
    ?form ontolex:representation ?word .
    ?l ontolex:sense ?sense .
    ?sense skos:definition ?gloss.
    FILTER(LANG(?gloss) = "${langCode}")
    }
    GROUP BY ?word ?lemma`;
    GROUP BY ?word ?lemma ?category`;

    const headers = { 'Accept': 'application/sparql-results+json' };
    const fullUrl = endpointUrl + '?query=' + encodeURIComponent(sparqlQuery);
    @@ -54,7 +51,7 @@ function mapToSchema(results) {
    results.forEach(result => {
    const wordInfo = {
    word: result.word.value,
    pos: result.grammar.value.split('|$|'),
    pos: result.grammar.value,
    lemma:null,
    wikidata: result.LexIDs.value.split('|$|').map(result => result.split("/").pop()),
    forms: null,
    @@ -73,8 +70,7 @@ function mapToSchema(results) {
    wordInfo.senses = [...senses] || [];

    for (let i = 0; i < uses.length; i++) {
    postypes[[uses[i]]] = sameMeaning[i] || sameMeaning[sameMeaning.length - 1];
    // Need help here spencer - theses arent mapped right. Assuming we need to change SPARQL query?
    postypes[[uses[i]]] = sameMeaning[i]; // Need help here spencer - theses arent mapped right. Assuming we need to change SPARQL query?
    }

    wordInfo.forms = postypes;
    @@ -85,5 +81,8 @@ function mapToSchema(results) {
    }

    // Example usage:
    const wordsToQuery = ['love'];

    // // Example usage:
    //// const wordsToQuery = ['information']; // maps properly due to only 2 results (singular / plural)
    const wordsToQuery = ['hate', 'going', 'go']; // where shit goes wrong. lol
    getWordInfo(wordsToQuery);
  6. MarketingPip revised this gist Nov 16, 2023. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions Spencer_Help_Request.js
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ async function getWordInfo(words, langCode = 'en') {
    ?sense skos:definition ?gloss.
    FILTER(LANG(?gloss) = "${langCode}")
    }
    GROUP BY ?word ?lemma ?category`;
    GROUP BY ?word ?lemma`;

    const headers = { 'Accept': 'application/sparql-results+json' };
    const fullUrl = endpointUrl + '?query=' + encodeURIComponent(sparqlQuery);
    @@ -54,7 +54,7 @@ function mapToSchema(results) {
    results.forEach(result => {
    const wordInfo = {
    word: result.word.value,
    pos: result.grammar.value,
    pos: result.grammar.value.split('|$|'),
    lemma:null,
    wikidata: result.LexIDs.value.split('|$|').map(result => result.split("/").pop()),
    forms: null,
    @@ -73,7 +73,8 @@ function mapToSchema(results) {
    wordInfo.senses = [...senses] || [];

    for (let i = 0; i < uses.length; i++) {
    postypes[[uses[i]]] = sameMeaning[i]; // Need help here spencer - theses arent mapped right. Assuming we need to change SPARQL query?
    postypes[[uses[i]]] = sameMeaning[i] || sameMeaning[sameMeaning.length - 1];
    // Need help here spencer - theses arent mapped right. Assuming we need to change SPARQL query?
    }

    wordInfo.forms = postypes;
    @@ -84,8 +85,5 @@ function mapToSchema(results) {
    }

    // Example usage:

    // // Example usage:
    //// const wordsToQuery = ['information']; // maps properly due to only 2 results (singular / plural)
    const wordsToQuery = ['hate', 'going', 'go']; // where shit goes wrong. lol
    const wordsToQuery = ['love'];
    getWordInfo(wordsToQuery);
  7. MarketingPip revised this gist Nov 15, 2023. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Spencer_Help_Request.js
    Original file line number Diff line number Diff line change
    @@ -84,5 +84,8 @@ function mapToSchema(results) {
    }

    // Example usage:
    const wordsToQuery = ['hate', 'going', 'go'];

    // // Example usage:
    //// const wordsToQuery = ['information']; // maps properly due to only 2 results (singular / plural)
    const wordsToQuery = ['hate', 'going', 'go']; // where shit goes wrong. lol
    getWordInfo(wordsToQuery);
  8. MarketingPip created this gist Nov 15, 2023.
    88 changes: 88 additions & 0 deletions Spencer_Help_Request.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    async function getWordInfo(words, langCode = 'en') {
    const endpointUrl = 'https://query.wikidata.org/sparql';

    const sparqlQuery = `
    SELECT ?word ?lemma (GROUP_CONCAT(DISTINCT ?category; separator="|\$|") AS ?grammar)
    (GROUP_CONCAT(DISTINCT ?forms; separator="|\$|") AS ?LexIDs)
    (GROUP_CONCAT(DISTINCT ?gloss; separator="|\$|") AS ?Senses)
    (GROUP_CONCAT(DISTINCT ?feat2; separator="|\$|") AS ?Uses)
    (GROUP_CONCAT(DISTINCT ?usagewords; separator="|\$|") AS ?SameMeaning)
    WHERE {
    VALUES ?word {${words.map(word => `'${word}'@${langCode}`).join(' ')}}
    ?l a ontolex:LexicalEntry ;
    dct:language wd:Q1860 ;
    wikibase:lemma ?lemma ;
    ontolex:lexicalForm ?form.
    OPTIONAL {
    ?l wikibase:lexicalCategory ?cat .
    ?cat rdfs:label ?category. FILTER(LANG(?category) = "${langCode}").
    }
    ?l ontolex:lexicalForm ?forms .
    ?forms wikibase:grammaticalFeature ?features.
    ?features rdfs:label ?feat2. FILTER(LANG(?feat2) = "${langCode}").
    ?forms ontolex:representation ?usagewords .
    ?form ontolex:representation ?word .
    ?l ontolex:sense ?sense .
    ?sense skos:definition ?gloss.
    FILTER(LANG(?gloss) = "${langCode}")
    }
    GROUP BY ?word ?lemma ?category`;

    const headers = { 'Accept': 'application/sparql-results+json' };
    const fullUrl = endpointUrl + '?query=' + encodeURIComponent(sparqlQuery);

    try {
    const response = await fetch(fullUrl, { headers });
    const results = await response.json();
    mapToSchema(results);
    } catch (error) {
    console.error('Error fetching data:', error);
    }
    }

    function mapToSchema(results) {
    results = results.results.bindings;
    console.log(results)//
    const mappedResults = {
    words: []
    };

    results.forEach(result => {
    const wordInfo = {
    word: result.word.value,
    pos: result.grammar.value,
    lemma:null,
    wikidata: result.LexIDs.value.split('|$|').map(result => result.split("/").pop()),
    forms: null,
    senses: []
    };
    //
    if(wordInfo.word.toLowerCase() != result.lemma.value.toLowerCase()){
    wordInfo.lemma = result.lemma.value
    }

    const senses = result.Senses.value.split('|$|');
    const uses = result.Uses.value.split('|$|');
    const sameMeaning = result.SameMeaning.value.split('|$|');
    const postypes = {};

    wordInfo.senses = [...senses] || [];

    for (let i = 0; i < uses.length; i++) {
    postypes[[uses[i]]] = sameMeaning[i]; // Need help here spencer - theses arent mapped right. Assuming we need to change SPARQL query?
    }

    wordInfo.forms = postypes;
    mappedResults.words.push(wordInfo);
    });

    console.log(mappedResults);
    }

    // Example usage:
    const wordsToQuery = ['hate', 'going', 'go'];
    getWordInfo(wordsToQuery);