Certainly! Let's break down the function you've provided and explain how it works step by step, including examples to illustrate each part.
Function Definition:
def unicodeToAscii(s):
# Turns a Unicode string into plain ASCII
return ''.join(
c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn'
and c in all_letters
)
Purpose of the Function:
The unicodeToAscii function converts a Unicode string (s) into a plain ASCII string. It does this by:
- Normalizing the Unicode string to decompose combined characters into their base characters and combining marks.
- Removing any diacritical marks (like accents).
- Keeping only characters that are considered letters (as defined in
all_letters).
Step-by-Step Explanation:
-
Normalization:
unicodedata.normalize('NFD', s)
- What it does: Converts the Unicode string
s into its decomposed form (Normalization Form D - NFD).
- Example: The character
'é' (Unicode: U+00E9) becomes 'e' and '́' (combining acute accent).
-
Iterating Over Characters:
c for c in unicodedata.normalize('NFD', s)
- What it does: Goes through each character
c in the normalized string.
-
Filtering Characters:
if unicodedata.category(c) != 'Mn' and c in all_letters
unicodedata.category(c) != 'Mn'
- What it checks: Ensures that the character
c is not a non-spacing mark (diacritical mark).
- Why: Non-spacing marks are the combining characters (like accents) that we want to remove.
c in all_letters
- What it checks: Ensures that the character
c is in the set of allowed letters (all_letters).
- Why: To include only desired characters, typically ASCII letters, and exclude any others.
-
Joining Characters:
''.join(...)
- What it does: Concatenates the filtered characters back into a single string.
Defining all_letters:
Before using the function, we need to define all_letters. In many contexts (such as character-level models), it's defined as:
import string
all_letters = string.ascii_letters + " .,;'"
string.ascii_letters: Contains all ASCII uppercase and lowercase letters ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ').
- Additional characters (
" .,;'"): Space and punctuation that we may want to keep.
Examples:
Let's go through some examples to see how the function works.
Example 1: "Ślusàrski"
Original String:
s = 'Ślusàrski'
Step 1: Normalize
import unicodedata
normalized_s = unicodedata.normalize('NFD', s)
- Result:
'Ślusàrski'
- Characters:
['S', '́', 'l', 'u', 's', 'a', '̀', 'r', 's', 'k', 'i']
Step 2: Filter Characters
Iterate over each character c in normalized_s:
- 'S'
- Category:
'Lu' (Letter, uppercase)
- In
all_letters: Yes
- Included
- '́' (Combining acute accent)
- Category:
'Mn' (Non-spacing mark)
- Excluded
- 'l', 'u', 's', 'a'
- Categories:
'Ll' (Letter, lowercase)
- In
all_letters: Yes
- Included
- '̀' (Combining grave accent)
- Category:
'Mn' (Non-spacing mark)
- Excluded
- 'r', 's', 'k', 'i'
- Categories:
'Ll' (Letter, lowercase)
- In
all_letters: Yes
- Included
Step 3: Join Characters
- Resulting String:
'Slusarski'
Example 2: "Červená"
Original String:
s = 'Červená'
Step 1: Normalize
normalized_s = unicodedata.normalize('NFD', s)
- Result:
'Červená'
- Characters:
['C', '̌', 'e', 'r', 'v', 'e', 'n', 'a', '́']
Step 2: Filter Characters
- 'C'
- Category:
'Lu'
- In
all_letters: Yes
- Included
- '̌' (Combining caron)
- 'e', 'r', 'v', 'e', 'n', 'a'
- Categories:
'Ll'
- In
all_letters: Yes
- Included
- '́' (Combining acute accent)
Step 3: Join Characters
- Resulting String:
'Cervena'
Example 3: "München"
Original String:
s = 'München'
Step 1: Normalize
normalized_s = unicodedata.normalize('NFD', s)
- Result:
'München'
- Characters:
['M', 'u', '̈', 'n', 'c', 'h', 'e', 'n']
Step 2: Filter Characters
- 'M'
- Category:
'Lu'
- In
all_letters: Yes
- Included
- 'u'
- Category:
'Ll'
- In
all_letters: Yes
- Included
- '̈' (Combining diaeresis)
- 'n', 'c', 'h', 'e', 'n'
- Categories:
'Ll'
- In
all_letters: Yes
- Included
Step 3: Join Characters
- Resulting String:
'Munchen'
Example 4: "François"
Original String:
s = 'François'
Step 1: Normalize
normalized_s = unicodedata.normalize('NFD', s)
- Result:
'François'
- Characters:
['F', 'r', 'a', 'n', 'c', '̧', 'o', 'i', 's']
Step 2: Filter Characters
- 'F', 'r', 'a', 'n', 'c'
- Categories:
'Lu' or 'Ll'
- In
all_letters: Yes
- Included
- '̧' (Combining cedilla)
- 'o', 'i', 's'
- Categories:
'Ll'
- In
all_letters: Yes
- Included
Step 3: Join Characters
- Resulting String:
'Francois'
Example 5: "中文"
Original String:
s = '中文' # Chinese characters for "Chinese language"
Step 1: Normalize
normalized_s = unicodedata.normalize('NFD', s)
- Result: The same as input (
'中文'), as these characters do not decompose.
Step 2: Filter Characters
- '中', '文'
- Categories: Both
'Lo' (Letter, other)
- In
all_letters: No (since all_letters contains only ASCII letters and some punctuation)
- Excluded
Step 3: Join Characters
- Resulting String:
'' (empty string)
Explanation of Results:
- The function effectively removes diacritics from letters, converting them to their base ASCII equivalents.
- Non-ASCII characters (like Chinese characters, Cyrillic letters, etc.) are excluded, resulting in their removal if they are not in
all_letters.
- This function is useful when you need to preprocess text data by stripping accents and ensuring only a specific set of characters is present.
Why Use This Function?
- Data Preprocessing: For machine learning models (especially those that work at the character level), it's often necessary to have consistent and simplified inputs.
- Normalization: Helps in situations where variations of characters (like accented letters) should be treated the same as their base letters.
- Filtering: Ensures that unexpected or unsupported characters do not cause issues downstream.
Key Takeaways:
- Normalization ('NFD'): Breaks down characters into their base forms and combining marks.
- Unicode Categories:
'Mn': Non-spacing marks (combining diacritical marks).
- Other categories like
'Lu' (Letter, uppercase) and 'Ll' (Letter, lowercase) are retained.
all_letters: Defines the set of characters you want to keep. Adjusting this can include or exclude more characters as needed.
Adjusting the Function:
- Including More Characters:
- Changing the Filter Criteria:
- To include all letters (even those from other scripts), you might remove the
c in all_letters condition and adjust your if statement accordingly.
Conclusion:
The unicodeToAscii function is a practical tool for text normalization, simplifying Unicode strings to plain ASCII by removing accents and unwanted characters. By understanding each part of the function and tailoring all_letters to your needs, you can effectively preprocess text data for a variety of applications.