Use strings.Replace () to replace all the values of a dictionary in a string

1

I have used this function a couple of times and now I have an exercise which I can not even run. I have a string ( cadena ) and a dictionary ( griegoDicc ), I need to replace each Greek character with the numeric value. But "result" is equal to "string", it does not perform the replacement. Will anyone know why this happens?

package main

import (
    "log"
    "strings"
)

func main() {
    cadena := "ΨφΔξβΨμλΓΛ ΡΘξΡΛξΠμφ λΨξΘΓΠμΓΔ ΨξΛφζΡμλΓ ΔξΛφζΡμλΓ ΔξΛφζΡμλΓ ΔξΨφΔξΡΛξ μηφΔΨξΔΡξ μΨλΓΘξαΓΔ ΡΩΓΘξΣΡΔξ ΡΩξλμΨΩΨξ ΓξΛΓξΩΨζΛ ΡξφΠηΓΛΔΓ ΔξΦΓξΘηξλ μΨΩΨξΔφΠΩ φΘφβΨξΓζμ φΡμΨΩξΛΓΘ ξΞμΨΣφΩαφ ΓΘξηΩφΔΓΘ ξΔΡΛξΘηΔξ ΦξΛΨΘξΛφζ μΡΘξΔΡΛξβ ηΩΔΨξμΡΘΞ ΨΩΔΡΩξΓΛξ ΠμΓΩξΞηΡζ ΛΨξΓμΠΡΩλ φΩΨξΘΓΛηΔ ξΓΛξΠμΓΩξ ΞηΡζΛΨξΓμ ΠΡΩλφΩΨξΘ ΓΛηΔξΦξΛΨ ΘξΛφζμΡΘξ ΔΡΛξβηΩΔΨ ξμΡΘΞΨΩΔΡ ΩξΓΛξΠμΓΩ ξΞηΡζΛΨξΓ μΠΡΩλφΩΨξ ΘΓΛηΔξΦξΛ ΨΘξΛφζμΡΘ ξΔΡΛξβηΩΔ ΨξμΡΘΞΨΩΔ ΡΩξΓΛξΠμΓ ΩξΞηΡζΛΨξ ΓμΠΡΩλφΩΨ ξΘΓΛηΔξαΨ μΨξΘΡΓΩξΡ λΡμΩΨΘξΛΨ ΘξΛΓημΡΛΡ ΘξϑηΡξΘηΞ φβΨΘξαΨΩΘ ΡΠηφμξϑηΡ ξΘηΞφβΨΘξ αΨΩΘΡΠηφμ ξαΨμΨΩΓΔΨ ΘξΔΡξΠΛΨμ φΓξΣφΣΓβΨ ΘξΨξεημΡβ ΨΘξαΨΩξΠΛ ΨμφΓξβΨμφ μξΨξεημΡβ ΨΘξαΨΩξΠΛ ΨμφΓξβΨμφ μξΨξεημΡβ ΨΘξαΨΩξΠΛ ΨμφΓξβΨμφ μ"
    // caracteres := "ΨφΔξβμλΓΛΡΘΠζηαΩΣΦΞϑε "
    var resultado string
    griegoDicc := [][]string{{"Ψ", "700"}, {"φ", "500"}, {"Δ", "4"}, {"ξ", "60"}, {"β", "2"}, {"μ", "40"}, {"λ", "30"}, {"Γ", "3"}, {"Λ", "30"}, {"Ρ", "100"}, {"Θ", "9"}, {"Π", "80"}, {"ζ", "7"}, {"η", "8"}, {"α", "1"}, {"Ω", "800"}, {"Σ", "200"}, {"Φ", "500"}, {"Ξ", "60"}, {"ϑ", "9"}, {"ε", "5"}}

    for _, equivalencia := range griegoDicc {

        resultado = strings.Replace(cadena, equivalencia[0], equivalencia[1], -1)

    }
    log.Print(resultado)

}
    
asked by Leandro Gutierrez 23.01.2018 в 19:47
source

2 answers

3

In the first iteration, resultado will have the replacement of "Ψ" by "700" in cadena :

resultado = strings.Replace(cadena, "Ψ", "700", -1)


In the second iteration, resultado will have the replacement of "φ" by "500" in cadena :

resultado = strings.Replace(cadena, "φ", "500", -1)


