[Free] Dictionary Extension : Developing Dictionary apps made Easy!

Ur wish man !

I think @TIMAI2 asked you that if he enters English as the language param, and स्वच्छ as the word, will your extension return clean or give an error?

It will return word not found

As an error

Ok, thanks

thank you for your contribution...
please follow the naming conventions and adjust your extension accordingly

Unbenannt

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

1 Like

Thank you @Taifun I will change the spellings soon !

Extension is now free and latest aix's will be uploaded soon

3 Likes

Now a great extension :ok_hand:

1 Like

Thank you @Kumaraswamy

1 Like

Can you add the parameter of "Spell of Word (Audio)" to the extension?

1 Like

Yes I can do it @Kumaraswamy

Hi, Rudra
Important Message
Can I use your extension for app submitted to MIT App Inventor Appathon for Good 2021
Please Reply.
Regards,
B.S.S.SRIKAR
Thank You

2 Likes

I posted this extension from another Account, now I abandoned it :laughing:

Anyways :

I, Anshuman aka Rudra, allow to republish the extension as part of the project file for the purposes of the MIT App Inventor Appathon 2021.

1 Like

Thank You So Much

1 Like

Best of luck for Appathon, waiting to see your app :upside_down_face:

1 Like

Thank You...

1 Like

Can You Give The example .aia file for this extension
because it is not working for this is my code:

and My Design Section

Sure ! :grin:
wait a minute :relaxed:

1 Like

Can You please send the .aia file

dictionary.aia (12.1 KB)

Also, I thought to make it Open source so :

package com.kab.Dictionary;

import android.os.AsyncTask;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@DesignerComponent(
        version = 1,
        category = ComponentCategory.EXTENSION,
        nonVisible = true
)

@SimpleObject(external = true)
public class Translator extends AndroidNonvisibleComponent {

    public Translator(ComponentContainer container) {
        super(container.$form());
    }

    @SimpleFunction
    public void Translate(String sourceLanguage, String textToTranslate) {
        API api = new API(sourceLanguage,textToTranslate);
        api.setTranslateListener(new API.TranslateListener() {
            @Override
            public void onSuccess(List<String> Synonyms, List<String> Definition, List<String> partsOfSpeech, List<String> Sentence) {
                List<String> synonyms = new ArrayList<String>();
                String synonym = "";
                for (i : Synonyms) {
                synonym = i.replace("(","");
                synonym = synonym.replace(")","");
                synonyms.add(synonym);
                }
                List<String> definitions = new ArrayList<String>();
                String definition = "";
                for (x : Definition) {
                definition = x.replace("(","");
                definition = synonym.replace(")","");
                definitions.add(definition);
                }
                List<String> partsofspeech = new ArrayList<String>();
                String partOfSpeech = "";
                for (v : partsOfSpeech) {
                partOfSpeech = v.replace("(","");
                partOfSpeech = partOfSpeech.replace(")","");
                partsofspeech.add(partOfSpeech);
                }
                List<String> sentences = new ArrayList<String>();
                String sentence = "";
                for (c : Sentence) {
                sentence = c.replace("(","");
                sentence = sentence.replace(")","");
                sentences.add(synonym)
                }
                GotResponse(synonyms, definitions, partsofspeech, sentences);
            }

            @Override
            public void onFailure(String ErrorText) {
                Failed(ErrorText);
            }
        });
    }

    @SimpleEvent
    public void GotResponse(List<String> Synonyms, List<String> Definitions, List<String> PartsOfSpeech, List<String> Sentences) {
        EventDispatcher.dispatchEvent(this, "GotResponse", Synonyms, Definitions, PartsOfSpeech, Sentences);
    }

    @SimpleEvent
    public void Failed(String error) {
        EventDispatcher.dispatchEvent(this, "Failed", error);
    }


    private static class API {

        public API(String langFrom,String text) {
            Async async = new Async();
            async.execute(langFrom,text);
        }


        class Async extends AsyncTask<String, String, String> {
            @Override
            protected String doInBackground(String... strings) {
                String resp = null;
                final String url;
                try {
                    url = "https://api.dictionaryapi.dev/api/v2/entries/" + strings[0] + "/" + URLEncoder.encode(strings[1], "UTF-8");
                    URL obj = new URL(url);
                    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                    con.setRequestProperty("User-Agent", "Mozilla/5.0");
                    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String inputLine;
                    StringBuffer response = new StringBuffer();
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                    resp = response.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return resp;
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(String result) {
                String temp = "";

                if (result == null) {
                    listener.onFailure("Network Error");
                } else {
                    try {
                        JSONArray mainData = new JSONArray(result);
                        JSONObject mainObject = (JSONObject) mainData.get(0);
                        JSONArray meanings = (JSONArray) mainObject.get("meanings");

                        List<String> partsOfSpeech = new ArrayList<>();
                        List<String> definitions = new ArrayList<>();
                        List<String> synonyms = new ArrayList<>();
                        List<String> sentences = new ArrayList<>();

                        for (int i = 0; i < meanings.length(); i++) {
                            JSONObject pos = meanings.getJSONObject(i);
                            partsOfSpeech.add(pos.get("partOfSpeech").toString());

                            JSONArray tempDef = pos.getJSONArray("definitions");

                            for (int j = 0;j < tempDef.length(); j++) {
                                JSONObject object = (JSONObject) tempDef.get(j);
                                if (object.has("definition")) {
                                    String def = object.get("definition").toString();
                                    definitions.add(def);
                                }
                                if (object.has("synonyms")) {
                                    JSONArray syn = (JSONArray) object.get("synonyms");
                                    for (int k = 0; k < syn.length(); k++) {
                                        synonyms.add(syn.get(k).toString());
                                    }
                                }
                                if (object.has("example")) {
                                    String eg = object.get("example").toString();
                                    sentences.add(eg);
                                }
                            }
                        }

                        listener.onSuccess(synonyms, definitions, partsOfSpeech, sentences);

                    } catch (Exception e) {
                        listener.onFailure(e.getLocalizedMessage());
                    }
                }
                super.onPostExecute(result);
            }

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);
            }

            @Override
            protected void onCancelled(String s) {
                super.onCancelled(s);
            }
        }

        private TranslateListener listener;

        public void setTranslateListener(TranslateListener listener) {
            this.listener = listener;
        }

        private interface TranslateListener {
            public void onSuccess(List<String> Synonyms, List<String> Definition, List<String> partsOfSpeech, List<String> Sentence);

            public void onFailure(String ErrorText);
        }

    }


}
2 Likes