In this article, we can learn how to check similar words and sentences using Text Embedding feature of Huawei ML Kit. This Huawei ML Kit provides Text Embedding feature which helps to get matching vector value of words or sentences. Using this feature, we can improve our research based on the result. It provides similarity between two words or sentences and similar words of a particular word searched. We can also improve searching and browsing efficiency using after getting results related to search text.
Use Cases
This service can be used in text search scenarios. A user can enter a keyword in your news app to return hot news related to the word, improving the search and browsing efficiency.
Precautions
Text embedding depends on the on-cloud API for recognition. During commissioning and usage, ensure that the device can access the Internet.
Requirements
Any operating system (MacOS, Linux and Windows).
Must have a Huawei phone with HMS 4.0.0.300 or later.
Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 and above installed.
Minimum API Level 21 is required.
Required EMUI 9.0.0 and later version devices.
How to integrate HMS Dependencies
First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.
Create a project in android studio, refer Creating an Android Studio Project.
Generate a SHA-256 certificate fingerprint.
To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > Tasks > android, and then click signingReport, as follows.
Note: Project Name depends on the user created name.
Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.
- Enter SHA-256 certificate fingerprint and click Save button, as follows.
- Click Manage APIs tab and enable ML Kit.
- Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
- Add the below plugin and dependencies in build.gradle(Module) file.
apply plugin: 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.5.0.300'
// ML Kit Text Embedding
implementation 'com.huawei.hms:ml-nlp-textembedding:2.0.4.300'
Now Sync the gradle.
Add the required permission to the AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Let us move to development
I have created a project on Android studio with empty activity let us start coding.
In the MainActivity.kt we can find the business logic for buttons.
class MainActivity : AppCompatActivity() {
var wordSimilar: Button? = null
var sentenceSimilar:Button? = null
var similarWords:Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
MLApplication.getInstance().apiKey = getString(R.string.api_key)
wordSimilar = findViewById(R.id.word_similar)
sentenceSimilar = findViewById(R.id.sentence_similar)
similarWords = findViewById(R.id.similar_words)
wordSimilar!!.setOnClickListener(View.OnClickListener {
startActivity(Intent(this@MainActivity, WordSimilarActivity::class.java)) })
sentenceSimilar!!.setOnClickListener(View.OnClickListener {
startActivity(Intent(this@MainActivity, SentenceSimilarActivity::class.java)) })
similarWords!!.setOnClickListener(View.OnClickListener {
startActivity(Intent(this@MainActivity, SimilarWordsActivity::class.java)) })
}
}
In the WordSimilarActivity.kt we can find the business logic for similar words.
class WordSimilarActivity : AppCompatActivity() {
private var analyzer: MLTextEmbeddingAnalyzer? = null
private var checkWordSimilarity: Button? = null
var resultTextView: TextView? = null
var similarWord1: EditText? = null
var similarWord2:EditText? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_word_similar)
resultTextView = findViewById(R.id.similar_word_value)
checkWordSimilarity = findViewById(R.id.check_word_similarity)
similarWord1 = findViewById(R.id.similar_word1)
similarWord2 = findViewById(R.id.similar_word2)
checkWordSimilarity!!.setOnClickListener(View.OnClickListener { getWordSimillarityApi() })
analyzer = UtilForAnalyzer.getAnalyzer()
}
private fun getWordSimillarityApi() {
val wordsSimilarityTask = analyzer!!.analyseWordsSimilarity(
similarWord1!!.text.toString(), similarWord2!!.text.toString() )
wordsSimilarityTask.addOnSuccessListener { wordsSimilarity ->
resultTextView!!.text = "The similarity value is $wordsSimilarity"
}.addOnFailureListener { e -> onFailure(e) }
}
private fun onFailure(e: Exception?) {
if (e is MLTextEmbeddingException) {
val embeddingException = e
embeddingException.errCode
embeddingException.message
} else {
}
}
}
Create a class for analyzer UtilForAnalyzer.kt.
class UtilForAnalyzer {
companion object{
private var analyzer: MLTextEmbeddingAnalyzer? = null
var setting: MLTextEmbeddingSetting? = null
fun getAnalyzer(): MLTextEmbeddingAnalyzer? {
if (analyzer == null) {
setting = MLTextEmbeddingSetting.Factory()
.setLanguage(MLTextEmbeddingSetting.LANGUAGE_EN)
.create()
analyzer = MLTextEmbeddingAnalyzerFactory.getInstance().getMLTextEmbeddingAnalyzer(setting)
}
return analyzer
}
}
}
In the SentenceSimilarActivity.kt we can find the business logic for similar sentence.
class SentenceSimilarActivity : AppCompatActivity() {
private var analyzer: MLTextEmbeddingAnalyzer? = null
private var checkSentenceSimilarity: Button? = null
private var sentence_1: EditText? = null
private var sentence_2: EditText? = null
var resultTextView: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sentence_similar)
sentence_1 = findViewById(R.id.sentence1)
sentence_2 = findViewById(R.id.sentence2)
resultTextView = findViewById(R.id.similar_word_value)
checkSentenceSimilarity = findViewById(R.id.check_sentence_similarity)
checkSentenceSimilarity!!.setOnClickListener(View.OnClickListener { getSentenceSimilarityApi() })
analyzer = UtilForAnalyzer.getAnalyzer()
}
private fun getSentenceSimilarityApi() {
val sentencesSimilarityTask = analyzer!!.analyseSentencesSimilarity(
sentence_1!!.text.toString(), sentence_2!!.text.toString()
)
sentencesSimilarityTask.addOnSuccessListener { sentencesSimilarity ->
resultTextView!!.text = "The similarity value is $sentencesSimilarity"
}.addOnFailureListener { e -> onFailure(e) }
}
private fun onFailure(e: Exception?) {
if (e is MLTextEmbeddingException) {
val embeddingException = e
embeddingException.errCode
embeddingException.message
} else {
}
}
}
In the SimilarWordsActivity.kt we can find the business logic for similar words count.
class SimilarWordsActivity : AppCompatActivity() {
private var findSimilarWords: Button? = null
private var word_1: EditText? = null
private var word_2:EditText? = null
private var analyzer: MLTextEmbeddingAnalyzer? = null
var resultTextView: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_similar_words)
word_1 = findViewById(R.id.word1)
word_2 = findViewById(R.id.word2)
resultTextView = findViewById(R.id.similar_word_value)
findSimilarWords = findViewById(R.id.check_word_similarity)
findSimilarWords!!.setOnClickListener(View.OnClickListener { noOfSimilarWords() })
analyzer = UtilForAnalyzer.getAnalyzer()
}
private fun noOfSimilarWords() {
val multipleSimilarityWordsTask = analyzer!!.analyseSimilarWords(
word_1!!.text.toString(), word_2!!.text.toString().toInt() )
multipleSimilarityWordsTask.addOnSuccessListener { words ->
val stringBuilder = StringBuilder()
for (word in words) {
stringBuilder.append(word)
stringBuilder.append("\t")
}
resultTextView!!.text = stringBuilder.toString()
}.addOnFailureListener { e -> onFailure(e) }
}
private fun onFailure(e: Exception?) {
if (e is MLTextEmbeddingException) {
val embeddingException = e
embeddingException.errCode
embeddingException.message
} else {
}
}
}
In the activity_main.xml we can create the UI screen.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/word_similar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Word Similarity"
android:textAllCaps="false"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/sentence_similar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/sentence_similar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Sentence Similarity"
android:textAllCaps="false"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/similar_words"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/word_similar" />
<Button
android:id="@+id/similar_words"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Similar Words"
android:textAllCaps="false"
android:textSize="17sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/word_similar" />
</androidx.constraintlayout.widget.ConstraintLayout>
In the activity_word_similar.xml we can create the UI screen for sentences.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WordSimilarActivity">
<EditText
android:id="@+id/similar_word1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/similar_word2"
android:background="@drawable/edittext_bg"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/similar_word2"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="17sp"
android:background="@drawable/edittext_bg"
app:layout_constraintBottom_toTopOf="@+id/similar_word_value"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/similar_word1" />
<TextView
android:id="@+id/similar_word_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/check_word_similarity"
android:textColor="@color/black"
android:textSize="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/similar_word2" />
<Button
android:id="@+id/check_word_similarity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check word Similarity"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/similar_word_value" />
</androidx.constraintlayout.widget.ConstraintLayout>
In the activity_sentence_similar.xml we can create the UI screen for sentences.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SentenceSimilarActivity">
<EditText
android:id="@+id/sentence1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/edittext_bg"
app:layout_constraintBottom_toTopOf="@+id/sentence2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/sentence2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/edittext_bg"
app:layout_constraintBottom_toTopOf="@+id/similar_word_value"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/sentence1" />
<TextView
android:id="@+id/similar_word_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/check_sentence_similarity"
android:textColor="@color/purple_500"
android:textSize="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/sentence2" />
<Button
android:id="@+id/check_sentence_similarity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Sentence Similarity"
android:textSize="17sp"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/similar_word_value" />
</androidx.constraintlayout.widget.ConstraintLayout>
In the activity_similar_words.xml we can create the UI screen for words count.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SimilarWordsActivity">
<EditText
android:id="@+id/word1"
android:gravity="center"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/word2"
android:background="@drawable/edittext_bg"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/word2"
android:gravity="center"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:hint="enter number"
android:textSize="17sp"
android:inputType="number"
android:background="@drawable/edittext_bg"
app:layout_constraintBottom_toTopOf="@+id/similar_word_value"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/word1" />
<TextView
android:id="@+id/similar_word_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/check_word_similarity"
android:textColor="@color/black"
android:textSize="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/word2" />
<Button
android:id="@+id/check_word_similarity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Similar Words"
android:textSize="17sp"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/similar_word_value" />
</androidx.constraintlayout.widget.ConstraintLayout>
Demo
Tips and Tricks
Make sure you are already registered as Huawei developer.
Set minSDK version to 21 or later, otherwise you will get AndriodManifest merge issue.
Make sure you have added the agconnect-services.json file to app folder.
Make sure you have added SHA-256 fingerprint without fail.
Make sure all the dependencies are added properly.
Conclusion
In this article, we can learn how to check similar words and sentences using Text Embedding feature of Huawei ML Kit. This Huawei ML Kit provides Text Embedding feature which helps to get matching vector value of words or sentences. Using this feature, we can improve our research based on the result. It provides similarity between two words or sentences and similar words of a particular word searched. We can also improve searching and browsing efficiency using after getting results related to search text.
I hope you have read this article. If you found it is helpful, please provide likes and comments.
Reference
ML Kit – Text Embedding
ML Kit – Training Video
Top comments (0)