Python;在分隔句中的单词后面添加空白屏幕

2024-07-05 14:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个心理实验,一个单词一个单词地呈现句子,然后用一个单独的单词来收集答案。我第一部分的输入是一个完整的句子,我在代码中把它拆分成单词,然后在每一帧上更新显示句子中的当前单词300毫秒。现在我是一个正在编码的noob,我不知道如何在每个单词出现后增加300毫秒的空白屏幕。在

句子:“约翰踢了水桶” 分成词,呈现为:约翰(300毫秒)空白屏幕(300毫秒)踢(300毫秒)空白屏幕(300毫秒)等

<>我已经收录了我的相关代码,但不知怎么搞不清如何在更新词之间获得空白屏幕。我尝试过创建一个空白屏幕图像,并将其插入到不同位置的循环中,但我无法理解。我们将非常感谢您的帮助!在

    # get the sentence for this trial and split it into a list of words:
words = Sentence.split() 

# count them (the length of the list): 
numWords = len(words)

# how long the text component should display for: 
totalDuration = numWords * 0.3 # time in seconds 

fixationDuration = 1.5 # put in whatever your actual fixation duration is 
currentWordIndex = -1 # will be useful in "each frame" tab

# t is the time elapsed in the trial. 
# Calculate what word we should be up to by seeing how many 300 ms periods have elapsed so far (& force it to be an integer): 
checkIndex = int((t-fixationDuration)/0.3) 

# see if we need to switch to a new word (including the very first one): 
if checkIndex < numWords:
    if checkIndex != currentWordIndex: 
        currentWordIndex = checkIndex 
        text_1.setText(words[currentWordIndex]) # update to the current word

几次迭代之后,我有了一个独立的脚本,它可以拆分一个句子,在每次测试中会显示一个固定十字,持续1.5秒(90帧),然后在分割后的句子中显示每个单词300毫秒的空格。但是,我无法在我的大脚本中使用它,因为它需要200个句子作为输入。当我试图把它融入到我的精神病学的构建器部分时(我想让它与学生保持兼容),它只会一遍又一遍地呈现第一个句子(固定交叉、单词、空白等到句尾,然后再固定和同一个句子)。每次试验都应该这样:

  • 固定交叉1.5秒/90帧(60Hz)
  • 单词/空白/单词/空白句子呈现
  • 参与者必须回答的目标词

现在目标单词是Builder中的一个单独的例程,但这似乎不是脚本不能工作的原因。它无法跟踪它应该呈现哪一个句子,因为它现在没有内置在其中(很明显,我用我的句子变量替换了“John kicked the bucket”这个句子,它是一个所有句子的列表),并且只呈现同一个句子,不管它拆分的句子中有多少个单词。这就是为什么我认为我需要checkIndex变量。我想我需要某种索引来检查每一帧显示了多少帧,这样它就知道是否要将我的文本信息更新到下一个单词。在

^{pr2}$

Tags: thetoin脚本if屏幕be单词
3条回答

在 请参阅下面的解决方案。在

这里要记住的一件重要的事情是,你应该使用离散的监视器帧/更新而不是连续时间来为视觉刺激计时。例如,使用core.wait(0.300)通常会导致文本到达下一帧,在某些试验中实际间隔为316.7ms,而在其他试验中则为300ms。在监视器更新之前,visual.Window.flip()函数将停止所有操作,因此在flip()上循环将使循环与监视器同步-非常有用!在

from psychopy import visual
words = 'John kicked the bucket'.split()
win = visual.Window()
text_1 = visual.TextStim(win)

for word in words:
    # Set text for this trial
    text_1.text = word

    # Show text for 300 ms = 18 frames on 60Hz monitor
    for frame in range(18):
        text_1.draw()
        win.flip()

    # Blank screen for 300 ms = 18 frames on 60Hz monitor
    for frame in range(18):
        win.flip()  # OBS: no drawing in this loop. Just a blank screen.

它确实需要你的计算机与监视器同步。如果您运行的刷新率与60赫兹不同,请更改帧数,以便获得所需的300毫秒。如果您使用的是精神病患者,请运行Coder>;demos>;timing>;timesByFrames,那么在60Hz的监视器上,您应该会看到一个大约16.667毫秒的窄分布。在

(代表运营商发布)。在

问题已经解决了。此代码进入生成器的“开始例程”部分:

 # get the sentence for this trial and 
 # split it into a list of words: 
 words = Sentence.split() 

 text_2.setText("+") #draw a fixation cross at the beginning of each trial for 90 frames
for frameN in range (90):
    text_2.draw()
    win.flip()

for word in words:

    text_2.setText(word) #set current word in Builder placeholder text variable
    for frameN in range (18):
           text_2.draw() #present current word for 18 frames
           win.flip()

    for frameN in range(18): #present blank screen for 18 frames
           win.flip()
import time

words = sentence.split()

for word in words:
    #place something to undo changes in blank screen, you can use try except
    text_1.setText(word)
    time.sleep(.3)
    text_1.setText("") 
    #make changes during blank screen, put images or something
    time.sleep(.3)       

如果我能正确理解你的问题,我想这应该是你想要的。代替text_1.setText(""),当你想显示空白屏幕时,放上你想做的任何事。在

相关问题 更多 >