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

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
Is 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);
        }

    }


} 

is this the code for the extension .aix

Yes

:grin:

1 Like

@TIMAI2 which means, the sock puppet account Rudra_Software_Corpo should be removed?

Taifun

U can do so :upside_down_face: just make sure posts arent lost

Hi, @Taifun can you explain what is 'sock puppet account'

Taifun

1 Like

Thank You :slightly_smiling_face: