Skip to content

Instantly share code, notes, and snippets.

@jaor
Last active August 23, 2018 22:08
Show Gist options
  • Save jaor/896e2bc05e515b42406f6496488516ca to your computer and use it in GitHub Desktop.
Save jaor/896e2bc05e515b42406f6496488516ca to your computer and use it in GitHub Desktop.
Name topics
{
"name": "Name topics",
"kind": "script",
"description": "Give a name to all topics in a topicmodel",
"source_code": "script.whizzml",
"imports":[
],
"inputs":[
{
"name": "topic-model",
"type": "topicmodel",
"description": "The topic model to update"
}],
"outputs":[
{
"name": "result",
"type": "number",
"description": "Number of topics renamed in the input topic model"
}]
}
(define number-of-terms-in-topic-name 2)
(define (topic-name-candidate terms n)
(let (terms (cons (head terms) (drop n (tail terms)))
terms (take number-of-terms-in-topic-name terms))
(join "_" (map head terms))))
(define (topic-name topic topics)
(let (terms (topic "top_terms")
seen (map (lambda (t) (t "name")) (values topics)))
(iterate (r "unknown" n (range (count terms)))
(let (n (topic-name-candidate terms n))
(if (member? n seen) r (break n))))))
(define (clean-names topics)
(iterate (r {} k (keys topics) t (values topics))
(assoc r k (assoc t "name" (replace (t "name") " " "_")))))
(define (rename-topics tm-id)
(let (tm (fetch tm-id)
topics (reduce (lambda (ns topic)
(assoc ns (topic "id") {"name" (topic-name topic ns)}))
{}
(tm ["topic_model" "topics"])))
(update tm-id {"topics" (clean-names topics)})
(count topics)))
(define result (rename-topics topic-model))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment