Programming challenge: a generative palindrome machine | by Jari Hiltunen | Sep, 2024


Generative AI, such as ChatGPT, is not very good at producing palindromes, even though the models understand the probability related to the proximity of different words. To learn something new, I wanted to test whether a machine learning (ML) model could be developed to automatically generate palindromes.

From a human perspective, creating a palindrome feels natural. We often start with a word, for example ‘ISO,’ and think about how letters could be added to it so that, when read backward, it would form a meaningful word. At this point, we can add the letter T and get the word ‘ISOT,’ which backward is ‘TOSI.’ However, this is not yet a palindrome because the words are not completely identical. A human would look for a word starting with the letter T and might come up with the word ‘TILA.’ This would give us ‘ISO TILA,’ which backward is ‘ALI TOSI,’ thus forming a palindrome. The process requires continuing and modifying the word until we find a symmetrical whole. This human process is intuitive and dynamic, but challenging for a machine.

The Limitations of Machine Learning in Creating Palindromes

Machine learning, such as LSTM models (long short-term memory), learns to recognize probabilities and dependencies in sequences. However, in the case of palindromes, it’s about strict symmetry — each letter or word mirrors itself in reverse order. LSTM does not naturally handle such a structural requirement. While machine learning can help find probable word combinations, it does not inherently recognize the strict structure required for symmetry.

First Attempts: Combining Random Words

I approached the problem with a simple programming test where I tried randomly combining words (adjectives, verbs, nouns) to see if they would form palindromes. However, this proved inefficient, as finding a palindrome through random sampling is rare. During practical tests, I got results consisting of random words:

Sentence 'laiskansitkeä kevytsora heilahdella' is not palindrome.
Sentence 'eklektinen sinologia tuurata' is not palindrome.
Sentence 'urheilullinen panna murahtaa' is not palindrome.

This approach was not effective, as random combinations do not lead to a symmetrical structure.

A Learning Model for Recognizing Palindromes

I also began by building a learning model, where I fed the neural network words and sentences that were either palindromes or non-palindromes. The goal of the model was to learn to distinguish palindromic sentences from non-palindromic ones. This involved cleaning the words, removing punctuation, and finally analyzing the sequences.

def is_palindrome(text):
return text == text[::-1]

However, based on these experiments, it became clear that machine learning struggles to recognize symmetry. The task of the machine is to learn probabilities, but the symmetry required for palindromes is more complex and requires traditional logical verification algorithms.

Hybrid Model: Combining Machine Learning and Logic

An interesting solution could be a hybrid model that uses both machine learning and logical verification. For example, the machine could suggest word combinations based on word frequency and context, and then an algorithm would verify if the resulting sentence is symmetrical. This approach could leverage machine learning to speed up suggestions, while the final verification would be rule-based.

Experiments and Observations:

  • Limitations of Machine Learning: Testing the LSTM model for generating palindromes did not yield the desired results because the model is not sensitive to reverse structures. The model could identify partial palindromes or give false positives.
  • Word-based Approach: Experimenting with randomly selected words was not an effective way to create palindromes. Generating a palindrome requires a specific structure that cannot be achieved through random combinations.
  • Hybrid Model Could Be Promising: By combining machine learning and logical verification, we can develop a method where the machine suggests probable word combinations, and then a logical algorithm checks if the sentence is a palindrome.

Through these tests, it is clear that while AI and machine learning offer many possibilities, generating palindromes still requires more specialized methods. AI can suggest word combinations, but the unique nature of palindromes requires rule-based structural validation. Machine learning can help accelerate the process, but creating a palindrome based solely on probabilities is challenging.

Python code and tests in Finnish article.



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*