vocaDB Tooltip

vocaDB developed a useful word search solution to help users have a more convenient language learning.
 
Instead of typing in a dictionary's input field for every search, VocaDB Tooltip enables users to find dictionary information by simply selecting a word or text, either by clicking or highlighting.
 
Our Tooltip uses extraction technology and provides translated definitions. It supports 38 languages.
 
It is powered by VocaDB's Tooltip API, Extraction API, and the APIs of Google Translate and MS Bing.
 
This document is intended for developers who want to build apps with integrated VocaDB Tooltip function.
To begin, you need to set up:
 
The app has been developed using Android Studio 2.1. It uses:
 
  • Android SDK 6.0, API Level 23 for Compiling
  • Android SDK 4.1, API Level 16 as Minimum SDK version
 
vocaDB Tooltip app is compatible with Andriod 4.1 (Jelly Bean) and up.
 
For more information about the configurations of the app, please check the build.gradle file under vocaDBTooltipDic/app.
This section provides steps on how to import VocaDB Tooltip app on your workspace and run.

Importing and Running the Application

To import the project to Android Studio:
  1. Start Android Studio and close any open Android Studio projects.
  2. From the Android Studio menu, select File > New > Import Project.
  3. Select the VocaDBTooltipDic project folder with the AndroidManifest.xml file and click Ok.
  4. Select the destination folder and click Next.
  5. Select the import options and click Finish.
 

Libraries

The app uses the AQuery library for doing asynchronous tasks and manipulating UI elements. Include the .jar file under vocaDBTooltipDic/app/libs.
 
 
Also, add the compile dependency in build.gradle file.
compile files('libs/android-query-full.0.26.8.jar')
 
Other dependency libraries are also included in the file.
 

Project Structure

The app contains one or more modules with source code files and resource files. 
 
 
The adapters folder
 
This folder contains objects for making a View for each item in the data. 
 
The base folder
 
This folder contains classes that other subclasses are derived. 
 
The databasehelpers folder
 
This folder contains classes that manage database creation and version management. 
 
The helpers folder
 
This folder contains classes that provide functionalities for other classes within the app.
 
The models folder
 
This folder contains classes that model the objects associated with creating, reading, updating, and deleting.
 
The parsers folder
 
This folder contains classes that is used for parsing JSON response. 
 
The utilities folder
 
This folder contains classes that define a set of methods which perform common, often re-used functions (static).
 
The views folder
 
This folder contains classes that represent the basic building block of user interface components.
 

The following graphics shows different tooltips with translations:

 


  • Tooltip Word

  • Tooltip Menu

  • Relative Words

  • Relative Idioms

  • Word List Extraction

  • Google Translate

  • MS Bing Translation

Searching for the meaning of a particular word can be easier using a tooltip. Tooltip works by either using double click or long press on a word.

Double Click

By double clicking a word, a popup will show which contains the image, part of speech, sound and meaning of the word.

The following code shows how to make a request using the Tooltip Word API.

/**
 * Method to get Tooltip Word from API
 */
private class TooltipWord extends AsyncTask {
	private String sSource;
	private String sSourceLanguage;
	private String sTargetLanguage;
	private int iReverse;

	public TooltipWord(String _source, String _sourceLang, String _targetLang, int _reverse) {
		this.sSource = _source;
		this.sSourceLanguage = _sourceLang;
		this.sTargetLanguage = _targetLang;
		this.iReverse = _reverse;
	}

	@Override
	protected void onPreExecute() {
		progressBar.setVisibility(View.VISIBLE);
		super.onPreExecute();
	}

	@Override
	protected JSONObject doInBackground(String... args) {
		new ServiceHandler();
		JSONParser jParser = new JSONParser();

		// Construct API Parameters
		int level = prefs.getInt("level", 21);
		nameValuePair = new ArrayList(10);
		nameValuePair.add(new BasicNameValuePair("slang", sSourceLanguage));
		nameValuePair.add(new BasicNameValuePair("tlang", sTargetLanguage));
		nameValuePair.add(new BasicNameValuePair("level",String.valueOf(21)));
		nameValuePair.add(new BasicNameValuePair("q", sSource));
		nameValuePair.add(new BasicNameValuePair("d", "1"));

		// Debug parameters
		AQUtility.debug("TOOLTIP WORD PARAMETERS:", nameValuePair);

		JSONObject json = jParser.getJSONFromUrl(Constants.TOOLTIP_WORD_URL, nameValuePair);
		return json;
	}

