Word to number conversion and vice versa

Asked

Viewed 461 times

2

Is there any way to turn whole words into unique numbers so that I can do the reverse process if necessary? Like it was a unique ID of that word, like

Palavra -> Numero -> Palavra
oi -> 09368 -> oi
  • Give more details on how this should be done. Show what you have already done.

  • @bigown did nothing at first, I wonder if there is something predefined for PHP or existing, if not, what comes to mind would be to create array with letters with predefined codes type A => 01, B = 02....

  • @rray is not what I seek.

  • 1

    @Elaine needs to establish the criteria, is there a direct relation? The numbers are calculated by the word? It’s just a code previously determined?

  • Huum you want to encrypt a word in number and then decrypt? maybe p use ASCII codes

  • @bigown there is no direct relation to the word, it is like a car plate that identifies that specific car.

  • @rray will be? I tried to use the functions ord and chr but they only go with one character at a time, and I would need an integer representing the word so that I can decipher it as you said yourself.

  • @Elaine these codes are numbers or are numeric texts?

  • 1

    @Elaine your comments are contradictory. Define what you really want, have direct relationship or have no?

  • @bigown is required to be numbers (int). Maybe I misunderstood, the word number should be able to indicate which word generated it. You understand?

  • @Elaine what is the criteria to get this code? I had already assembled the answer without having the direct relationship.

  • @bigown nothing specific, performing the function is enough, you have ideas of how to do?

  • 1

    @Elaine No. There are numerous ways to do this. You need to have a criterion. You need to have rules on how that number should be found. You have to have the constraints of what you can and cannot do. How to deal with collisions, just to give an example.

  • @bigown have no idea about these points, as I said "what comes to mind would be to create array with letters with predefined codes type A => 01, B = 02...."

  • I gave an answer based on this, but if you want to calculate, you’d have to have criteria.

  • @Elaine vc could specify in the question some restrictions regarding the numbers that will be generated from a word, for example: Can be negative number? There is a maximum or minimum number?

  • @Elaine I would do this with a mathematical calculus, so that from each word I would generate a number for it, calculating vowels and consonants, there are other forms, but this may be an option.

Show 13 more comments

1 answer

5


I will keep this answer here because it can help someone, the question was better clarified in the comments and the problem was another. But the other cannot be solved without having criteria to define it, which can even be complicated if you need a conversion to a single number. Even if it is guaranteed that there can only be words (therefore short), the number of combinations to avoid repetition is so large that it is better to leave the text even.

I imagine what you want is the dictionary, or array associative, as this feature is best known in PHP:

$palavras = array(
    "oi" => 09368,
    "tchau"   => 01234,
    "palavra" => 34986,
    "abobrinha" => 72494);
$codigo = $palavras["oi"]; //busca rápida
$palavra = array_search(01234, $palavras); //busca lenta

The first to be done through function hash, is very fast (complexity O(1)). The search for the value is slow (complexity O(n)). It is possible to obtain complexity O(log n) which is close to O(1) if the array is guaranteed to have the values sorted. Then it is possible to perform a binary search.

It is still possible, if memory is not an impediment, to have a second array with the values reversed. There it is possible to search in O(1) in both. Thus:

$palavras = array(
    09368 => "oi",
    01234 => "tchau",
    34986 => "palavra",
    72494 => "abobrinha");

I put in the Github for future reference.

I kept the zeroes meaningless, but obviously they’re not necessary

Browser other questions tagged

You are not signed in. Login or sign up in order to post.