4

I am trying to remove the Arabic text diacritic. For example I need to convert this َب to this ب , here is my code :

if (text != "") {
    for char in text! {
        print(char)
        print(char.unicodeScalars.first?.value)
        if allowed.contains("\(char)"){
            newText.append(char)
        }
    }
    self.textView.text = text!
} else {
//            TODO :
//            show an alert
    print("uhhh no way")
}

I have tried these solutions but with no luck :

How to remove diacritics from a String in Swift?

NSString : easy way to remove UTF-8 accents from a string?

Anton Belousov
  • 1,140
  • 15
  • 34
keloa zoldik
  • 95
  • 1
  • 15

5 Answers5

6

You can use Regex, try this code

 let myString = "الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ"
        let regex = try! NSRegularExpression(pattern: "[\\u064b-\\u064f\\u0650-\\u0652]", options: NSRegularExpression.Options.caseInsensitive)
        let range = NSMakeRange(0, myString.unicodeScalars.count)
        let modString = regex.stringByReplacingMatches(in: myString, options: [], range: range, withTemplate: "")
        print(modString)

Output : الحمد لله رب العالمين

a.masri
  • 2,439
  • 1
  • 14
  • 32
5

Use this extension:

extension String {
    /// strip combining marks (accents or diacritics)
    var stripDiacritics: String {
        let mStringRef = NSMutableString(string: self) as CFMutableString
        CFStringTransform(mStringRef, nil, kCFStringTransformStripCombiningMarks, false)
        return mStringRef as String
    }
}

enter image description here

Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
1

you can use CFStringTransform with kCFStringTransformStripCombiningMarks

to remove (accents or diacritics)

        let original = "ََب"
        let mutableString = NSMutableString(string: original) as CFMutableString
        CFStringTransform(mutableString, nil, kCFStringTransformStripCombiningMarks, Bool(truncating: 0))
        let normalized = (mutableString as NSMutableString).copy() as! NSString

        print(normalized)

CFStringTransform

A constant containing the transformation of a string by removing combining marks.

kCFStringTransformStripCombiningMarks

The identifier of a transform to strip combining marks (accents or diacritics).

Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
1
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#this code for arabic preporocessing
import pyarabic.araby as araby
import pyarabic.number as number

text = u'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ'

#Strip Harakat from arabic word except Shadda.
from pyarabic.araby import strip_harakat
print(strip_harakat(text))
# الحمد للّه ربّ العالمين

#حذف الحركات بما فيها الشدة
#Strip vowels from a text, include Shadda.
from pyarabic.araby import strip_tashkeel
print(strip_tashkeel(text))
#الحمد لله رب العالمين
FouziaBou
  • 19
  • 2
1

Based on @Hashem-Aboonajmi answer

extension String {
    /// strip combining marks (accents or diacritics)
    func stripDiacritics(active: Bool) -> String {
        if !active {return self}
        let mStringRef = NSMutableString(string: self) as CFMutableString
        CFStringTransform(mStringRef, nil, kCFStringTransformStripCombiningMarks, false)
        return mStringRef as String
    }
}

in case you want to activate or deactivate

iTarek
  • 736
  • 1
  • 8
  • 17
  • Just beware this method removes the ء also, so أ becomes ا and ئ become ى There for I will see the other methods – iTarek Oct 14 '22 at 12:57