For a Natural Language Processing analysis task it's desirable to highlight a text string programmatically according to XML tags that reference specific words or phrases.
The XML tags specify the character ranges in the input string as well as give other information such as identifiers and other codes.
Since character ranges are provided in the XML markup, originally I thought StringReplacePart
would do the job, but it doesn't seem possible to transform the string into more general expressions, eg using Subscript or Style.
For example (please excuse I'm showing only a fragment due to Protected Health Information), given the (partial) input text:
In[566]:= StringTake[I2B2[1, text], -500]
Out[566]= " pain and was treated with Percocet . She went for a \
debridement of her left calf lesion on 10/2/93 and was started \
empirically on IV ceftriaxone which was changed to po doxycycline on \
the day of discharge . A follow-up CT scan was done which did not \
show any evidence for splenomegaly or hepatomegaly . The 1 cm cyst \
which was seen in 10/92 was still present . There was a question of a \
cyst in her kidney with a stone right below the cyst , although this \
did not seem to be clinically significant ."
And the XML tag:
In[581]:= Cases[I2B2[1, tags],
XMLElement[
"EVENT", {id_, start_, end_, text_ -> "a stone", modality_,
polarity_, type_}, _]]
Out[581]= {XMLElement[
"EVENT", {"id" -> "E75", "start" -> "2476", "end" -> "2483",
"text" -> "a stone", "modality" -> "POSSIBLE", "polarity" -> "POS",
"type" -> "PROBLEM"}, {}]}
The following StringReplacePart
works (to ALL CAPS) since the output is still a string:
In[577]:= StringReplacePart[
StringTake[I2B2[1, text], -500],
"A STONE", {2476,
2483 - 1} - ((I2B2[1, text] // StringLength ) - 500)]
Out[577]= " pain and was treated with Percocet . She went for a \
debridement of her left calf lesion on 10/2/93 and was started \
empirically on IV ceftriaxone which was changed to po doxycycline on \
the day of discharge . A follow-up CT scan was done which did not \
show any evidence for splenomegaly or hepatomegaly . The 1 cm cyst \
which was seen in 10/92 was still present . There was a question of a \
cyst in her kidney with A STONE right below the cyst , although this \
did not seem to be clinically significant ."
But the following does not since I'm trying to highlight in Red using Style:
In[578]:= StringReplacePart[
StringTake[I2B2[1, text], -500],
Style["a stone",
Red], {2476, 2483 - 1} - ((I2B2[1, text] // StringLength ) - 500)]
Out[578]= StringReplacePart[" pain and was treated with Percocet . \
She went for a debridement of her left calf lesion on 10/2/93 and was \
started empirically on IV ceftriaxone which was changed to po \
doxycycline on the day of discharge . A follow-up CT scan was done \
which did not show any evidence for splenomegaly or hepatomegaly . \
The 1 cm cyst which was seen in 10/92 was still present . There was a \
question of a cyst in her kidney with a stone right below the cyst , \
although this did not seem to be clinically significant .", \!\(\*
StyleBox["\<\"a stone\"\>",
StripOnInput->False,
LineColor->RGBColor[1, 0, 0],
FrontFaceColor->RGBColor[1, 0, 0],
BackFaceColor->RGBColor[1, 0, 0],
GraphicsColor->RGBColor[1, 0, 0],
FontColor->RGBColor[1, 0, 0]]\), {413, 419}]
Are there functions similar to StringReplacePart that can output more general expressions? Or in any case how to perform the desired highlighting with Subscript and Style?
Row
) is trivial, styling multiple sections, potentially overlapping, becomes far from trivial. $\endgroup$