<s> from __future__ import print_function <EOL> import string <EOL> import operator <EOL> import os <EOL> import math <EOL> import collections <EOL> clear = lambda : os . system ( '<STR_LIT>' ) <EOL> from voice import Voice <EOL> from word import Word <EOL> __author__ = '<STR_LIT>' <EOL> '''<STR_LIT>''' <EOL> default_VBSets = { '<STR_LIT>' : <NUM_LIT:20> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : True , '<STR_LIT>' : True , '<STR_LIT>' : False } <EOL> class Voicebox ( object ) : <EOL> def __init__ ( self , settings = default_VBSets ) : <EOL> self . voices = { } <EOL> self . voiceWeights = { } <EOL> self . num_opts = settings [ '<STR_LIT>' ] <EOL> self . direction = settings [ '<STR_LIT>' ] <EOL> self . vision = settings [ '<STR_LIT>' ] <EOL> self . showvalue = settings [ '<STR_LIT>' ] <EOL> self . showsource = settings [ '<STR_LIT>' ] <EOL> self . proportional_src = settings [ '<STR_LIT>' ] <EOL> def addVoiceFromFile ( self , filename , voicename = '<STR_LIT>' ) : <EOL> if voicename == '<STR_LIT>' : <EOL> voicename = filename <EOL> voicenumber = str ( len ( self . voices ) + <NUM_LIT:1> ) + "<STR_LIT>" <EOL> voicename = voicenumber + voicename <EOL> D = self . makeDict ( '<STR_LIT>' , filename ) <EOL> if filename [ <NUM_LIT:0> : <NUM_LIT:12> ] == '<STR_LIT>' : <EOL> filename = filename [ <NUM_LIT:12> : ] <EOL> self . voices [ voicename ] = Voice ( filename , D ) <EOL> self . voiceWeights [ voicename ] = <NUM_LIT:1> <EOL> return self . voices [ voicename ] <EOL> def addVoiceFromDict ( self , D , voicename ) : <EOL> voicenumber = str ( len ( self . voices ) + <NUM_LIT:1> ) + "<STR_LIT>" <EOL> voicename = voicenumber + voicename <EOL> self . voices [ voicename ] = Voice ( voicename , D ) <EOL> self . voiceWeights [ voicename ] = <NUM_LIT:1> <EOL> return self . voices [ voicename ] <EOL> def setVoiceWeight ( self , voicename , weight ) : <EOL> self . voiceWeights [ voicename ] = weight <EOL> def addTranscript ( self , tname , speakerlist ) : <EOL> path = '<STR_LIT>' % tname <EOL> f = open ( path , "<STR_LIT:r>" ) <EOL> lines = f . read ( ) . lower ( ) . split ( '<STR_LIT::>' ) <EOL> labeled_lines = [ ] <EOL> for i in range ( <NUM_LIT:1> , len ( lines ) ) : <EOL> labeled_lines . append ( ( lines [ i - <NUM_LIT:1> ] . split ( ) [ - <NUM_LIT:1> ] , lines [ i ] . split ( ) [ <NUM_LIT:0> : - <NUM_LIT:1> ] ) ) <EOL> speakers = { } <EOL> for line in labeled_lines : <EOL> name = line [ <NUM_LIT:0> ] <EOL> if name in speakerlist : <EOL> if name in speakers : <EOL> speakers [ name ] += line [ <NUM_LIT:1> ] <EOL> else : <EOL> speakers [ name ] = line [ <NUM_LIT:1> ] <EOL> else : <EOL> if '<STR_LIT>' in speakerlist : <EOL> if '<STR_LIT>' in speakers : <EOL> speakers [ '<STR_LIT>' ] += line [ <NUM_LIT:1> ] <EOL> else : <EOL> speakers [ '<STR_LIT>' ] = line [ <NUM_LIT:1> ] <EOL> for name in speakers : <EOL> filename = '<STR_LIT>' % name <EOL> path = '<STR_LIT>' % filename <EOL> outfile = open ( path , '<STR_LIT:w>' ) <EOL> toWrite = "<STR_LIT:U+0020>" . join ( speakers [ name ] ) <EOL> outfile . write ( toWrite ) <EOL> self . addVoiceFromFile ( filename , filename [ <NUM_LIT:12> : ] ) <EOL> def makeDict ( self , str , filename = '<STR_LIT>' ) : <EOL> if filename == '<STR_LIT>' : <EOL> sentences = str . split ( '<STR_LIT:.>' ) <EOL> else : <EOL> print ( "<STR_LIT>" , filename + "<STR_LIT>" ) <EOL> path = '<STR_LIT>' + filename <EOL> f = open ( path , "<STR_LIT:r>" ) <EOL> sentences = f . read ( ) . split ( '<STR_LIT:.>' ) <EOL> for s in range ( <NUM_LIT:0> , len ( sentences ) ) : <EOL> sentences [ s ] = sentences [ s ] . strip ( '<STR_LIT:\n>' ) <EOL> try : <EOL> sentences [ s ] = sentences [ s ] . translate ( string . maketrans ( "<STR_LIT>" , "<STR_LIT>" ) , string . punctuation ) <EOL> except AttributeError : <EOL> sentences [ s ] = sentences [ s ] . translate ( { i : "<STR_LIT>" for i in string . punctuation } ) <EOL> sentences [ s ] = sentences [ s ] . lower ( ) <EOL> sentences [ s ] = sentences [ s ] . split ( ) <EOL> D = { } <EOL> for s in sentences : <EOL> for w in range ( <NUM_LIT:0> , len ( s ) ) : <EOL> self . addWord ( D , s , w , self . vision ) <EOL> D = self . normalizeFreqs ( D ) <EOL> D = self . sigScores ( D , D ) <EOL> return D <EOL> def normalizeFreqs ( self , D ) : <EOL> if D == { } : <EOL> return D <EOL> s = <NUM_LIT:0> <EOL> for w in D : <EOL> s = s + D [ w ] . freq <EOL> for w in D : <EOL> D [ w ] . set_sub1 ( self . normalizeFreqs ( D [ w ] . sub1 ) ) <EOL> D [ w ] . set_sub2 ( self . normalizeFreqs ( D [ w ] . sub2 ) ) <EOL> D [ w ] . set_norm ( D [ w ] . freq / float ( s ) ) <EOL> return D <EOL> def sigScores ( self , D , superD ) : <EOL> if D == { } : <EOL> return D <EOL> for w in D : <EOL> D [ w ] . set_sub1 ( self . sigScores ( D [ w ] . sub1 , superD ) ) <EOL> D [ w ] . set_sub2 ( self . sigScores ( D [ w ] . sub2 , superD ) ) <EOL> D [ w ] . set_sig ( ( D [ w ] . norm / superD [ w ] . norm ) * math . log ( D [ w ] . freq + <NUM_LIT:1> , <NUM_LIT:10> ) ) <EOL> return D <EOL> def addWord ( self , D , s , pos , v ) : <EOL> word = s [ pos ] <EOL> if v <= <NUM_LIT:0> : <EOL> if word in D : <EOL> D [ word ] . set_freq ( D [ word ] . freq + <NUM_LIT:1> ) <EOL> else : <EOL> D [ word ] = Word ( word ) <EOL> return D <EOL> elif v > <NUM_LIT:0> : <EOL> if word in D : <EOL> newsub1 = D [ word ] . sub1 <EOL> if pos < len ( s ) - <NUM_LIT:1> : <EOL> newsub1 = self . addWord ( D [ word ] . sub1 , s , pos + <NUM_LIT:1> , v - <NUM_LIT:1> ) <EOL> newsub2 = D [ word ] . sub2 <EOL> if v > <NUM_LIT:1> and pos < len ( s ) - <NUM_LIT:2> : <EOL> newsub2 = self . addWord ( D [ word ] . sub2 , s , pos + <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> D [ word ] . set_freq ( D [ word ] . freq + <NUM_LIT:1> ) <EOL> D [ word ] . set_sub1 ( newsub1 ) <EOL> D [ word ] . set_sub2 ( newsub2 ) <EOL> return D <EOL> else : <EOL> D [ word ] = Word ( word ) <EOL> newsub1 = { } <EOL> if pos < len ( s ) - <NUM_LIT:1> : <EOL> newsub1 = self . addWord ( D [ word ] . sub1 , s , pos + <NUM_LIT:1> , v - <NUM_LIT:1> ) <EOL> newsub2 = { } <EOL> if v > <NUM_LIT:1> and pos < len ( s ) - <NUM_LIT:2> : <EOL> newsub2 = self . addWord ( D [ word ] . sub2 , s , pos + <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> D [ word ] . set_sub1 ( newsub1 ) <EOL> D [ word ] . set_sub2 ( newsub2 ) <EOL> return D <EOL> def useOneVoice ( self , chosenVoiceName ) : <EOL> for voicename in self . voiceWeights : <EOL> self . voiceWeights [ voicename ] = <NUM_LIT:0> <EOL> self . voiceWeights [ chosenVoiceName ] = <NUM_LIT:1> <EOL> def getVoices ( self ) : <EOL> print ( "<STR_LIT>" ) <EOL> toReturn = { } <EOL> tab = <NUM_LIT:15> <EOL> for voicename in sorted ( self . voices ) : <EOL> print ( voicename , '<STR_LIT:U+0020>' * ( tab - len ( voicename ) ) + "<STR_LIT>" + str ( self . voiceWeights [ voicename ] ) + "<STR_LIT:)>" ) <EOL> def getOptions ( self , recent_words ) : <EOL> aggregate = { } <EOL> for v in self . voices : <EOL> voice = self . voices [ v ] <EOL> if self . voiceWeights != <NUM_LIT:0> : <EOL> contribution = voice . weighContexts ( voice . activeD , recent_words ) <EOL> for w in contribution : <EOL> if w in aggregate : <EOL> aggregate [ w ] [ <NUM_LIT:0> ] += contribution [ w ] * self . voiceWeights [ v ] <EOL> aggregate [ w ] [ <NUM_LIT:1> ] [ v ] = contribution [ w ] * self . voiceWeights [ v ] <EOL> else : <EOL> aggregate [ w ] = [ <NUM_LIT:0> , { } ] <EOL> aggregate [ w ] [ <NUM_LIT:0> ] += contribution [ w ] * self . voiceWeights [ v ] <EOL> aggregate [ w ] [ <NUM_LIT:1> ] [ v ] = contribution [ w ] * self . voiceWeights [ v ] <EOL> if self . proportional_src : <EOL> for v in aggregate [ w ] [ <NUM_LIT:1> ] : <EOL> aggregate [ w ] [ <NUM_LIT:1> ] [ v ] = aggregate [ w ] [ <NUM_LIT:1> ] [ v ] / sum ( aggregate [ w ] [ <NUM_LIT:1> ] . values ( ) ) <EOL> toReturn = [ ] <EOL> for w in aggregate : <EOL> toReturn . append ( [ w , aggregate [ w ] [ <NUM_LIT:0> ] , aggregate [ w ] [ <NUM_LIT:1> ] ] ) <EOL> ranked = sorted ( toReturn , key = operator . itemgetter ( <NUM_LIT:1> ) ) <EOL> optionList = list ( reversed ( ranked ) ) [ <NUM_LIT:0> : self . num_opts ] <EOL> self . options = optionList <EOL> return optionList <EOL> def printOptions ( self ) : <EOL> print ( "<STR_LIT>" ) <EOL> for wordValSrc in self . options : <EOL> tabs = <NUM_LIT:1> <EOL> tabsize = <NUM_LIT> <EOL> toPrint = wordValSrc [ <NUM_LIT:0> ] <EOL> offset = "<STR_LIT:U+0020>" * ( tabs * tabsize - len ( toPrint ) ) <EOL> tabs += <NUM_LIT:1> <EOL> if self . showvalue : <EOL> val = float ( "<STR_LIT>" . format ( wordValSrc [ <NUM_LIT:1> ] ) ) <EOL> toPrint += ( offset + "<STR_LIT:(>" + str ( val ) + "<STR_LIT:)>" ) <EOL> offset = "<STR_LIT:U+0020>" * ( tabs * tabsize - len ( toPrint ) ) <EOL> tabs += <NUM_LIT:1> <EOL> if self . showsource : <EOL> for v in self . voices : <EOL> if v in wordValSrc [ <NUM_LIT:2> ] : <EOL> val = float ( "<STR_LIT>" . format ( wordValSrc [ <NUM_LIT:2> ] [ v ] ) ) <EOL> else : <EOL> val = <NUM_LIT> <EOL> toPrint += ( offset + "<STR_LIT:(>" + v + "<STR_LIT>" + str ( val ) + "<STR_LIT:)>" ) <EOL> offset = "<STR_LIT:U+0020>" * ( tabs * tabsize + <NUM_LIT:5> - len ( toPrint ) ) <EOL> tabs += <NUM_LIT:1> <EOL> print ( toPrint ) </s>
