Home > Mobile >  How to find words repeated 5 times in a text file in python?
How to find words repeated 5 times in a text file in python?

Time:01-10

I was trying to code a word counter program which selecting word from a random text file. The condition is that words that repeat 5 or more times will be written on the screen. I tried many examples. I tried count() function with if condition like this:

if fullText.count(word) > 5:...

but it does not work.

Here is my code:

from tkinter import *
from tkinter import filedialog

main = Tk()
main.title(".TXT File Word Counter")
main.resizable(height=FALSE, width=FALSE)
main.geometry('500x400')
main.configure(bg='#757575')

labelfont = ("Arial", 14, "bold")

result = dict()

def clear_text():
    textfield.delete(0, END)
    ShowCountedWords.delete(1.0, END)

def open_file():
    main.filename = filedialog.askopenfilename()

def count_word(file):
    fileOpen = open(str(file), 'r')
    fullText = fileOpen.readlines()
    fileOpen.close()
    for word in textfield.get().split(', '):
        for text in fullText:
            if word in result:
                result[word] = result[word]   text.count(word)
            else:
                result[word] = text.count(word)
    ShowCountedWords.delete(1.0, END)
    for key, value in result.items():
        ShowCountedWords.insert('1.0', '{0} : {1} \n'.format(key, value))

    result.clear()


heading = Label(main, text=".TXT File Word Counter")
heading.place(x=150, y=2)
heading.config(bg="#757575", font=labelfont, fg="#ffffff")

textfield = Entry()
textfield.place(x=3, y=30)
textfield.config(width=81, borderwidth=2)

btnSelectFile = Button(main, text="Select .txt File", command=lambda : open_file())
btnSelectFile.place(x=4, y=60)
btnSelectFile.config(width=20, bg="#66BB6A")

btnCount = Button(main, text="Count Words", command=lambda : count_word(main.filename))
btnCount.place(x=173, y=60)
btnCount.config(width=20, bg="#42A5F5")

btnClear = Button(main, text="Clear", command=lambda : clear_text())
btnClear.place(x=344, y=60)
btnClear.config(width=20, bg="#ef5350")

ShowCountedWords = Text(main, height=18, width=61)
ShowCountedWords.place(x=4, y=100)
ShowCountedWords.config(bg="#616161", fg="#ffffff")


main.mainloop()

What should I do? (tkinter is unimportant at this problem.)

CodePudding user response:

fullText.replace(",","")
fullText.replace(".","")
for word in textfield.get().split(' '):
   #your code

if you split the string with ", " most words will get skipped

CodePudding user response:

In your loop "for text in fullText" you are iterating through a list of lines from the file you opened, not a list of words to add to a dictionary. Your logic is correct, iterate through the list, find a word, and if it already exists in the dictionary add a count to it and if not, make it a new instance with count = 1. However, you need to split each line from the list of lines into a sublist of words, and those are the words you want to be iterating through.

CodePudding user response:

In this case, it's easier to handle the file content as one big string, not as a list of lines:

with open(file, "r") as f:
    fulltext = f.read().lower()
    for word in textfield.get().lower().split(", "):
        count = fulltext.count(word)
        if count >= 5:
            result[word] = count
  •  Tags:  
  • Related