#!/usr/bin/env runhugs import IO import System import List import Char import Maybe import Directory type Word=String type Score=Int type LinkList=[Word] type Node=(Word,Score,LinkList) type Graph=[Node] type FT=[(Word,Score)] stopWords=words "a about an are as at be by com de en for from how I in is it la of on or that the this to was what when where who will with www" -- flattens list of lists flatten1 = foldr1 (++) -- member function, checks for word w in line l contains w l = any (==w) l -- functions for pulling out elements from triples and a quintuple (d'oh) third (a,b,c)=c middle (a,b,c)=b first (a,b,c)=a firstof (a,b,c,d,e) = a -- ensure all words are >2 chars long and not in list of stopWords cleanText = (filter ((>2).length)).(\\ stopWords) -- builds frequency table of uniq elements in AS versus count thereof freqtable :: [Word] -> FT freqtable as = sortBy (\l r -> compare (snd r) (snd l)) [ (a, (length.(filter (==a))) as) | a<-nub as ] -- returns list of phrases as they occur in given string phrases :: String -> [[Word]] phrases = (map (cleanText.words)) . lines -- return the score part from a frequency table ftlookup :: FT -> String -> Score ftlookup ft w = snd $ head $ filter ((==w).fst) ft -- returns the set of words in phrases in which the word is found modulo the word itself filterWords :: [[Word]] -> Word -> LinkList filterWords ps w = filter (not.(==w)) $ nub $ flatten1 $ filter (contains w) ps -- build the overall graph structure in an alist of triples genGraph :: [[Word]] -> FT -> Graph genGraph ps ft = let ws=nub $ sort $ flatten1 ps in [ (w, ftlookup ft w, filterWords ps w) | w <- ws ] -- return a list of pairs of interesting terms pairs :: FT -> Graph -> [(Score, [(Word,Score)])] pairs ft g = let stuff=[ ((ftlookup ft a)+(ftlookup ft b), sortBy (\l r -> compare (snd r) (snd l)) [(a, ftlookup ft a), (b, ftlookup ft b)]) | a <- map first g, b <- flatten1 $ map third $ filter ((==a).first) g ] in sortBy (\l r -> compare (fst l) (fst r)) $ nub $ filter ((>2).fst) stuff main = do c <- getContents let ft= freqtable $ words c ps=phrases c db= genGraph ps ft sortedgraph= sortBy (\l r -> compare (middle l) (middle r)) db p = pairs ft db in -- putStr ("Got freq-list: \n" ++ (unlines $ map show ft)) putStr ("Got graph: \n" ++ (unlines $ map show db) ++ "\n\nGot scored pairs: \n" ++ (unlines $ map show p)) putStr ("\n")