	@Override
	protected void onPostExecute(JSONObject json) {
		AQUtility.debug("Tooltip Word Result", json);
		progressBar.setVisibility(View.GONE);
		try {
			int returnType = json.getInt("type");
			if (returnType == 0) {
				speakerImageView.setVisibility(View.GONE);
				txt_meaning.setText("No result(s) found.");
				txt_meaning.setGravity(Gravity.CENTER);
			} else if(returnType > 0) {
				// Check for contents object
				if (!json.getString("contents").equals("0")) {
					JSONObject contents = json.getJSONObject("contents");
					if (!contents.getString("word").equals("0")) {
						JSONObject words = contents.getJSONObject("word");
						txt_voca.setTypeface(Typeface.create(VocaDBUtils.setNationFont(targetLang), Typeface.BOLD));
						txt_voca.setText(words.getString("voca"));
						txt_part.setText(words.getString("part") + " / " + words.getString("level"));
						speakerImageView.setVisibility(View.VISIBLE);
						txt_meaning.setTypeface(Typeface.create(VocaDBUtils.setNationFont(targetLang), Typeface.NORMAL));
						txt_meaning.setText(VocaDBUtils.displayMeaning(words.getString("voca"), words.getString("means"), targetLang));
						txt_meaning.setGravity(Gravity.LEFT);
						if (words.getString("image").equals(""))
							image_imageview.setVisibility(View.GONE);
						else {
							StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
							StrictMode.setThreadPolicy(policy);

							URL url = new URL(Constants.VOCA_URL + "small/" + words.getString("image") + ".jpg");
							Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
							image_imageview.setImageBitmap(bmp);
							image_imageview.setVisibility(View.VISIBLE);
							AQUtility.debug("Image URL", Constants.VOCA_URL + "word/" + words.getString("image") + ".jpg");
						}
					}
				}
			}
		} catch (Exception e){
			e.printStackTrace();
		}

		super.onPostExecute(json);
	}
}

Long Press

Another method to get the meaning of a word/words is by using long press. When you long press on a word, the tooltip menu will show. It contains the search button to make a request to the API, color buttons to highlight the selected text and a copy button if you want to simply copy the text.

You have two options when you long press a word, first is getting the relative words and the second one is extracting the word list.

Relative words means all vocabulary words that is connected or having relation to the selected word. In this example, the selected word is person.

Word list extraction means listing all unique words found in a text or sentence. In this example, the selected text is "To seize a person for legal action".

The following code shows how to make a request using the Tooltip Text API.

/**
 * Method to get Tooltip Text from API
 */
private class TooltipText extends AsyncTask {

	private String sSource;
	private String sSourceLanguage;
	private String sTargetLanguage;
	private int iReverse;

	public TooltipText(String _source, String _sourceLang, String _targetLang, int _reverse) {
		this.sSource = _source;
		this.sSourceLanguage = _sourceLang;
		this.sTargetLanguage = _targetLang;
		this.iReverse = _reverse;
	}

	@Override
	protected JSONObject doInBackground(String... args) {
		new ServiceHandler();
		JSONParser jParser = new JSONParser();

		// Construct API Parameters
		int level = prefs.getInt("level", 21);
		nameValuePair = new ArrayList(10);
		nameValuePair.add(new BasicNameValuePair("slang", sSourceLanguage));
		nameValuePair.add(new BasicNameValuePair("tlang", sTargetLanguage));
		nameValuePair.add(new BasicNameValuePair("level",String.valueOf(21)));
		nameValuePair.add(new BasicNameValuePair("q", sSource));
		nameValuePair.add(new BasicNameValuePair("d", "1"));

		String url = "";
		// Check query if contains white space
		if (VocaDBUtils.containsWhitespace(sSource)) {
			AQUtility.debug("Contains white space");
			url = Constants.TOOLTIP_TEXT_URL;
		} else {
			AQUtility.debug("No white space");
			url = Constants.TOOLTIP_RELATIVE_WORD_URL;
		}

		// Debug parameters
		AQUtility.debug("TOOLTIP TEXT PARAMETERS:", nameValuePair);

		JSONObject json = jParser.getJSONFromUrl(url, nameValuePair);
		return json;
	}

