I am trying to expand sentences that include [ ] to indicate options, ( ) to specify groupings and | to indicate the or operator and list all the possibilities. For example:
"Hey [there] you [hood]." should return four sentences:
Hey there you hood. Hey there you. Hey you hood. Hey you.
The final goal will look like this:
Input: "(His|Her) dog was [very|extremely] confused."
Output: His dog was very confused. His dog was extremely confused. His dog was confused. Her dog was very confused. Her dog was extremely confused. Her dog was confused.
I do this using regular expression matching and recursion. I searched for both CPAN and SO under the phrases:
Text extension
extended offers
expanding conditions
extension options
expanding groupings
no luck.
Thanks.
I pretty much edited this question to better reflect its evolution and removed large portions that became obsolete as the question developed. The question posed above is that most of the answers below are trying to solve.
My current status is as follows:
After struggling with the problem above during the day, I have two solutions that are very close to what I want. One of them is mine, and the second is PLT. However, I decided to try a fundamentally different approach.
Using regular expressions and manually parsing these sentences seems like a very ugly way to do something. So I decided instead to write a grammar for my βlanguageβ and use a parser to parse it.
This gives me an extra layer of abstraction and avoids the following scenario described by Damian Conway in Perl Best Practices: [about regular expressions]
cut and paste and change-a-bit-and-oh-now-it-does-not-work-on-all-so-modification let's get this a little more, and watch if not-that-helps-not-he-didn't- but-we-entrusted-now it can be so if we, with a change of what-bit instead of-hmmm-that's-closer, but still not-Perfectly-right-possible-if-I did the third, repetition-non-greedy- instead of-oops-now this-counter-not-match-for-all-possible-I-should-only-post-it-to-PerlMonks.org-see if-they-know-how wrong there
It also makes it easier if the grammar of these expressions needs to change, and I need to subsequently support other constructs.
Last update:
I solved my problem with open source tools . This will decrypt the JSGF version of my input and create a stateful converter. From there, you can go through FST to create all possible results.