Message Boards Message Boards

0
|
4221 Views
|
13 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Can I use the Wolfram Language to generate Vector images of text?

I am designing a psycholinguistics experiment in which I will be presenting a large number of sentences in a self-paced reading task.

For instance I would need the following images if I were to use this particular sentence:

--- -------- - ----- ---- --- --------- ------ -- - ---- -- --- ---- ---------- --------.

For instance - ----- ---- --- --------- ------ -- - ---- -- --- ---- ---------- --------.

--- -------- I would ---- --- --------- ------ -- - ---- -- --- ---- ---------- --------.

--- -------- - ----- need the --------- ------ -- - ---- -- --- ---- ---------- --------.

--- -------- - ----- ---- --- following images -- - ---- -- --- ---- ---------- --------.

--- -------- - ----- ---- --- --------- ------ if I ---- -- --- ---- ---------- --------.

--- -------- - ----- ---- --- --------- ------ -- - were to --- ---- ---------- --------.

--- -------- - ----- ---- --- --------- ------ -- - ---- -- use this ---------- --------.

--- -------- - ----- ---- --- --------- ------ -- - ---- -- --- ---- particular sentence.

In an evenly spaced font of course, but you get the idea. Also, the experiment involves four factorial conditions, in which both either, or neither of the words in view are presented in a less legible color. I designed the original stimulus using a web-based html to image converter and did every image excrutiatingly by hand. That is not plausible for the full experiment. It seems to me that this task should be fairly straightforward and well within the power of the Wolfram Language.

POSTED BY: Kyle Zimmer
13 Replies

Longer answer: see the attached notebook that implements something along the lines of what I think you are wanting. Of course this is just a quick sketch (20 minutes development time) but it could be expanded in a variety of ways....

--David

Attachments:
POSTED BY: David Reiss

Do look at the notebook posted above, but for others that want a quick copy and paste of the final example code here it is:

ClearAll[modifyWords];

modifyWords[{word1_String, word2_String}, {style1_, style2_}] :=

  Switch[{style1, style2},

   {None, None},
   {word1, word2},

   {None, _},
   {word1, Style[word2, style2]},

   {_, None},
   {Style[word1, style1], word2},

   {_, _},
   {Style[word1, style1], Style[word2, style2]}
   ];

ClearAll[PsychoSentence];

Options[PsychoSentence] = {ReplacementCharacter -> "-"};

