Skip to content

Commit

Permalink
GF functions in yaml
Browse files Browse the repository at this point in the history
* test: add yaml parsing result test
* refactor: add support for gf functions in grammar build
* refactor: generator support for gf type
* refactor: specify roles in amr type gf syntax
  • Loading branch information
rokasramas authored and Žygimantas Medelis committed Jan 9, 2020
1 parent fb71269 commit dea5438
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 18 deletions.
2 changes: 1 addition & 1 deletion api/src/data/entities/amr.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
:syntax (for [instance syntax]
(reduce-kv (fn [m k v]
(assoc m k (cond-> v
(not (contains? #{:value :role} k))
(not (contains? #{:value :role :roles} k))
(keyword))))
{}
(into {} instance)))})
Expand Down
19 changes: 19 additions & 0 deletions api/test/resources/document_plans/location-amr.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{:type "Document-plan"
:segments [{:children [{:roles [{:children [{:type "Quote"
:srcId "uf:[gtVTc!uQe%WeEy${"
:text "Alimentum"}]
:name "name"}
{:children [{:type "Quote"
:srcId "#UU2hr35[NTazfg6d7JV"
:text "city centre"}]
:name "location"}]
:dictionaryItem {:name "at-location"
:type "Dictionary-item"
:srcId "5SgtK-{hj)?WV7+(Y=o."
:itemId "at-location"}
:type "AMR"
:srcId "Bu~!.wu`aau*X?Kca^2B"
:conceptId "at-location"}]
:type "Segment"
:srcId "5h957b7[5;yOW^=~2x[A"}]
:srcId "CJ*//Sz(JnX?Kt).0!j/"}
5 changes: 3 additions & 2 deletions core/src/acc_text/nlg/gf/generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@
(if (sequential? expr)
(cond->> (join-function-body expr)
(< 1 (count expr)) (format "(%s)"))
(let [{:keys [type value]} expr]
(let [{:keys [type value params]} expr]
(case type
:literal (format "\"%s\"" (escape-string value))
:function (format "%s.s" value)))))
:function (format "%s.s" value)
:gf (format "%s %s" value (str/join (interleave params (repeat " "))))))))

(defn get-operator [expr next-expr]
(when (some? next-expr)
Expand Down
9 changes: 6 additions & 3 deletions core/src/acc_text/nlg/gf/grammar/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(zipmap relations concepts)))

(defn attach-selectors [m attrs]
(let [selectors (->> (keys attrs) (remove #{:pos :role :value}) (select-keys attrs))]
(let [selectors (->> (keys attrs) (remove #{:pos :role :roles :value :type}) (select-keys attrs))]
(cond-> m (seq selectors) (assoc :selectors selectors))))

(defmulti build-function (fn [concept _ _ _] (:type concept)))
Expand Down Expand Up @@ -76,11 +76,14 @@
{:name (concept->name concept)
:params (map concept->name children)
:body (for [syntax (->> (keyword value) (get amr) (:frames) (map :syntax))]
(for [{value :value pos :pos role :role :as attrs} syntax]
(for [{:keys [value pos role roles type] :as attrs} syntax]
(let [role-key (when (some? role) (str/lower-case role))]
(-> (cond
(contains? role-map role-key) {:type :function
:value (get role-map role-key)}
(= :gf type) {:type :gf
:value value
:params (cons (concept->name function-concept) (map role-map roles))}
(some? role) {:type :literal
:value (format "{{%s}}" role)}
(= pos :AUX) {:type :function
Expand All @@ -93,7 +96,7 @@
:else {:type :literal
:value "{{...}}"})
(attach-selectors attrs)
(assoc :pos pos)))))
(cond-> (when (some? pos)) (assoc :pos pos))))))
:ret [:s "Str"]}))

(defmethod build-function :shuffle [concept children _ _]
Expand Down
35 changes: 35 additions & 0 deletions core/test/acc_text/nlg/gf/grammar_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@
(defn build-grammar [semantic-graph-name context]
(grammar/build :module :instance (utils/load-test-semantic-graph semantic-graph-name) context))

(deftest gf-grammar-building
(is (= #::grammar{:module :module
:instance :instance
:flags {:startcat "DocumentPlan01"}
:syntax [{:name "DocumentPlan01"
:params ["Segment02"]
:body [{:type :function :value "Segment02"}]
:ret [:s "Str"]}
{:name "Segment02"
:params ["Amr03"]
:body [{:type :function :value "Amr03"}]
:ret [:s "Str"]}
{:name "Amr03"
:params ["DictionaryItem04" "Quote05" "Quote06"]
:body [[{:type :gf :value "AtLocation"
:params ["DictionaryItem04" "Quote05" "Quote06"]}]]
:ret [:s "Str"]}
{:name "DictionaryItem04"
:params []
:body [[{:type :literal :value "place"}]
[{:type :literal :value "venue"}]]
:ret [:s "Str"]}
{:name "Quote05"
:params []
:body [{:type :literal :value "Alimentum"}]
:ret [:s "Str"]}
{:name "Quote06"
:params []
:body [{:type :literal :value "city centre"}]
:ret [:s "Str"]}]}
(build-grammar
"location-amr"
{:amr {:at-location {:frames [{:syntax [{:type :gf :value "AtLocation" :roles ["name" "location"]}]}]}}
:dictionary {"at-location" ["place" "venue"]}}))))

(deftest grammar-building
(is (= #::grammar{:module :module
:instance :instance
Expand Down
14 changes: 14 additions & 0 deletions core/test/resources/semantic_graphs/location-amr.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#:acc-text.nlg.semantic-graph{:relations [{:from :01 :to :02 :role :segment}
{:from :02 :to :03 :role :instance}
{:from :03 :to :04 :role :function}
{:from :03 :to :05 :role :ARG0 :attributes {:name "name"}}
{:from :03 :to :06 :role :ARG1 :attributes {:name "location"}}]
:concepts [{:id :01 :type :document-plan}
{:id :02 :type :segment}
{:id :03 :type :amr :value "at-location"}
{:id :04
:type :dictionary-item
:value "at-location"
:attributes {:name "at-location"}}
{:id :05 :type :quote :value "Alimentum"}
{:id :06 :type :quote :value "city centre"}]}
20 changes: 8 additions & 12 deletions grammar/concept-net/at-location.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
members:
- place
roles:
- address
- name
- location
frames:
- example: There is a place in the city center
syntax:
- pos: LEX
value: "There"
- pos: AUX
- pos: DET
- pos: NP
value: "place"
- pos: ADP
value: "in"
- pos: DET
- pos: NP
role: address
- type: gf
value: AtLocation
roles:
- name
- location

0 comments on commit dea5438

Please sign in to comment.