Jump to content
TheSaint

Alternate Solutions - Examples

Recommended Posts

Posted (edited)

As an interesting exercise, I thought I would start this topic that others can add to if they want. Perhaps others, including myself, will learn a thing or more. :)

I will make this first post a kind of index.

Basically as any experienced coder knows, there is more than one way to achieve things with your code.

Some more complex methods require a good degree of experience to implement, while some are quick and simple but involve a lot of code, and then there are a variety of methods somewhere between the two extremes.

Replace Foreign or Accented Characters  (jhcd's alternate method)  (Danyfirex method)  (CYCho method)

Toggle Button or Control Text (TheDcoder's solutions plus TheSaint's)

 

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites
Posted (edited)

Replace Foreign or Accented Characters

This is something I recently had a need for, due to the third party program Calibre having strict rules about folder etc naming, though it stores the original names in its database.

You can run each method with something like this of course.

Global $text = "Rosãleê Ündérlæ"
$text = ReplaceForeignCharacters($text)
MsgBox(0, "Replacement Text", $text)

My first approach, was quick and simple, but not very elegant.

Func ReplaceForeignCharacters($text)
    $text = StringReplace($text, "À", "A", 0, 1)
    $text = StringReplace($text, "Á", "A", 0, 1)
    $text = StringReplace($text, "Â", "A", 0, 1)
    $text = StringReplace($text, "Ã", "A", 0, 1)
    $text = StringReplace($text, "Ä", "A", 0, 1)
    $text = StringReplace($text, "Å", "A", 0, 1)
    $text = StringReplace($text, "Æ", "AE", 0, 1)
    $text = StringReplace($text, "Ç", "C", 0, 1)
    $text = StringReplace($text, "È", "E", 0, 1)
    $text = StringReplace($text, "É", "E", 0, 1)
    $text = StringReplace($text, "Ê", "E", 0, 1)
    $text = StringReplace($text, "Ë", "E", 0, 1)
    $text = StringReplace($text, "Ì", "I", 0, 1)
    $text = StringReplace($text, "Í", "I", 0, 1)
    $text = StringReplace($text, "Î", "I", 0, 1)
    $text = StringReplace($text, "Ï", "I", 0, 1)
    $text = StringReplace($text, "Ð", "D", 0, 1)
    $text = StringReplace($text, "Ñ", "N", 0, 1)
    $text = StringReplace($text, "Ò", "O", 0, 1)
    $text = StringReplace($text, "Ó", "O", 0, 1)
    $text = StringReplace($text, "Ô", "O", 0, 1)
    $text = StringReplace($text, "Õ", "O", 0, 1)
    $text = StringReplace($text, "Ö", "O", 0, 1)
    $text = StringReplace($text, "×", "x", 0, 1)
    $text = StringReplace($text, "Ø", "O", 0, 1)
    $text = StringReplace($text, "Ù", "U", 0, 1)
    $text = StringReplace($text, "Ú", "U", 0, 1)
    $text = StringReplace($text, "Û", "U", 0, 1)
    $text = StringReplace($text, "Ü", "U", 0, 1)
    $text = StringReplace($text, "Ý", "Y", 0, 1)
    ;$text = StringReplace($text, "Þ", "", 0, 1)
    $text = StringReplace($text, "ß", "B", 0, 1)
    $text = StringReplace($text, "à", "a", 0, 1)
    $text = StringReplace($text, "á", "a", 0, 1)
    $text = StringReplace($text, "â", "a", 0, 1)
    $text = StringReplace($text, "ã", "a", 0, 1)
    $text = StringReplace($text, "ä", "a", 0, 1)
    $text = StringReplace($text, "å", "a", 0, 1)
    $text = StringReplace($text, "æ", "ae", 0, 1)
    $text = StringReplace($text, "ç", "c", 0, 1)
    $text = StringReplace($text, "è", "e", 0, 1)
    $text = StringReplace($text, "é", "e", 0, 1)
    $text = StringReplace($text, "ê", "e", 0, 1)
    $text = StringReplace($text, "ë", "e", 0, 1)
    $text = StringReplace($text, "ì", "i", 0, 1)
    $text = StringReplace($text, "í", "i", 0, 1)
    $text = StringReplace($text, "î", "i", 0, 1)
    $text = StringReplace($text, "ï", "i", 0, 1)
    $text = StringReplace($text, "ð", "o", 0, 1)
    $text = StringReplace($text, "ñ", "n", 0, 1)
    $text = StringReplace($text, "ò", "o", 0, 1)
    $text = StringReplace($text, "ó", "o", 0, 1)
    $text = StringReplace($text, "ô", "o", 0, 1)
    $text = StringReplace($text, "õ", "o", 0, 1)
    $text = StringReplace($text, "ö", "o", 0, 1)
    $text = StringReplace($text, "ø", "o", 0, 1)
    $text = StringReplace($text, "ù", "u", 0, 1)
    $text = StringReplace($text, "ú", "u", 0, 1)
    $text = StringReplace($text, "û", "u", 0, 1)
    $text = StringReplace($text, "ü", "u", 0, 1)
    $text = StringReplace($text, "ý", "y", 0, 1)
    ;$text = StringReplace($text, "þ", "", 0, 1)
    $text = StringReplace($text, "ÿ", "y", 0, 1)
    Return $text
EndFunc ;=> ReplaceForeignCharacters

I then later decided, in passing, that I should make it more streamlined, so I started out doing the following, which I have only finished for the sake of this topic.

Func ReplaceForeignCharacters($text)
    Local $char, $let
    $char = "À"
    $let = "A"
    While 1
        $text = StringReplace($text, $char, $let, 0, 1)
        If $char == "À" Then
            $char = "Á"
        ElseIf $char == "Á" Then
            $char = "Â"
        ElseIf $char == "Â" Then
            $char = "Ã"
        ElseIf $char == "Ã" Then
            $char = "Ä"
        ElseIf $char == "Ä" Then
            $char = "Å"
        ElseIf $char == "Å" Then
            $char = "Æ"
            $let = "AE"
        ElseIf $char == "Æ" Then
            $char = "Ç"
            $let = "C"
        ElseIf $char == "Ç" Then
            $char = "È"
            $let = "E"
        ElseIf $char == "È" Then
            $char = "É"
        ElseIf $char == "É" Then
            $char = "Ê"
        ElseIf $char == "Ê" Then
            $char = "Ë"
        ElseIf $char == "Ë" Then
            $char = "Ì"
            $let = "I"
        ElseIf $char == "Ì" Then
            $char = "Í"
        ElseIf $char == "Í" Then
            $char = "Î"
        ElseIf $char == "Î" Then
            $char = "Ï"
        ElseIf $char == "Ï" Then
            $char = "Ð"
            $let = "D"
        ElseIf $char == "Ð" Then
            $char = "Ñ"
            $let = "N"
        ElseIf $char == "Ñ" Then
            $char = "Ò"
            $let = "O"
        ElseIf $char == "Ò" Then
            $char = "Ó"
        ElseIf $char == "Ó" Then
            $char = "Ô"
        ElseIf $char == "Ô" Then
            $char = "Õ"
        ElseIf $char == "Õ" Then
            $char = "Ö"
        ElseIf $char == "Ö" Then
            $char = "×"
            $let = "x"
        ElseIf $char == "×" Then
            $char = "Ø"
            $let = "O"
        ElseIf $char == "Ø" Then
            $char = "Ù"
            $let = "U"
        ElseIf $char == "Ù" Then
            $char = "Ú"
        ElseIf $char == "Ú" Then
            $char = "Û"
        ElseIf $char == "Û" Then
            $char = "Ü"
        ElseIf $char == "Ü" Then
            $char = "Ý"
            $let = "Y"
        ElseIf $char == "Ý" Then
            $char = "ß"
            $let = "B"
        ElseIf $char == "ß" Then
            $char = "à"
            $let = "a"
        ElseIf $char == "à" Then
            $char = "á"
        ElseIf $char == "á" Then
            $char = "â"
        ElseIf $char == "â" Then
            $char = "ã"
        ElseIf $char == "ã" Then
            $char = "ä"
        ElseIf $char == "ä" Then
            $char = "å"
        ElseIf $char == "å" Then
            $char = "æ"
            $let = "ae"
        ElseIf $char == "æ" Then
            $char = "ç"
            $let = "c"
        ElseIf $char == "ç" Then
            $char = "è"
            $let = "e"
        ElseIf $char == "è" Then
            $char = "é"
        ElseIf $char == "é" Then
            $char = "ê"
        ElseIf $char == "ê" Then
            $char = "ë"
        ElseIf $char == "ë" Then
            $char = "ì"
            $let = "i"
        ElseIf $char == "ì" Then
            $char = "í"
        ElseIf $char == "í" Then
            $char = "î"
        ElseIf $char == "î" Then
            $char = "ï"
        ElseIf $char == "ï" Then
            $char = "ð"
            $let = "o"
        ElseIf $char == "ð" Then
            $char = "ñ"
            $let = "n"
        ElseIf $char == "ñ" Then
            $char = "ò"
            $let = "o"
        ElseIf $char == "ò" Then
            $char = "ó"
        ElseIf $char == "ó" Then
            $char = "ô"
        ElseIf $char == "ô" Then
            $char = "õ"
        ElseIf $char == "õ" Then
            $char = "ö"
        ElseIf $char == "ö" Then
            $char = "ø"
        ElseIf $char == "ø" Then
            $char = "ù"
            $let = "u"
        ElseIf $char == "ù" Then
            $char = "ú"
        ElseIf $char == "ú" Then
            $char = "û"
        ElseIf $char == "û" Then
            $char = "ü"
        ElseIf $char == "ü" Then
            $char = "ý"
            $let = "y"
        ElseIf $char == "ý" Then
            $char = "ÿ"
        ElseIf $char == "ÿ" Then
            ExitLoop
        EndIf
    WEnd
    Return $text
EndFunc ;=> ReplaceForeignCharacters

Becoming dissatisfied with that partway through, I then came up with and used the following in my program.

Func ReplaceForeignCharacters($text)
    Local $char, $let, $p, $pair, $pairs
    $pairs = "À,A|Á,A|Â,A|Ã,A|Ä,A|Å,A|Æ,AE|Ç,C|È,E|É,E|Ê,E|Ë,E|Ì,I|Í,I|Î,I|Ï,I|Ð,D|Ñ,N|Ò,O|Ó,O|Ô,O|Õ,O|Ö,O|×,x|Ø,O|Ù,U|Ú,U|Û,U|Ü,U|Ý,Y|ß,B|" _
        & "à,a|á,a|â,a|ã,a|ä,a|å,a|æ,ae|ç,c|è,e|é,e|ê,e|ë,e|ì,i|í,i|î,i|ï,i|ð,o|ñ,n|ò,o|ó,o|ô,o|õ,o|ö,o|ø,o|ù,u|ú,u|û,u|ü,u|ý,y|ÿ,y"
    $pairs = StringSplit($pairs, "|", 1)
    For $p = 1 To $pairs[0]
        $pair = $pairs[$p]
        $pair = StringSplit($pair, ",", 1)
        $char = $pair[1]
        $let = $pair[2]
        $text = StringReplace($text, $char, $let, 0, 1)
    Next
    Return $text
EndFunc ;=> ReplaceForeignCharacters

I could have refined it further, as I am sure my good buddy @TheDcoder would have done, to avoid repetition of some of the second pair characters, but I was happy enough as is ... and I like a quick easy visual ... play it simple is my mantra.

Please feel free to offer alternate solutions, improve upon my code etc. I imagine plenty would come up with a RegEx version, etc.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

Try this:

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Very good. Thanks.


Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

Hello. This is not as fast as jchd's code. But it works.

 

Global Const $g_aChars = [["À", "A"], ["Á", "A"], ["Â", "A"], ["Ã", "A"], ["Ä", "A"], ["Å", "A"], ["Æ", "AE"], ["Ç", "C"], ["È", "E"], ["É", "E"], _
        ["Ê", "E"], ["Ë", "E"], ["Ì", "I"], ["Í", "I"], ["Î", "I"], ["Ï", "I"], ["Ð", "D"], ["Ñ", "N"], _
        ["Ò", "O"], ["Ó", "O"], ["Ô", "O"], ["Õ", "O"], ["Ö", "O"], ["×", "x"], ["Ø", "O"], ["Ù", "U"], ["Ú", "U"], ["Û", "U"], ["Ü", "U"], ["Ý", "Y"], _
        ["ß", "B"], ["à", "a"], ["á", "a"], ["â", "a"], ["ã", "a"], ["ä", "a"], ["å", "a"], ["æ", "ae"], _
        ["ç", "c"], ["è", "e"], ["é", "e"], ["ê", "e"], ["ë", "e"], ["ì", "i"], ["í", "i"], ["î", "i"], ["ï", "i"], ["ð", "o"], ["ñ", "n"], ["ò", "o"], _
        ["ó", "o"], ["ô", "o"], ["õ", "o"], ["ö", "o"], ["ø", "o"], ["ù", "u"], ["ú", "u"], ["û", "u"], ["ü", "u"], ["ý", "y"], ["ÿ", "y"]]





Func ReplaceForeignCharacters($text)
    For $i = 0 To UBound($g_aChars) - 1
        $text = StringReplace($text, $g_aChars[$i][0], $g_aChars[$i][1], 0, 1)
    Next
    Return $text
EndFunc   ;==>ReplaceForeignCharacters

Saludos

Share this post


Link to post
Share on other sites

Yeah but if you include greek, cyrillic, you-name-it and (multiplied by) all the possible diacritics and modifiers, you end up with a big fat monster.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Hi all! This is pretty fast.
 

$sForeignText = "Rosãleê Ündérlæ"
$aForeignText = StringSplit($sForeignText, "")

$sForeignCharaters = "À,Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,×,Ø,Ù,Ú,Û,Ü,Ý,ß,à,á,â,ã,ä,å,æ,ç,è,é,ê,ë,ì,í,î,ï,ð,ñ,ò,ó,ô,õ,ö,ø,ù,ú,û,ü,ý,ÿ"
$sEnglishCharaters = "A,A,A,A,A,A,AE,C,E,E,E,E,I,I,I,I,D,N,O,O,O,O,O,x,O,U,U,U,U,Y,B,a,a,a,a,a,a,ae,c,e,e,e,e,i,i,i,i,o,n,o,o,o,o,o,o,u,u,u,u,y,y"

$aEnglishCharaters = StringSplit($sEnglishCharaters, ",")

$sEnglishText = ""
For $i = 1 To $aForeignText[0]
    $iPosition = StringInStr($sForeignCharaters, $aForeignText[$i], 1)
    If $iPosition Then
        $sEnglishText &= $aEnglishCharaters[($iPosition+1)/2]
    Else
        $sEnglishText &= $aForeignText[$i]
    EndIf
Next

MsgBox(0, "Replacement Text", $sEnglishText)

 

Share this post


Link to post
Share on other sites

At least with the German umlauts a mistake has crept in:
"Ä" is not "A", but "Ae"
"ä" is not "a", but "ae"
"Ö" is not "O", but "Oe"
"ö" is not "o", but "oe"
"Ü" is not "U", but "Ue"
"ü" is not "u", but "ue"
"ß" is not "B", but "ss"

I wish you a happy code patching :thumbsup:


App: Au3toCmd              UDF: _SingleScript()                             

Share this post


Link to post
Share on other sites

I'm gonna pretend I never read that ..... and hope for the best. :whistle:


Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites
27 minutes ago, Exit said:

At least with the German umlauts a mistake has crept in

True of course but handling that in the general case isn't possible without external knowledge of the used script (script = writen language).

In general unaccenting boils down to irrecoverable loss of information. That may have unimportant consequences or on the contrary raise very serious issues, depending on context.

For instance unaccenting sentences like "Æ e i åa å o e i åa o å" which means, in some trøndersk dialect, "I am in the river and she is in the river as well" is likely to produce strict nonsense but a human operator can probably recognize the meaning in "ae e i aa a o e i aa o a" if (s)he can read the dialect.

Ligatures are another problem to solve, not really easier.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Very nice gentlemen :)

Concerning the comment made by @Exit, I read somewhere that this kind of substitution cannot (should not?) be done blind.

 

One must know the conventions of both the source and target languages to be successful. 

:)

 


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

I guess that is probably so, and my approach was to convert whatever wasn't standard English into such.

But then I was having to cope with (guess really) what Calibre does when it names folders and files ... problematical when trying to build a library entry path based on ebook title and author name sub folder structure ,,, Calibre likes to truncate titles too, if beyond a certain length, including adding a bracketed ID number at the end, and characters like a question mark for instance get replaced by an underslash. Its SQL database can be queried for original elements, but you don't get the modified variants returned.


Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...