Python Forum

Full Version: Write text expander in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I want to write simple program in Python that replace the certain sequence of letters with some unicode symbol. For example, "delta_low" -> "δ". User defines input and output.

I have no idea how to implement it. What the place to start?
I suggest the re module, especially the re.sub() function. This presentation could be a good starting point.
As Unicode is no problem in Python 3,start bye doing small test.
Example.
>>> sentence = 'Some measurement with delta_low did get get wrong'
>>> symbol = "δ"
>>> symbol_word = 'delta_low'
>>> if symbol_word in sentence:
...     print(sentence.replace(symbol_word, symbol))
...     
Some measurement with δ did get get wrong
If many symbols can eg using a dictionary,then replace as over but now using the dictionary.
>>> translation = {'delta_low': 'δ', 'Epsilon': 'ε'}
>>> translation['Epsilon']
'ε'
>>> 
Then can as @Gribouillis suggests look a regex or translate functions,usage deepens on the project complexity.
str.translate is for replacing character for character, string or None. So 'δ' -> 'delta_low' is possible, but 'delta_low' -> 'δ' is not.

>>> mapping = str.maketrans({"δ": 'delta_low'})                                                                                            
>>> 'Some measurement with δ did get get wrong'.translate(mapping)                                                                         
'Some measurement with delta_low did get get wrong'
>>> mapping = str.maketrans({'delta_low': "δ"})                                                                                            
/../
ValueError: string keys in translate table must be of length 1
.maketrans documentation:

Quote:Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y.

.translate documentation:

Quote:Translation table, which must be a mapping of Unicode ordinals to Unicode ordinals, strings, or None.
but my question was not about the process of replacement itself. I know what ReExp is.

My question was about how to make a programm that run in background of OS and replace letters automatically when user it writes anywhere, for example, here, in this post.
(Jan-16-2020, 08:18 AM)constantin01 Wrote: [ -> ]My question was about how to make a programm that run in background of OS and replace letters automatically when user it writes anywhere, for example, here, in this post.

nope, nothing of that kind in your original question :-)

(Jan-15-2020, 03:46 PM)constantin01 Wrote: [ -> ]Hi

I want to write simple program in Python that replace the certain sequence of letters with some unicode symbol. For example, "delta_low" -> "δ". User defines input and output.

I have no idea how to implement it. What the place to start?