I need to detect the language of a string read from a pdf file the text is basically in English language, but "NLLanguageRecognizer" return that it is "Romanian"
the function I am using is :
class func detectedLangaugeFormat(for string: String) -> String {
if #available(iOS 12.0, *) {
let recognizer = NLLanguageRecognizer()
recognizer.processString(string)
guard let languageCode = recognizer.dominantLanguage?.rawValue else { return "rtl" }
let detectedLangauge = Locale.current.localizedString(forIdentifier: languageCode)
print("lan")
let currentLocale = NSLocale.current as NSLocale
let direction: NSLocale.LanguageDirection = NSLocale.characterDirection(forLanguage: languageCode)
if direction == .rightToLeft {
return "rtl"
}else if direction == .leftToRight {
return "ltr"
}
} else {
// Fallback on earlier versions
}
return "rtl"
}
and the string given to this method is :
"\r\n A Simple PDF File \r\n This is a small demonstration .pdf file - \r\n just for use in the Virtual Mechanics tutorials. More text. And more \r\n text. And more text. And more text. And more text. \r\n And more text. And more text. And more text. And more text. And more \r\n text. And more text. Boring, zzzzz. And more text. And more text. And \r\n more text. And more text. And more text. And more text. And more text. \r\n And more text. And more text. \r\n And more text. And more text. And more text. And more text. And more \r\n text. And more text. And more text. Even more. Continued on page 2 ...\r\n Simple PDF File 2 \r\n ...continued from page 1. Yet more text. And more text. And more text. \r\n And more text. And more text. And more text. And more text. And more \r\n text. Oh, how boring typing this stuff. But not as boring as watching \r\n paint dry. And more text. And more text. And more text. And more text. \r\n Boring. More, a little more text. The end, and just as well. "