compromise icon indicating copy to clipboard operation
compromise copied to clipboard

Support for named capture groups

Open piepjaquet9 opened this issue 6 years ago • 4 comments

A suggestion for improvement would be to support named capture groups like below

var groups1 = nlp("flight from Paris to San Francisco").match("flight from [from=#city] to [to=#city]").groups(); // {from: "Paris", to: "San Francisco"} var groups2 = nlp("flight to San Francisco from Paris").match("flight to [to=#city] from [from=#city]").groups(); // {from: "Paris", to: "San Francisco"}

piepjaquet9 avatar Nov 29 '18 03:11 piepjaquet9

hi phil, yeah I like that oo

spencermountain avatar Nov 29 '18 14:11 spencermountain

Bump...I was looking at something similar. I created/parse a syntax like this flight from [0: #city] to [1: #city], and convert it into flight from [#city] to #city + flight from #city to [#city] which I add to plugin and automatically create tags for each to find them later (so if our tag is Flights, I tag them both as Flights then Flights#0 and Flights#1 respectively.

It's a start if anyone is still interested, works for me - but feels a bit heavy handed. Named would be nice. I'm doing it this way so I know the order of the terms to then use later as arguments in a function (to generate outputs).

Drache93 avatar Dec 18 '19 19:12 Drache93

hey @Drache93 that's really neat. I'd love to see it, if it's available. You two are right, i am always doing this:

let m=doc.match('#City to #City')
let from = m.match('^#City')
let to = m.match('#City$')
// ...

the [] syntax is already taken for capture groups, which is admittedly an awkward choice...

hmmm.

if our string-syntax is already too-dense, maybe we should surface and add support for this in a json syntax, so you can do things explicitly, like:

doc.match([
  {tag:'City', name:'from'},
  {normal:'to'},
  {tag:'City', name:'to'}
])

... like that?

spencermountain avatar Dec 18 '19 21:12 spencermountain

Hmm that's pretty interesting. I'd be tempted to leave the normal as a pure string (...not sure if it feels like bad practise though), and require spaces to defined:

doc.match([
  {tag:'City', name:'from'},
  ' to ',
  {tag:'City', name:'to'}
])

I imagine they'd be some minor performance gains with this, removing the need to parse the match string?

@spencermountain I'm basically building contents for a plugin, I'll try get an example tomorrow, basically just a bunch of regexes to create some tags/post process matches.

P.S. Get a better time zone :D

Drache93 avatar Dec 18 '19 21:12 Drache93