PsychoSentence[sentence_String, {style1_, style2_}, 
   OptionsPattern[]] :=

  Module[{splitSentence, dashedTemplate, stringTable, 
    processedSentences, replacementCharacter},

   replacementCharacter = OptionValue[ReplacementCharacter];

   splitSentence = Partition[StringSplit[sentence], 2, 2, 1, ""];

   dashedTemplate = 
    Map[StringReplace[#, _ -> replacementCharacter] &, 
     splitSentence, {2}];

   splitSentence = modifyWords[#, {style1, style2}] & /@ splitSentence;

   stringTable = Table[
     Riffle[Flatten[
       ReplacePart[dashedTemplate, i -> splitSentence[[i]]]], 
      " "], {i, Length[splitSentence]}];

   processedSentences = Row /@ stringTable;
   processedSentences = Riffle[processedSentences, "\n"];

   CellPrint[
    TextCell[Row[processedSentences], "Text", FontFamily -> "Courier",
      FontSize -> 11, ShowStringCharacters -> False]]

   ];

PsychoSentence[sentence_String] := 
 PsychoSentence[sentence, {None, None}]

And here is an example of it's use (it prints out a formatted Cell as its output):

PsychoSentence["The time has come for all good iguanas to have some kelp", {LightPurple, Green}]

enter image description here

POSTED BY: David Reiss

Kyle, see the attached updated notebook which now has an additional function, PsychoSentenceSVG, that takes a directory path as an additional argument and which exports each individual line to its own svg file. --David

Attachments:
POSTED BY: David Reiss

You are amazing David.

POSTED BY: Kyle Zimmer

...Blushes...

Actually, it's the Wolfram Language that's amazing.

Spread the word!

POSTED BY: David Reiss
Posted 10 years ago

It is not the kind of presentation you were asking for, but if you want another impressive example of what a few dozen lines of Mathemaica can do for presenting carefully positioned and formatted and highlighted text to a viewer then you might search the forum for the word spritz

POSTED BY: Bill Simpson

Bill, yes, that's a wonderful example.

Kyle, send me an email at the address in my Wolfram Community profile (click on my name) and we can finish up via email.

Best, David

POSTED BY: David Reiss

Short answer: yes.

POSTED BY: David Reiss
Posted 10 years ago

Maybe you like this: Export["pi-shu.svg", Rasterize@"pi.shu.edu.cn"]

POSTED BY: Fuping Tan

Thank you so much David. That notebook is fantastic. I suppose there is probably a way to extend it so that it separates the final product into its individual lines and exports .svg files of a set size for each? Fu-Ping, would the function you gave me do the export part? I'm not sure I understand how to implement it. I apologize for my incompetence.

Thanks again both of you, this is extremely helpful to me.

POSTED BY: Kyle Zimmer

I absolutely will. My adviser will love this. He already uses Mathematica to do all of his cognitive modelling.

Would it be too much to ask to ask how to put a period at the end of each sentences and accept an argument that defines how the sentences should be named?

Ideally, I could take a matrix as below,

[Sentence | SentenceNumber | Factor 1 (1 grey, 2 black) | Factor 2 (1 grey, 2 black)]

plug that array into the function, and generate all of my sentence images at once along with a matrix as below,

[Sentence | SentenceNumber | Factor 1 (1 grey, 2 black) | Factor 2 (1 grey, 2 black) | /Images/sXwY.svg]

where X is the sentence number and Y is the window number within that sentence. I feel bad asking for more, haha, but you have already saved me days of work, so I feel it can't hurt to ask.

And hm. I just found out "E-Prime 2.0 allows for image formats .bmp, .jpg, .jpeg, gif, .png, .tif, .tiff, .emf, *.wmf". I can change that part no problem, but do you have a suggestion? The file sizes can't get too big, because we are measuring reaction times, but the clarity of the text is also a critical aspect of the design. Do you have a suggestion? I tried EFM and WFM but they are nasty looking compared to the SVG.

Thanks again, you make me want to read "A New Kind of Science", and I will definitely put a blurb about you in my website with a link to yours.

POSTED BY: Kyle Zimmer

Thanks Bill.

POSTED BY: Kyle Zimmer

Just for people's reference, attached is the updated notebook.

And here is the associated function:

ClearAll[PsychoSentenceSVG, modifyWords];

modifyWords[{word1_String, word2_String}, {style1_, style2_}] :=

  Switch[{style1, style2},

   {None, None},
   {word1, word2},

   {None, _},
   {word1, Style[word2, style2]},

   {_, None},
   {Style[word1, style1], word2},

   {_, _},
   {Style[word1, style1], Style[word2, style2]}
   ];


Options[PsychoSentenceSVG] = {ReplacementCharacter -> "-", 
   FontFamily -> "Courier", FontSize -> 12};

PsychoSentenceSVG[sentence_String, path_String, 
   fileName_String, {style1_, style2_}, OptionsPattern[]] :=

  Module[{splitSentence, dashedTemplate, stringTable, 
    processedSentences, replacementCharacter, fontFamily, fontSize},

   replacementCharacter = OptionValue[ReplacementCharacter];
   fontFamily = OptionValue[FontFamily];
   fontSize = OptionValue[FontSize];

   splitSentence = Partition[StringSplit[sentence], 2, 2, 1, ""];

   dashedTemplate = 
    Map[StringReplace[#, _ -> replacementCharacter] &, 
     splitSentence, {2}];

   splitSentence = modifyWords[#, {style1, style2}] & /@ splitSentence;

   stringTable = Table[
     Riffle[Flatten[
       ReplacePart[dashedTemplate, i -> splitSentence[[i]]]], 
      " "], {i, Length[splitSentence]}];

   processedSentences = Row /@ stringTable;
   processedSentences = 
    TextCell[#, "Text", FontFamily -> fontFamily, 
       FontSize -> fontSize, ShowStringCharacters -> False] & /@ 
     processedSentences;

   MapIndexed[
    Export[FileNameJoin[{path, 
        "s" <> fileName <> "w" <> ToString[#2[[1]]] <> ".svg"}], #1, 
      "SVG"] &, processedSentences]

   ];
Attachments:
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract