1

I am piloting a language experiment and would like to test several masking options to interrupt the orthographic processing of my stimuli. One option that I would like to test is consonant strings length-matched to my stimuli. For example, I have a stimulus "bakers slice loaves" and I would like to populate that row in a new column with something like "ntjwsk mwsfp jklwsc", for stimulus "cars drive miles" I would populate that row of the new column with something like "tplk wsdpt bvtrl"

Is there a way to do this?

jfallon
  • 11
  • 2
  • You could start by figuring out a function that does this string matching. If all your examples are just words separated by whitespace, you can try something like this https://stackoverflow.com/questions/12761510/python-how-can-i-calculate-the-average-word-length-in-a-sentence-using-the-spl to get a list of the words. That way you can use https://stackoverflow.com/questions/2823316/generate-a-random-letter-in-python to create random consonant words for each original one. Let's say your function is randomize(sentence). Then you can do ``` df['new'] = df['original'].apply(randomize) ```. – Harpe Jan 13 '22 at 16:14
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 22 '22 at 21:20

1 Answers1

0

The solution

If you aim for random strings of consonants, the following would work:

import random
import pandas as pd

df = pd.DataFrame({"stimulus": ["bakers slice loaves", "cars drive miles", "winter is coming"]})

def transform(c):
    return random.choice("bcdfghjklmnpqrstvwxz")

df["transformed_stimulus"] = df.apply(lambda row: "".join([y if y in [" "] else transform(y) for y in row["stimulus"]]), axis=1)

Otherwise, you can also define your own rules in transform function for transforming each letter.

Background for the solution

The solution utilizes list comprehension for handling the individual stimulus strings first by running each letter through the transform function. Then the str.join method joins the transformed letters again as a complete string. The second list [" "] omits whitespaces and you can add other characters too. E.g., [" ", "a", "e"] omits whitespace, a, and e characters.

heilala
  • 770
  • 8
  • 19