	@Override
	protected void onPostExecute(JSONObject json) {
		AQUtility.debug("Tooltip Text Result", json);
		try {
			int returnType = json.getInt("type");
			if (returnType == 0) {
				txt_no_result.setVisibility(View.VISIBLE);
				wordButton.setVisibility(View.GONE);
				wordlistView.setVisibility(View.GONE);
				idiomButton.setVisibility(View.GONE);
				idiomListView.setVisibility(View.GONE);
			} else if (returnType > 0) {
				// Check if contents is not empty
				if (json.getString("contents").equals("0") || json.getString("contents").equals("")) {
					txt_no_result.setVisibility(View.VISIBLE);
					wordButton.setVisibility(View.GONE);
					wordlistView.setVisibility(View.GONE);
					idiomButton.setVisibility(View.GONE);
					idiomListView.setVisibility(View.GONE);
				} else {
					JSONObject data = (json.getJSONObject("contents")).getJSONObject("words");
					// Check if relative idioms json exists
					if (data.has("relative_words") && !data.getString("relative_words").equals("")) {
						JSONArray relative_words = data.getJSONArray("relative_words");
						for (int i = 0; i < relative_words.length(); i++) {
							JSONObject item = relative_words.getJSONObject(i);
							Words w1 = new Words(item.getString("voca"), " - " + item.getString("part") + " / ",
									item.getString("level"), item.getString("means"), item.getInt("ilevel"));
							if (wordNotExists(item.getString("voca"))) {
								arrWordlist.add(w1);
							}
						}
						wordButton.setVisibility(View.VISIBLE);
						wordlistView.setVisibility(View.VISIBLE);
						idiomListView.setVisibility(View.GONE);
					} else {
						wordButton.setVisibility(View.GONE);
						wordlistView.setVisibility(View.GONE);
						idiomListView.setVisibility(View.VISIBLE);
						AQUtility.debug("No relative_words");
					}

					JSONObject idioms = (json.getJSONObject("contents")).getJSONObject("idioms");
					// Check if relative idioms json exists
					if (idioms.has("relative_idioms") && !idioms.getString("relative_idioms").equals("")) {
						JSONArray relative_idioms = idioms.getJSONArray("relative_idioms");
						for (int i = 0; i < relative_idioms.length(); i++) {
							JSONObject item = relative_idioms.getJSONObject(i);
							Idioms i1 = new Idioms(item.getString("idiom"), item.getString("level"), item.getString("means"), item.getInt("ilevel"));
							if (vocaDBUtils.idiomNotExists(item.getString("idiom"), arrIdiomList)) {
								arrIdiomList.add(i1);
							}
						}
					} else {
						idiomButton.setVisibility(View.GONE);
						idiomListView.setVisibility(View.GONE);
						AQUtility.debug("No relative_idioms");
					}
				}
			}
		} catch (Exception e){
			e.printStackTrace();
		}

		if(arrWordlist.size()>0) {
			wordButton.setVisibility(View.VISIBLE);
			wordButton.setText("W ("+arrWordlist.size()+")");
			wordDataAdapter.notifyDataSetChanged();
		} else
			wordButton.setVisibility(View.GONE);

		if(arrIdiomList.size()>0) {
			idiomButton.setVisibility(View.VISIBLE);
			idiomButton.setText("I ("+arrIdiomList.size()+")");
			idiomsAdapter.notifyDataSetChanged();
		} else
			idiomButton.setVisibility(View.GONE);

		// Check if both relative words and idioms is empty
		if (arrWordlist.size() == 0 && arrIdiomList.size() == 0)
			txt_no_result.setVisibility(View.VISIBLE);

		super.onPostExecute(json);
	}
}
The Tooltip API allows developers to make requests and get responses in JSON format.
 

API Key

The API key for this app is defined in app/your_app_package_name/base/Constants.java.
// API Keys
public static final String TOOLTIP_API_KEY = "YOUR_API_KEY";
 

Apply for API Key

URLs

All URLs are defined also in app/your_app_package_name/base/Constants.java.
// API URLs
public static final String BASE_URL = "http://www.vocadb.com/v2_dic/";
public static final String TOOLTIP_WORD_URL = BASE_URL + "api_dic_tooltip_word.php";
public static final String TOOLTIP_RELATIVE_WORD_URL = BASE_URL + "api_dic_relative_word.php";
public static final String TOOLTIP_TEXT_URL = BASE_URL + "api_dic_tooltip_text.php";
 
It is important to add the custom header named x-voca-apikey, along with your API key.
httpPost.addHeader("x-voca-apikey", YOUR_API_KEY);
 
Also, don't forget to add UTF-8 encoding type to the request.
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

Parameters

The parameters needed to make a request are:
  • slang - Source language reference
  • tlang - Target language reference
  • level - User-defined level for extraction
  • q - Searched word
  • d - Direction, 1 = mobile

Sample Request

Tooltip Word

http://www.vocadb.com/v2_dic/api_dic_tooltip_word.php?slang=en&tlang=en&level=21&q=person&d=1

Tooltip Relative Words

http://www.vocadb.com/v2_dic/pi_dic_relative_word.php?slang=en&tlang=en&level=21&q=person&d=1

Tooltip Text

http://www.vocadb.com/v2_dic/api_dic_tooltip_text.php?slang=en&tlang=en&level=21&q=to+seize+a+person+for+legal+action&d=1
 
For more information about the API, click the button below:
 

Tooltip Word API Documentation

Tooltip Relative Word Documentation

Tooltip Text API Documentation


 

© vocaDB 2016. All rights reserved.