So I want to be able to add to the tally of a certain phrase that is contained in a file. However I have no clue on where to start.
Let me explain a little more:
I have a .txt file that is called setPhrases.txt
. This file contains this with in it:
What is your name?, 9
What time is dinner?, 8
Thank-You., 9
I have done all my homework., 7
Can you bring me a drink Please?, 6
Hi my name is Dave., 10
I am currently able to take (n)
number of the top phrases (So the ones with the highest count) and display them in a box on the screen.
If the user chose the biggest box (In this case the one that says "Hi my name is Dave." in it) then it will be displayed in the top screen/box. As you can see below:
Once the user has decided he/she has chosen the phrase they want they will then press OK which should then recognize the phrase/phrases on screen and should add to the tally in the file by +1.
- So in this case, the
Hi my name is Dave.
tally, would go up by one and will change to 11 and will be saved to the file as:Hi my name is Dave., 11
, without making any changes to any of the other phrases in that file.
Here is the full code.(Sometimes it is easier to have full code.)
This is the part where it checks to see if OK
has been pressed and then proceeds on:
elif textSelected == "OK":
self.deletePanes()
self.createPhrases()
This is how I open the file:
def get_n_nouns(self, n):
#Returns the n most common nouns
with open("setPhrases.txt") as in_file:
reader = csv.reader(in_file)
data = [[row[0], int(row[1])] for row in list(reader)]
return sorted(data, key=lambda x: -x[1])[:n]
This is where I write in to Pane/Box:
def createPhrases(self):
print("createPhrases")
self.deletePanes()
self.show_keyboard = False
self.show_words = False
self.show_phrases = True
self.show_terminal = True
words = self.get_n_nouns(2)
for word, count in words:
self.addPane("{}: {}".format(word, count), WORDS)
self.addPane("Boxes", PHRASE)
self.addPane("Keyboard", PHRASE)
self.addPane("OK", PHRASE)
self.drawPanes()
Any help or comments are much appreciated.
You should store the data read by you csv reader in a list, so that when modified, you can create a writer and write to the file.
You can find the correct value to increment by searching through the list to find the string that the user clicked, or store the indicies of the displayed strings.