Write a program to read a file ‘Story.txt’ and create another file, storing an index of Story.txt, telling which line of the file each word appears in. If word appears more than once, then index should show all the line numbers containing the word.

Here’s a Python program that reads a file called ‘Story.txt’, creates another file called ‘Index.txt’, and stores an index of the words in ‘Story.txt’, indicating the line numbers where each word appears:

def create_word_index(input_file, output_file):
    word_index = {}

    with open(input_file, 'r') as file:
        for line_number, line in enumerate(file, start=1):
            words = line.strip().split()
            for word in words:
                if word in word_index:
                    word_index[word].append(line_number)
                else:
                    word_index[word] = [line_number]

    with open(output_file, 'w') as index_file:
        for word, line_numbers in word_index.items():
            index_file.write(f"{word}: {', '.join(map(str, line_numbers))}\n")

    print("Word index created successfully.")


input_file = "Story.txt"
output_file = "Index.txt"

create_word_index(input_file, output_file)

 

Make sure you have a text file named ‘Story.txt’ in the same directory as the Python script. The program reads the file line by line and splits each line into words. It maintains a dictionary called word_index where the keys are words and the values are lists of line numbers where the word appears.

After processing the input file, the program writes the word index to the output file. Each line in the output file represents a word and the corresponding line numbers where it appears, separated by commas.

To run the program, specify the input file name as ‘Story.txt’ and the output file name as ‘Index.txt’. The resulting index file will be created or overwritten with the word index information.

Please note that this program treats words in a case-sensitive manner. If you want to ignore case, you can convert all the words to lowercase during processing by modifying the line words = line.strip().split() to words = line.strip().lower().split().


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *