-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelstats.hs
37 lines (32 loc) · 1.27 KB
/
modelstats.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import qualified Data.ByteString.Char8 as BS
--import Data.ByteString.Char8 (ByteString)
import Control.Monad
import Text.Printf
import Data.Binary
import Data.List
import qualified Data.Map as Map
import Data.Map (Map)
import Data.Ord
import Data.Tuple
import System.Environment (getArgs)
import Data.Array.IArray
import NLP.LDA
main = do
[filename] <- getArgs
lda <- decodeFile filename
let wm = wordmap lda
let rwm = invertMap wm
let topic_weights = [sum [theta lda ! (m,k) | m <- [1..num_documents lda]] | k <- [1..num_topics lda]]
-- let word_weights = phi lda
let top_topics = map (1+) $ topIndices (num_topics lda) topic_weights
printf "Top %d topic IDs: %s\n" (length top_topics) (show top_topics)
forM_ top_topics $ \topic_id -> do
let top_word_ids = map (1+) $ topIndices 20 [phi lda ! (topic_id,v) | v <- [1..num_words lda]]
let top_words = map (rwm Map.!) top_word_ids
printf "Top %d words for topic %d:\n" (length top_words) topic_id
forM_ top_words (printf "\t%s\n" . BS.unpack)
return ()
topIndices :: Ord a => Int -> [a] -> [Int]
topIndices n = map fst . take n . sortBy (flip (comparing snd)) . zip [0..]
invertMap :: Ord v => Map k v -> Map v k
invertMap = Map.fromList . map swap . Map.toList