In cadena ?!
There is the problem. You are always replacing in the original string. On the other hand, if you assign everything to resultado at the beginning, and always replace it in resultado it would work:

cadena := "ΨφΔξβΨμλΓΛ ΡΘξΡΛξΠμφ λΨξΘΓΠμΓΔ ΨξΛφζΡμλΓ ΔξΛφζΡμλΓ ΔξΛφζΡμλΓ ΔξΨφΔξΡΛξ μηφΔΨξΔΡξ μΨλΓΘξαΓΔ ΡΩΓΘξΣΡΔξ ΡΩξλμΨΩΨξ ΓξΛΓξΩΨζΛ ΡξφΠηΓΛΔΓ ΔξΦΓξΘηξλ μΨΩΨξΔφΠΩ φΘφβΨξΓζμ φΡμΨΩξΛΓΘ ξΞμΨΣφΩαφ ΓΘξηΩφΔΓΘ ξΔΡΛξΘηΔξ ΦξΛΨΘξΛφζ μΡΘξΔΡΛξβ ηΩΔΨξμΡΘΞ ΨΩΔΡΩξΓΛξ ΠμΓΩξΞηΡζ ΛΨξΓμΠΡΩλ φΩΨξΘΓΛηΔ ξΓΛξΠμΓΩξ ΞηΡζΛΨξΓμ ΠΡΩλφΩΨξΘ ΓΛηΔξΦξΛΨ ΘξΛφζμΡΘξ ΔΡΛξβηΩΔΨ ξμΡΘΞΨΩΔΡ ΩξΓΛξΠμΓΩ ξΞηΡζΛΨξΓ μΠΡΩλφΩΨξ ΘΓΛηΔξΦξΛ ΨΘξΛφζμΡΘ ξΔΡΛξβηΩΔ ΨξμΡΘΞΨΩΔ ΡΩξΓΛξΠμΓ ΩξΞηΡζΛΨξ ΓμΠΡΩλφΩΨ ξΘΓΛηΔξαΨ μΨξΘΡΓΩξΡ λΡμΩΨΘξΛΨ ΘξΛΓημΡΛΡ ΘξϑηΡξΘηΞ φβΨΘξαΨΩΘ ΡΠηφμξϑηΡ ξΘηΞφβΨΘξ αΨΩΘΡΠηφμ ξαΨμΨΩΓΔΨ ΘξΔΡξΠΛΨμ φΓξΣφΣΓβΨ ΘξΨξεημΡβ ΨΘξαΨΩξΠΛ ΨμφΓξβΨμφ μξΨξεημΡβ ΨΘξαΨΩξΠΛ ΨμφΓξβΨμφ μξΨξεημΡβ ΨΘξαΨΩξΠΛ ΨμφΓξβΨμφ μ"

var resultado string
griegoDicc := [][]string{{"Ψ", "700"}, {"φ", "500"}, {"Δ", "4"}, {"ξ", "60"}, {"β", "2"}, {"μ", "40"}, {"λ", "30"}, {"Γ", "3"}, {"Λ", "30"}, {"Ρ", "100"}, {"Θ", "9"}, {"Π", "80"}, {"ζ", "7"}, {"η", "8"}, {"α", "1"}, {"Ω", "800"}, {"Σ", "200"}, {"Φ", "500"}, {"Ξ", "60"}, {"ϑ", "9"}, {"ε", "5"}}

//ACÁ asignamos el valor original a resultado
resultado = cadena
for _, equivalencia := range griegoDicc {
    //y acá reemplazamos sobre el valor de resultado
    resultado = strings.Replace(resultado, equivalencia[0], equivalencia[1], -1)
}
log.Print(resultado)

link

    
answered by 23.01.2018 / 21:47
source
0

Actually, the replacement of the characters of the Greek alphabet by the numerical values of the Greek table is one of the many options for the translation / decoding of messages.

Rather what should be done is something similar to what is done with Roman numeracy. You have to look for the pattern, either as an index of arrangement, or by interpolation and also because not by UNICODE code (the difference of the value of a letter of our alphabet to a Greek one, is a constant value 848).

There are several ways to try to interpret that text.

All this because what I read the Greek translation of that text is not coherent if it is translated literally.

    
answered by 28.02.2018 в 02:07