Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/cms 118 add javadoc for language detection #123

Merged
merged 10 commits into from
Apr 3, 2024
4 changes: 1 addition & 3 deletions languagedetection/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hms.lib.commonmobileservices.languagedetection">
</manifest>
<manifest package="com.hms.lib.commonmobileservices.languagedetection" />
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ import com.hms.lib.commonmobileservices.languagedetection.implementation.ILangua
import com.huawei.agconnect.config.AGConnectServicesConfig
import com.huawei.hms.mlsdk.common.MLApplication

/**
* Utility class for accessing language detection services provided by Huawei and Google.
*/
class HuaweiGoogleLanguageDetector private constructor() {

companion object {

/**
* Retrieves a language detection client based on the mobile service type of the device.
* @param context The context used for determining the mobile service type.
* @param confidenceThreshold The confidence threshold to be applied to language detection.
* @return An instance of ILanguageDetection corresponding to the mobile service type.
* @throws IllegalArgumentException If the mobile service type is not supported.
*/
fun getClient(
context: Context,
confidenceThreshold: Float? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@ package com.hms.lib.commonmobileservices.languagedetection.common

import com.hms.lib.commonmobileservices.core.ErrorModel

/**
* Sealed class representing the result of a detection operation.
* @param T The type of data contained in the result.
*/
sealed class DetectionResult<out T> {

/**
* Represents a successful detection result.
* @property data The data resulting from the detection operation.
*/
data class Success<out T>(
val data: T
): DetectionResult<T>()
) : DetectionResult<T>()

/**
* Represents an error that occurred during the detection operation.
* @property errorMessage The error message describing the failure.
* @property errorModel An optional error model providing additional details about the failure.
*/
data class Error(
val errorMessage: String? = null,
val errorModel: ErrorModel? = null
): DetectionResult<Nothing>()
) : DetectionResult<Nothing>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
// limitations under the License.
package com.hms.lib.commonmobileservices.languagedetection.common

/**
* Data class representing a possible language detected during language identification.
* @param langCode The language code representing the detected language.
* @param confidence The confidence score indicating the level of confidence in the detection result.
*/
data class PossibleLanguage(
val langCode: String,
val confidence: Float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@ import com.google.mlkit.nl.languageid.LanguageIdentificationOptions
import com.hms.lib.commonmobileservices.languagedetection.implementation.GoogleLanguageIdentification
import com.hms.lib.commonmobileservices.languagedetection.implementation.ILanguageDetection

class GoogleLanguageDetectorFactory: LanguageDetectionFactory() {
/**
* Factory class for creating instances of ILanguageDetection using Google's language detection services.
*/
class GoogleLanguageDetectorFactory : LanguageDetectionFactory() {

/**
* Creates an instance of ILanguageDetection without specifying a confidence threshold.
* @return An instance of ILanguageDetection using Google's default language identification client.
*/
override fun create(): ILanguageDetection {
return GoogleLanguageIdentification(LanguageIdentification.getClient())
}

/**
* Creates an instance of ILanguageDetection with a specified confidence threshold.
* @param confidenceThreshold The confidence threshold to be applied to language identification.
* @return An instance of ILanguageDetection using Google's language identification client with the specified confidence threshold.
*/
override fun create(confidenceThreshold: Float): ILanguageDetection {
val threshold = confidenceThreshold.coerceIn(0.0f,1.0f)
val threshold = confidenceThreshold.coerceIn(0.0f, 1.0f)
val options = LanguageIdentificationOptions.Builder()
.setConfidenceThreshold(threshold)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ import com.hms.lib.commonmobileservices.languagedetection.implementation.ILangua
import com.huawei.hms.mlsdk.langdetect.MLLangDetectorFactory
import com.huawei.hms.mlsdk.langdetect.cloud.MLRemoteLangDetectorSetting

class HuaweiLanguageDetectorFactory: LanguageDetectionFactory() {

/**
* Factory class for creating instances of ILanguageDetection using Huawei's language detection services.
*/
class HuaweiLanguageDetectorFactory : LanguageDetectionFactory() {

/**
* Creates an instance of ILanguageDetection without specifying a confidence threshold.
* @return An instance of ILanguageDetection using Huawei's default remote language detector.
*/
override fun create(): ILanguageDetection {
val mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
.remoteLangDetector

return HuaweiLanguageDetection(mlRemoteLangDetector)
}

/**
* Creates an instance of ILanguageDetection with a specified confidence threshold.
* @param confidenceThreshold The confidence threshold to be applied to language detection.
* @return An instance of ILanguageDetection using Huawei's remote language detector with the specified confidence threshold.
*/
override fun create(confidenceThreshold: Float): ILanguageDetection {
val threshold = confidenceThreshold.coerceIn(0.0f,1.0f)
val threshold = confidenceThreshold.coerceIn(0.0f, 1.0f)
val setting = MLRemoteLangDetectorSetting.Factory()
.setTrustedThreshold(threshold)
.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,36 @@ import com.hms.lib.commonmobileservices.languagedetection.implementation.HuaweiL
import com.hms.lib.commonmobileservices.languagedetection.implementation.ILanguageDetection
import java.lang.IllegalArgumentException

/**
* Abstract factory class for creating instances of ILanguageDetection.
*/
abstract class LanguageDetectionFactory {

/**
* Creates an instance of ILanguageDetection without specifying a confidence threshold.
* @return An instance of ILanguageDetection.
*/
abstract fun create(): ILanguageDetection

/**
* Creates an instance of ILanguageDetection with a specified confidence threshold.
* @param confidenceThreshold The confidence threshold to be applied to language detection.
* @return An instance of ILanguageDetection.
*/
abstract fun create(confidenceThreshold: Float): ILanguageDetection

/**
* Companion object containing utility methods for creating LanguageDetectionFactory instances.
*/
companion object {
inline fun <reified T: ILanguageDetection> createFactory(): LanguageDetectionFactory =
when(T::class){

/**
* Creates a specific LanguageDetectionFactory based on the type of ILanguageDetection.
* @return An instance of LanguageDetectionFactory corresponding to the ILanguageDetection type.
* @throws IllegalArgumentException If the ILanguageDetection type is not supported.
*/
inline fun <reified T : ILanguageDetection> createFactory(): LanguageDetectionFactory =
when (T::class) {
HuaweiLanguageDetection::class -> HuaweiLanguageDetectorFactory()
GoogleLanguageIdentification::class -> GoogleLanguageDetectorFactory()
else -> throw IllegalArgumentException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ import com.hms.lib.commonmobileservices.core.ErrorModel
import com.hms.lib.commonmobileservices.languagedetection.common.DetectionResult
import com.hms.lib.commonmobileservices.languagedetection.common.PossibleLanguage

/**
* Implementation of ILanguageDetection using Google's language identification service.
* @property languageIdentifier The underlying language identifier used for language detection.
* @constructor Creates a GoogleLanguageIdentification instance with the provided language identifier.
*/
internal class GoogleLanguageIdentification(
private val languageIdentifier: LanguageIdentifier
) : ILanguageDetection {

/**
* Detects the language of the provided source text.
* @param sourceText The text for which language detection is performed.
* @param callback A callback function to handle the detection result.
*/
override fun detectLanguage(
sourceText: String,
callback: (detectResult: DetectionResult<String>) -> Unit
Expand Down Expand Up @@ -51,6 +61,11 @@ internal class GoogleLanguageIdentification(
}
}

/**
* Detects possible languages of the provided source text along with their confidence levels.
* @param sourceText The text for which language detection is performed.
* @param callback A callback function to handle the detection result.
*/
override fun detectPossibleLanguages(
sourceText: String,
callback: (detectResult: DetectionResult<List<PossibleLanguage>>) -> Unit
Expand Down Expand Up @@ -81,5 +96,8 @@ internal class GoogleLanguageIdentification(
}
}

/**
* Stops the language detector.
*/
override fun stopDetector() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ import com.hms.lib.commonmobileservices.languagedetection.common.DetectionResult
import com.hms.lib.commonmobileservices.languagedetection.common.PossibleLanguage
import com.huawei.hms.mlsdk.langdetect.cloud.MLRemoteLangDetector

/**
* Implementation of ILanguageDetection using Huawei's remote language detector.
* @property languageDetector The underlying MLRemoteLangDetector used for language detection.
* @constructor Creates a HuaweiLanguageDetection instance with the provided MLRemoteLangDetector.
*/
class HuaweiLanguageDetection(
private val languageDetector: MLRemoteLangDetector
): ILanguageDetection {
) : ILanguageDetection {

/**
* Detects the language of the provided source text.
* @param sourceText The text for which language detection is performed.
* @param callback A callback function to handle the detection result.
*/
override fun detectLanguage(
sourceText: String,
callback: (detectResult: DetectionResult<String>) -> Unit
Expand All @@ -42,6 +52,11 @@ class HuaweiLanguageDetection(
}
}

/**
* Detects possible languages of the provided source text along with their confidence levels.
* @param sourceText The text for which language detection is performed.
* @param callback A callback function to handle the detection result.
*/
override fun detectPossibleLanguages(
sourceText: String,
callback: (detectResult: DetectionResult<List<PossibleLanguage>>) -> Unit
Expand Down Expand Up @@ -71,6 +86,9 @@ class HuaweiLanguageDetection(
}
}

/**
* Stops the language detector.
*/
override fun stopDetector() {
languageDetector.stop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,33 @@ package com.hms.lib.commonmobileservices.languagedetection.implementation
import com.hms.lib.commonmobileservices.languagedetection.common.DetectionResult
import com.hms.lib.commonmobileservices.languagedetection.common.PossibleLanguage

/**
* Interface defining language detection functionalities.
*/
interface ILanguageDetection {

/**
* Detects the language of the provided source text.
* @param sourceText The text for which language detection is performed.
* @param callback A callback function to handle the detection result.
*/
fun detectLanguage(
sourceText: String,
callback: (detectResult: DetectionResult<String>) -> Unit
)

/**
* Detects possible languages of the provided source text along with their confidence levels.
* @param sourceText The text for which language detection is performed.
* @param callback A callback function to handle the detection result.
*/
fun detectPossibleLanguages(
sourceText: String,
callback: (detectResult: DetectionResult<List<PossibleLanguage>>) -> Unit
)

/**
* Stops the language detector.
*/
fun stopDetector()
}
Loading