0

here, I am trying to spell check a txt file. first half of my code outputs original (var word) and corrected, if any, (var spell(word)). my replacement code uses a data structure like {'zero':'0', 'temp':'bob', 'garbage':'nothing', 'garnvsh': 'garnish'}. Now I would like to produce the same data structure from the loop dynamically. Can you, please, help me figure it out how to output similar data structure with var word and var spell(word) with first half of the code?

import os, os.path
from textblob import TextBlob
from autocorrect import spell
import fileinput
import json

data = []

with open("input.txt", "r") as my_file:
    for line in my_file.readlines():
        zen = TextBlob(line)
        for word in zen.words:
            word, spell(word)

replacements = {'zero':'0', 'temp':'bob', 'garbage':'nothing', 'garnvsh': 'garnish'}

with open('../input.txt') as infile, open('../output.txt', 'w') as outfile:
    for line in infile:
        for src, target in replacements.iteritems():
            line = line.replace(src, target)
        outfile.write(line)

1 Answer 1

1

I suppose you want to create dictionary which its keys are word and values are spell(word).

my_dict = { word: spell(word) for word in zen.words }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.