/* Test of the module dcg_capture in Picat. See dcg_capture.pi This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import dcg_capture. main => go. /* Check a string and capture substrings Output: checking = Jaco was an American bassist [name = Jaco,was_is = was,adj = American,noun = bassist] */ go => String = "Jaco was an American bassist", println(checking=String), jaco_bassist([Name,WasIs,Adj,Noun],String,[]), println([name=Name,was_is=WasIs,adj=Adj,noun=Noun]), nl, fail, nl. /* Generate all solutions and capture substrings This has 40 solutions. Output: string = Jaco was an American dancer [name = Jaco,was_is = was,adj = an American,noun = dancer] string = Jaco was an American bassist [name = Jaco,was_is = was,adj = an American,noun = bassist] string = Jaco was an American singer [name = Jaco,was_is = was,adj = an American,noun = singer] string = Jaco was an American programmer [name = Jaco,was_is = was,adj = an American,noun = programmer] ... */ go2 => jaco_bassist([Name,WasIs,Adj,Noun],String,[]), println(string=String), println([name=Name,was_is=WasIs,adj=Adj,noun=Noun]), nl, fail, nl. /* checking = [first1 = Håkan,last1 = Kjellerstrand] Generating: [string = Hakan Kjellstad,first = Hakan,last = Kjellstad] [string = Hakan Kjellstand,first = Hakan,last = Kjellstand] [string = Hakan Kjellstrad,first = Hakan,last = Kjellstrad] [string = Hakan Kjellstrand,first = Hakan,last = Kjellstrand] ... [string = håkan källarbad,first = håkan,last = källarbad] [string = håkan källarband,first = håkan,last = källarband] [string = håkan källarbrad,first = håkan,last = källarbrad] [string = håkan källarbrand,first = håkan,last = källarbrand] len = 384 */ go3 => if hakan_kjellerstrand([First1,Last1],"Håkan Kjellerstrand",[]) then println(checking=[first1=First1,last1=Last1]) else println(checking_not_ok) end, nl, println("Generating:"), All = findall([S,First,Last],hakan_kjellerstrand([First,Last],S,[])), foreach([S,F,L] in All) println([string=S,first=F,last=L]) end, println(len=All.len), nl. % % Tested in go/0 and go2/0 % jaco_bassist([Name,WasIs,Adj,Noun]) --> phrase_capture( (("J" ; "j"), "aco"), Name), space, phrase_capture( ("was" ; "is"), WasIs), space, phrase_capture( ("an American" ; "a famous"), Adj), space, phrase_capture( ("dancer" ; "bassist" ; "singer" ; "programmer" ; "dueller"), Noun). space --> " ". % Tested in go3/0. hakan_kjellerstrand([First,Last]) --> phrase_capture(( ("H" ; "h"),("a" ; "å"), "k","a","n"),First), space, phrase_capture(( ("K" ; "k"), ("je" ; "ä"), "ll", ("" ; "er" ; "ar"), ("st" ; "b"), ("" ; "r"), "a", (""; "n"), "d"), Last).