Mathematica Asked by Mualpha7 on February 15, 2021
Is there a simple way of finding the position of a specific instance of a substring within a string? For instance, in the sentence "Bob bought his car at the car dealership". Something like StringPosition
would give me the position of both instances of car, namely {{16, 18}, {27, 29}}
, but what if I only wanted the first or the second instance of the word?
If I do a StringPosition
on every word of the sentece, both instances of the word "car" would yield {{16, 18}, {27, 29}}
, as opposed to {16, 18}
for the first instance and {27, 29}
for the second instance, which is not very useful.
This seems to be a recurrent theme for a lot of Mathematica’s NLP functions when they are applied to text: The functions act on each word, without considering the context of the word within the text. Mathematica just gives you all possible answers for each word.
StringPosition[str, WordCharacter .., Overlaps -> False]
{{1, 3}, {5, 10}, {12, 14}, {16, 18}, {20, 21}, {23, 25}, {27, 29}, {31, 40}}
Correct answer by kglr on February 15, 2021
DeleteDuplicates[Sort[Flatten[StringPosition["Bob bought his car at the car dealership", #] & /@ TextWords@"Bob bought his car at the car dealership", 1]]]
gives us
{{1, 3}, {5, 10}, {12, 14}, {16, 18}, {20, 21}, {23, 25}, {27, 29}, {31, 40}}
Answered by Mualpha7 on February 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP