How to replace exact matching hindi words with substitute word in for non-word characters (like विषयों)

86 Views Asked by At

I'm using python 3 Jupyter notebook. I want to write code that searches for specific words from my list and if it matches exactly, I want to replace it with substitute word. I want make sure, that this is exact match and not a partial match from a word. Input word could be in Hindi or English or transliterate.

Input string - "जेईई मेन पाठ्यक्रम 2024 को पढ़ने से न केवल आपको यह समझने में मदद मिलेगी कि आपको किन विषयों का अध्ययन करने की आवश्यकता है, बल्कि यह आपको तदनुसार एक अध्ययन योजना तैयार करने में भी मदद करेगा। पाठ्यक्रम आपको यह सुनिश्चित करने में भी मदद करेगा कि परीक्षा की तैयारी के दौरान आप कोई भी विषय न छोड़ें।"

word to be searched = ["विषयक" , "विषय" , "topic"] 
substitute word = "टॉपिक"
  • Here is my code & its output -
import re

def exact_match_replace(input_text, search_word, substitute_word):
    # Escape special characters in search word and create a regex pattern
    search_pattern = rf"\b{re.escape(search_word)}\b"
    
    # Use regex to find exact matches and replace with substitute word
    result = re.sub(search_pattern, substitute_word, input_text, flags=re.IGNORECASE)
    
    return result

# Sample usage
input_text = "जेईई मेन पाठ्यक्रम 2024 को पढ़ने से न केवल आपको यह समझने में मदद मिलेगी कि आपको किन विषयों का अध्ययन करने की आवश्यकता है, बल्कि यह आपको तदनुसार एक अध्ययन योजना तैयार करने में भी मदद करेगा। पाठ्यक्रम आपको यह सुनिश्चित करने में भी मदद करेगा कि परीक्षा की तैयारी के दौरान आप कोई भी विषय न छोड़ें।"
search_word = "विषय"
substitute_word = "टॉपिक"

result_text = exact_match_replace(input_text, search_word, substitute_word)
print(result_text)

Actual output from code:

जेईई मेन पाठ्यक्रम 2024 को पढ़ने से न केवल आपको यह समझने में मदद मिलेगी कि आपको किन टॉपिकों का अध्ययन करने की आवश्यकता है, बल्कि यह आपको तदनुसार एक अध्ययन योजना तैयार करने में भी मदद करेगा। पाठ्यक्रम आपको यह सुनिश्चित करने में भी मदद करेगा कि परीक्षा की तैयारी के दौरान आप कोई भी टॉपिक न छोड़ें।

Expected output

जेईई मेन पाठ्यक्रम 2024 को पढ़ने से न केवल आपको यह समझने में मदद मिलेगी कि आपको किन विषयों का अध्ययन करने की आवश्यकता है, बल्कि यह आपको तदनुसार एक अध्ययन योजना तैयार करने में भी मदद करेगा। पाठ्यक्रम आपको यह सुनिश्चित करने में भी मदद करेगा कि परीक्षा की तैयारी के दौरान आप कोई भी टॉपिक न छोड़ें।

The \b metacharacter matches the empty string but only at the beginning or end of a word. However, it doesn't work for words that have non-word characters (like विषयों).

Expectation - code solution that should work as per expected output.

1

There are 1 best solutions below

0
Andj On

You need to use the regex module instead of the re module.

The re module's definition of \w and \b isn't what you expect, any character that has a general category of Mark is not considered as a word forming character by Python. This is divergent to Unicode's definition.

For instance:

import re
search_word = "विषय"
re.findall(r'\b\w+\b', search_word)
# ['व', 'षय']

The independent vowel is a non-word character, and re will not match your search term since a word boundary is inside the search term.

Use instead install regex:

pip install -U regex

Then, replace the import statement with

import regex as re

This gives you:

import regex as re

def exact_match_replace(input_text, search_word, substitute_word):
    # Escape special characters in search word and create a regex pattern
    search_pattern = rf"\b{re.escape(search_word)}\b"
    
    # Use regex to find exact matches and replace with substitute word
    result = re.sub(search_pattern, substitute_word, input_text, flags=re.IGNORECASE)
    
    return result

# Sample usage
input_text = "जेईई मेन पाठ्यक्रम 2024 को पढ़ने से न केवल आपको यह समझने में मदद मिलेगी कि आपको किन विषयों का अध्ययन करने की आवश्यकता है, बल्कि यह आपको तदनुसार एक अध्ययन योजना तैयार करने में भी मदद करेगा। पाठ्यक्रम आपको यह सुनिश्चित करने में भी मदद करेगा कि परीक्षा की तैयारी के दौरान आप कोई भी विषय न छोड़ें।"
search_word = "विषय"
substitute_word = "टॉपिक"

result_text = exact_match_replace(input_text, search_word, substitute_word)
print(result_text)
# जेईई मेन पाठ्यक्रम 2024 को पढ़ने से न केवल आपको यह समझने में मदद मिलेगी कि आपको किन विषयों का 
# अध्ययन करने की आवश्यकता है, बल्कि यह आपको तदनुसार एक अध्ययन योजना तैयार करने में भी मदद करेगा। 
# पाठ्यक्रम आपको यह सुनिश्चित करने में भी मदद करेगा कि परीक्षा की तैयारी के दौरान आप कोई भी टॉपिक न छोड़ें।