Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creation of the interface for calling Ken Language Models.
- Loading branch information
julosaure
committed
Sep 21, 2012
1 parent
2e869fb
commit 01058e4
Showing
3 changed files
with
110 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,62 @@ | |||
(ns decloder.kenlm | |||
(:require clojure.java.shell) | |||
(:require clojure.string) | |||
) | |||
|
|||
;; GLOBALS | |||
|
|||
(def LM_QUERY "/Users/julien/workspaces/xp/kenlm/query") | |||
|
|||
(def LM_BIN "/Users/julien/workspaces/clojure/decloder/data/lm/lm_giga_64k_vp_3gram/lm_giga_64k_vp_3gram.kenbinary") | |||
|
|||
;; UTILS | |||
|
|||
|
|||
;; FUNCTIONS | |||
|
|||
(defn call-lm [n-grams] | |||
(let [res (clojure.java.shell/sh LM_QUERY LM_BIN :in n-grams)] | |||
(if (> 0 (count (:err res))) | |||
(println "Error while querying LM with " n-grams ": " (:err res)) | |||
(:out res) | |||
) | |||
) | |||
) | |||
|
|||
(def TOTAL_PAT #"Total: ([-0-9.]+) ") | |||
|
|||
(defn parse-lm-output-line [line] | |||
(let [matcher (re-matcher TOTAL_PAT line) | |||
score (re-find matcher)] | |||
(println line) | |||
;(println (map #(str "X" % "X") score)) | |||
;(println score) | |||
(Double. (second score)) | |||
) | |||
) | |||
|
|||
|
|||
(defn parse-lm-output [out list-n-grams] | |||
(let [first-word (first list-n-grams) | |||
first-word-escaped (clojure.string/escape first-word {\( "\\(", \) "\\)", \* "\\*", \? "\\?"}) | |||
;tt (println first-word-escaped) | |||
pat (re-pattern first-word-escaped)] | |||
(loop [lines (clojure.string/split-lines out)] | |||
(if (re-find pat (first lines)) | |||
(parse-lm-output-line (first lines)) | |||
(recur (rest lines)) | |||
) | |||
) | |||
) | |||
) | |||
|
|||
|
|||
(defn score-ngrams [n-grams] | |||
{:pre [(string? n-grams) | |||
(> (count n-grams) 0)] | |||
:post [(pos? %)]} | |||
(let [list-n-grams (clojure.string/split n-grams #" ") | |||
out (call-lm n-grams)] | |||
(- (parse-lm-output out list-n-grams)) | |||
) | |||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters