Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Look up English words from Merriam Learner's Dictionary #51

Open
gijigae opened this issue Dec 1, 2020 · 1 comment
Open

Look up English words from Merriam Learner's Dictionary #51

gijigae opened this issue Dec 1, 2020 · 1 comment

Comments

@gijigae
Copy link

gijigae commented Dec 1, 2020

✂️ Copy of your #42SmartBlock from Roam

  • #42SmartBlock zz_Merriam
    
    • <%NOBLOCKOUTPUT%><%JAVASCRIPTASYNC:
      ```javascript
      // Default to the page title
      var wordToLookUp = encodeURIComponent(prompt('Enter word to lookup.',document.title));
      // Get your API from https://dictionaryapi.com/register/index
      var apiKey = 'Your API'
      
      var url = "https://dictionaryapi.com/api/v3/references/learners/json/" + wordToLookUp + "/?key="+apiKey;
      var dicUrl = "https://learnersdictionary.com/definition/" + wordToLookUp
      var audio = "https://media.merriam-webster.com/audio/prons/en/us/mp3/"
      var responseText = $.ajax({url:url, type:"GET", async:false,}).responseText; 
      var data = JSON.parse(responseText);
      
      for(idx in data) {
        var shortDef = data[idx].meta["app-shortdef"]
        var ipa = "na"
        if (data[idx].hwi["prs"] != undefined){
        	var prs = data[idx].hwi["prs"]
          ipa = prs[0].ipa
      
          if (prs[0].sound != undefined){
            var sound = prs[0].sound["audio"]
            var file = sound + ".mp3"
            var subDir = sound[0] + "/"
            var audioPath = audio + subDir + file
            var embed = "{{audio: " + audioPath + "}}"
          }
        }
        // Print the speech of word along with ipa pronunciation
        await roam42.smartBlocks.activeWorkflow.outputAdditionalBlock(
          shortDef["fl"] + " | / " + ipa + " / "
        );
      
        // Print URL of Merriam Learner's dictionary
        await roam42.smartBlocks.activeWorkflow.outputAdditionalBlock(dicUrl);
      
        // Definition of the word
        await roam42.smartBlocks.activeWorkflow.outputAdditionalBlock(shortDef["def"]);
      
        // Embed audio for the word
        if (prs[0].sound != undefined){
          await roam42.smartBlocks.activeWorkflow.outputAdditionalBlock(embed);
        }
      
        // Fetch only the first record	
        break;
      }
      return '';```
      %>
      

📋 Describe the SmartBlock

Look up English words from Merriam Learner's dictionary. Page title is the default word to look up .
It extracts 5 information from the dictionary.

  1. Speech of word
  2. IPA Pronunciation
  3. URL to dictionary
  4. Definition
  5. Audio Pronunciation

✅ Describe any prerequisites or dependencies that are required for this SmartBlock

API Key from Merriam Dictionary is required and the same can be obtained from https://dictionaryapi.com/register/index. There are multiple API Keys and this code works for "Learners Dictionary".

image

📷 Screenshot of your #42SmartBlock workflow/template from Roam

merriam

💡 Additional Info

2020-12-02_07-19-41 (1)

@gijigae
Copy link
Author

gijigae commented Dec 20, 2020

Updated the code to accommodate the changes made in Roam42 (2020-12-19b,Grogu).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant