Random Replacement of words using Python


Introduction to Random Replacement of Word

In this article, we are going to learn about the random replacement of words. Random word replacement means that we will randomly select a word from the input text and replace that word with the word that we will select randomly from the list of strings. This process helps us introduce variation and generates different text versions.

As we know python is a free, open-source programming language, which provides us with a range of tools and functionalities by the help of which we could perform random replacement of words. We are going to use the random module for our approach to implement our code. Random modules help us to generate random indices which we could use to select random words from the text.

In this article, we will use Python to understand a specific approach for the random word replacement in a given text.

Syntax

Let us now look at the syntax of the various functions that we will be using in the article.

random.randint(start,stop)

The randint() method returns an integer number of the selected element from the specified range.

  • Start − The first parameter takes an integer and specifies at which position to start.

  • Stop − It takes an integer and specifies at which position to end.

split(separator,maxsplit)

The split() method splits a string into a list.

  • Separator − It is optional, it specifies the separator to use when splitting the string. By default, any whitespace is a separator.

  • maxsplit − It is optional and it specifies how many splits to do. The default value is -1, which is "all occurrences".

join(iterable)

The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

  • Iterable − It is required and any iterable object where all the returned values are strings.

Approach

Let us now look into the approach of the following code −

  • First, we have imported the random module

  • Then we have defined a function called random_word_replacement which would take two arguments. first takes the original text and the second takes a list of strings.

  • After that, the text is split into a list of strings using the split method and stored in the words variable.

  • We have generated random positions for words and word_list and stored them into random_index_in_text and random_index_in_list respectively.

  • And then the randomly chosen word in word_list is replaced by the randomly chosen word in words.

  • The list of word in words variable is joined back to form a string and gets stored in the replaced_text variable.

  • This replaced_text variable is then returned from the function and stored in the "str" variable.

  • Then finally the output string is printed on the console screen.

In the provided example, a list ["Good", "Better", "Best"] is passed as the word_list, and the string "Tutorials Point is a good place for learning" is passed as the text parameter. The resulting replaced text is then printed.

This code randomly selects a word from the input text and replaces it with a random word from the provided list, showcasing a simple example of random word replacement using Python.

Example

Let us first look at the code before moving into the example.

import random
 
def random_word_replacement(text, word_list):
   # Split the text into a list of words
   words = text.split()
 
   random_index_in_text = random.randint(0, len(words) - 1)
   random_index_in_list = random.randint(0, len(word_list) - 1)
	
   words[random_index_in_text] = word_list[random_index_in_list]
	
   replaced_word = ' '.join(words)
   return(replaced_word)
	
list = ["Good", "Better", "Best"]
str = random_word_replacement("Tutorials Point is a good place for learning", list)
 
print(str)

Output

Tutorials Point Best a good place for learning

Now let us understand the code with an example −

Suppose we have the input text − "Tutorials Point is a good place to learn" and input word list: ["Good", "Better", "Best"]

The breakdown of the steps is −

The text is split into a list of words: ["Tutorials", "Point", "is", "a", "good", "place", "for", "learning"].

Suppose two random positions are generated −

Suppose the random position generated for the text variable is the fourth position (corresponding to the word "good" in the text list).

Similarly, the random position generated for the list is the first position (corresponding to the word "Better" in the word list).

So, the word "good" at the randomly chosen position in the text list would be replaced by "Better" from the word list.

The updated list of words then becomes: ["Tutorials", "Point", "is", "a", "Better", "place", "for", "learning"].

The updated list is then joined back to form a string: "Tutorials Point is a Better place for learning".

The replaced text is then returned as the output.

In this example, the code randomly selects the word "good" from the input text and then randomly selects the word "Better" from the word list and replaces it with "good" to make a new modified sentence i.e., "Tutorial Point is a Better place for learning."

Use Case of String Replacement in Python

Random word replacement can be a useful tool in various real-world scenarios that involves text manipulation and natural language processing.

Random word replacement introduces variation in the text which can be useful in creative writing and content marketing etc.

It is also used in a chatbot or virtual assistant to enhance user interaction by avoiding monotonous replies.

By leveraging the flexibility and control offered by random word replacement, businesses, and individuals can enhance the appeal and richness of their textual content, resulting in increased reader engagement and improve user experience.

Conclusion

Random word replacement helps us to introduce creativity and diversity in our written content. This technique is useful in various practical applications such as creative writing, content marketing, chatbots, virtual assistants, and social media platforms, and in many other fields as well.

Random word replacement also helps in natural language processing tasks by providing data preprocessing capabilities and enables text generation with different variations. Word replacement in Python helps writers, researchers, and developers to create engaging and versatile content. It also helps in dynamic content generation and also improves user experience.

Updated on: 04-Oct-2023

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements