如何添加所选短语的计数? [英] How can I add to the tally of a chosen phrase?
问题描述
因此,我希望能够将文件中包含的某个特定短语添加到计数中.但是我不知道从哪里开始.
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.
让我解释一下:
我有一个名为setPhrases.txt
的.txt文件.此文件包含以下内容:
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
我目前能够采用(n)
个热门短语(因此,具有最高计数的短语)并将它们显示在屏幕上的框中.
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:
一旦用户确定他/她已经选择了他们想要的短语,然后按OK(确定),然后应该在屏幕上识别出该短语/短语,并应在+1中添加到文件中的提示中.
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.
- 因此,在这种情况下,
Hi my name is Dave.
计数将上升一个,并将变为11,然后将其另存为文件:Hi my name is Dave., 11
,而无需对该文件中的其他任何短语进行任何更改
- 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.)
这是它检查是否已按下OK
并继续进行的部分:
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.
推荐答案
您应该将csv读取器读取的数据存储在列表中,以便在修改后可以创建写入器并写入文件.
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.
with open("setPhrases.txt") as out_file:
writer = csv.writer(out_file)
for row in file_rows:
spamwriter.writerow(row[0],row[1])
您可以通过搜索列表来找到用户单击的字符串,或存储显示的字符串的标记来找到要增加的正确值.
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.
这篇关于如何添加所选短语的计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!