Language Detector
Lemmatizer module. Provides classes for text language detection using lemmatization and token sampling.
- LanguageDetector: Class for performing language detection using lemmatization and token sampling.
- in_target_language(): A legacy function that wraps the LanguageDetector's is_known() method.
- langdetect(): A legacy function that wraps the LanguageDetector's is_known() method.
Classes
LanguageDetector
A class that performs language detection using lemmatization and token sampling.
Source code in simplemma/language_detector.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
Functions
__init__(lang, token_sampler=MostCommonTokenSampler(), lemmatization_strategy=DefaultStrategy())
Initialize the LanguageDetector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lang |
Union[str, Tuple[str, ...]]
|
The target language or languages to detect. |
required |
token_sampler |
TokenSampler
|
The token sampling strategy to use.
Defaults to |
MostCommonTokenSampler()
|
lemmatization_strategy |
LemmatizationStrategy
|
The lemmatization
strategy to use. |
DefaultStrategy()
|
Source code in simplemma/language_detector.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
main_language(text, additional_token_samplers=[RelaxedMostCommonTokenSampler()])
Determine the main language of the given text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text to analyze. |
required |
additional_token_samplers |
List[TokenSampler]
|
Additional token
sampling strategies to use. Defaults to |
[RelaxedMostCommonTokenSampler()]
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The main language of the text. |
Source code in simplemma/language_detector.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
proportion_in_each_language(text)
Calculate the proportion of each language in the given text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text to analyze. |
required |
Returns:
Type | Description |
---|---|
Dict[str, float]
|
Dict[str, float]: A dictionary containing the detected languages and their respective proportions. |
Source code in simplemma/language_detector.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
proportion_in_target_languages(text)
Calculate the proportion of text in the target language.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text to analyze. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The proportion of text in the target language(s). |
Source code in simplemma/language_detector.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
Functions
in_target_language(text, lang, greedy=False, token_sampler=MostCommonTokenSampler())
Calculate the proportion of text in the target language(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text to analyze. |
required |
lang |
Union[str, Tuple[str, ...]]
|
The target language(s) to compare against. |
required |
greedy |
bool
|
Whether to use greedy lemmatization. Defaults to |
False
|
token_sampler |
TokenSampler
|
The token sampling strategy to use.
Defaults to |
MostCommonTokenSampler()
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The proportion of text in the target language(s). |
Source code in simplemma/language_detector.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
langdetect(text, lang, greedy=False, token_samplers=[MostCommonTokenSampler(), RelaxedMostCommonTokenSampler()])
Detect the language(s) of the given text and their proportions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text to analyze. |
required |
lang |
Union[str, Tuple[str, ...]]
|
The target language(s) to compare against. |
required |
greedy |
bool
|
Whether to use greedy lemmatization. Defaults to |
False
|
token_samplers |
List[TokenSampler]
|
The list of token sampling strategies
to use. Defaults to |
[MostCommonTokenSampler(), RelaxedMostCommonTokenSampler()]
|
Returns:
Type | Description |
---|---|
List[Tuple[str, float]]
|
List[Tuple[str, float]]: A list of tuples containing the detected language(s) and their respective proportions. |
Source code in simplemma/language_detector.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|