Exemplo n.º 1
0
    def scansion(self, meter=None, conscious=False):
        bp = self.bestParse(meter)
        # if not bp: return
        lowestScore = ""
        str_ot = ""
        if bp:
            str_ot = bp.str_ot()
            lowestScore = bp.score()
        from tools import makeminlength

        self.om(
            "\t".join(
                [
                    str(x)
                    for x in [
                        makeminlength(str(self), being.linelen),
                        makeminlength(str(bp) if bp else "", being.linelen),
                        len(self.allParses(meter)),
                        lowestScore,
                        str_ot,
                    ]
                ]
            ),
            conscious=conscious,
        )
Exemplo n.º 2
0
    def store_stats(self, meter, init):
        textname = init.getName()
        if not textname:
            textname = str(self).replace(" ", "_")

        ## store stats
        if not textname in init.meter_stats["lines"]:
            init.meter_stats["lines"][textname] = {}
        if not textname in init.meter_stats["positions"]:
            init.meter_stats["positions"][textname] = {}
        if not textname in init.meter_stats["texts"]:
            init.meter_stats["texts"][textname] = {}
        if not textname in init.meter_stats["_ot"]:
            init.meter_stats["_ot"][textname] = makeminlength("line", being.linelen) + "\tmeter\t" + init.ckeys + "\n"

        try:

            parsedat = []
            for k, v in sorted(self.__bestparse[meter.id].constraintScores.items()):
                if not k in init.meter_stats["texts"][textname]:
                    init.meter_stats["texts"][textname][k] = []
                init.meter_stats["texts"][textname][k].append(v)

                # parsedat.append(v/len(self.__bestparse.positions))	#???
                parsedat.append(v)

            linekey = (
                str(len(init.meter_stats["lines"][textname]) + 1).zfill(6)
                + "_"
                + str(self.__bestparse[meter.id].posString())
            )
            init.meter_stats["lines"][textname][linekey] = parsedat

            ## OT stats
            parses = self.__parses[meter.id]
            init.meter_stats["_ot"][textname] += (
                makeminlength(str(self), being.linelen) + "\t" + parses[0].str_ot() + "\n"
            )
            if len(parses) > 1:
                for parse in parses[1:]:
                    init.meter_stats["_ot"][textname] += makeminlength("", being.linelen) + "\t" + parse.str_ot() + "\n"

            for posn in range(len(self.__bestparse[meter.id].positions)):
                pos = self.__bestparse[meter.id].positions[posn]
                (posdat, ckeys) = pos.formatConstraints(normalize=True, getKeys=True)

                for cnum in range(len(ckeys)):
                    if not posn in init.meter_stats["positions"][textname]:
                        init.meter_stats["positions"][textname][posn] = {}
                    if not ckeys[cnum] in init.meter_stats["positions"][textname][posn]:
                        init.meter_stats["positions"][textname][posn][ckeys[cnum]] = []
                    init.meter_stats["positions"][textname][posn][ckeys[cnum]].append(posdat[cnum])
        except (IndexError, KeyError, AttributeError) as e:
            # print "!! no lines successfully parsed with any meter"
            pass
Exemplo n.º 3
0
    def store_stats(self, meter, init):
        textname = init.getName()
        if not textname: textname = str(self).replace(" ", "_")

        ## store stats
        if (not textname in init.meter_stats['lines']):
            init.meter_stats['lines'][textname] = {}
        if (not textname in init.meter_stats['positions']):
            init.meter_stats['positions'][textname] = {}
        if (not textname in init.meter_stats['texts']):
            init.meter_stats['texts'][textname] = {}
        if (not textname in init.meter_stats['_ot']):
            init.meter_stats['_ot'][textname] = makeminlength(
                "line", being.linelen) + "\tmeter\t" + init.ckeys + "\n"

        parsedat = []
        for k, v in sorted(self.__bestparse[meter].constraintScores.items()):
            if (not k in init.meter_stats['texts'][textname]):
                init.meter_stats['texts'][textname][k] = []
            init.meter_stats['texts'][textname][k].append(v)

            #parsedat.append(v/len(self.__bestparse.positions))	#???
            parsedat.append(v)

        linekey = str(len(init.meter_stats['lines'][textname]) +
                      1).zfill(6) + "_" + str(
                          self.__bestparse[meter].posString())
        init.meter_stats['lines'][textname][linekey] = parsedat

        ## OT stats
        parses = self.__parses[meter]
        init.meter_stats['_ot'][textname] += makeminlength(
            str(self), being.linelen) + "\t" + parses[0].str_ot() + "\n"
        if len(parses) > 1:
            for parse in parses[1:]:
                init.meter_stats['_ot'][textname] += makeminlength(
                    "", being.linelen) + "\t" + parse.str_ot() + "\n"

        for posn in range(len(self.__bestparse[meter].positions)):
            pos = self.__bestparse[meter].positions[posn]
            (posdat, ckeys) = pos.formatConstraints(normalize=True,
                                                    getKeys=True)

            for cnum in range(len(ckeys)):
                if (not posn in init.meter_stats['positions'][textname]):
                    init.meter_stats['positions'][textname][posn] = {}
                if (not ckeys[cnum]
                        in init.meter_stats['positions'][textname][posn]):
                    init.meter_stats['positions'][textname][posn][
                        ckeys[cnum]] = []
                init.meter_stats['positions'][textname][posn][
                    ckeys[cnum]].append(posdat[cnum])
Exemplo n.º 4
0
 def scansion(self, meter=None, conscious=False):
     bp = self.bestParse(meter)
     lowestScore = bp.score()
     from tools import makeminlength
     self.om("\t".join([
         str(x) for x in [
             makeminlength(str(self), being.linelen),
             makeminlength(str(bp), being.linelen),
             len(self.allParses(meter)), lowestScore,
             bp.str_ot()
         ]
     ]),
             conscious=conscious)
Exemplo n.º 5
0
	def scansion(self,meter=None,conscious=False):
		bp=self.bestParse(meter)
		config=being.config
		#if not bp: return
		lowestScore=''
		str_ot=''
		count=''
		meterstr=''
		if bp:
			meterstr=bp.str_meter()
			str_ot=bp.str_ot()
			lowestScore=bp.score()
			count=bp.totalCount
		from tools import makeminlength
		self.om("\t".join( [str(x) for x in [makeminlength(str(self),config['linelen']), makeminlength(str(bp) if bp else '', config['linelen']),meterstr,len(self.allParses(meter)),count,lowestScore,str_ot] ] ),conscious=conscious)
Exemplo n.º 6
0
	def printScores(self, scores):
		output = ""
		for key, value in sorted(((str(k.name),v) for (k,v) in scores.items()),key=lambda lx: -lx[1]):
			if not value: break
			output += makeminlength("[*"+key+"]:"+str(value),24)
		output = output[:-1]
		return output
Exemplo n.º 7
0
 def printScores(self, scores):
     output = ""
     for key, value in sorted(
         ((str(k.name), v) for (k, v) in scores.items())):
         output += makeminlength("[*" + key + "]:" + str(value), 24)
     output = output[:-1]
     return output
Exemplo n.º 8
0
	def store_stats(self,meter,init):
		textname=init.getName()
		if not textname: textname=str(self).replace(" ","_")
		
		## store stats
		if (not textname in init.meter_stats['lines']):
			init.meter_stats['lines'][textname]={}
		if (not textname in init.meter_stats['positions']):
			init.meter_stats['positions'][textname]={}
		if (not textname in init.meter_stats['texts']):
			init.meter_stats['texts'][textname]={}
		if (not textname in init.meter_stats['_ot']):
			init.meter_stats['_ot'][textname]=makeminlength("line",being.linelen)+"\tmeter\t"+init.ckeys+"\n"
		
		parsedat=[]
		for k,v in sorted(self.__bestparse[meter].constraintScores.items()):
			if (not k in init.meter_stats['texts'][textname]):
				init.meter_stats['texts'][textname][k]=[]
			init.meter_stats['texts'][textname][k].append(v)
			
			#parsedat.append(v/len(self.__bestparse.positions))	#???
			parsedat.append(v)
			
		linekey=str(len(init.meter_stats['lines'][textname])+1).zfill(6)+"_"+str(self.__bestparse[meter].posString())
		init.meter_stats['lines'][textname][linekey]=parsedat
		
		## OT stats
		parses=self.__parses[meter]
		init.meter_stats['_ot'][textname]+=makeminlength(str(self),being.linelen)+"\t"+parses[0].str_ot()+"\n"
		if len(parses)>1:
			for parse in parses[1:]:
				init.meter_stats['_ot'][textname]+=makeminlength("",being.linelen)+"\t"+parse.str_ot()+"\n"
			
		
		
		for posn in range(len(self.__bestparse[meter].positions)):
			pos=self.__bestparse[meter].positions[posn]
			(posdat,ckeys)=pos.formatConstraints(normalize=True,getKeys=True)
			
			for cnum in range(len(ckeys)):
				if (not posn in init.meter_stats['positions'][textname]):
					init.meter_stats['positions'][textname][posn]={}
				if (not ckeys[cnum] in init.meter_stats['positions'][textname][posn]):
					init.meter_stats['positions'][textname][posn][ckeys[cnum]]=[]
				init.meter_stats['positions'][textname][posn][ckeys[cnum]].append(posdat[cnum])
Exemplo n.º 9
0
	def scansion(self,meter=None,conscious=False):
		bp=self.bestParse(meter)
		config=being.config
		#if not bp: return
		lowestScore=''
		str_ot=''
		count=''
		meterstr=''
		if bp:
			meterstr=bp.str_meter()
			str_ot=bp.str_ot()
			lowestScore=bp.score()
			count=bp.totalCount
		from tools import makeminlength
		#print makeminlength(unicode(bp),60)
		data = [makeminlength(unicode(self),config['linelen']), makeminlength(unicode(bp) if bp else '', config['linelen']),meterstr,len(self.allParses(meter)),count,lowestScore,str_ot]
		data_unicode = [unicode(x) for x in data]
		self.om("\t".join( data_unicode ),conscious=conscious)
Exemplo n.º 10
0
 def printScores(self, scores):
     output = ""
     for key, value in sorted(
         ((str(k.name), v) for (k, v) in scores.items()),
             key=lambda lx: -lx[1]):
         if not value: break
         output += makeminlength("[*" + key + "]:" + str(value), 24)
     output = output[:-1]
     return output
Exemplo n.º 11
0
	def scansion(self,meter=None,conscious=False):
		bp=self.bestParse(meter)
		lowestScore=bp.score()
		from tools import makeminlength
		self.om("\t".join( [ str(x) for x in [makeminlength(str(self),being.linelen), makeminlength(str(bp), being.linelen),len(self.allParses(meter)),lowestScore,bp.str_ot()] ] ),conscious=conscious)
Exemplo n.º 12
0
	def printScores(self, scores):
		output = ""
		for key, value in sorted(((str(k.name),v) for (k,v) in scores.items())):
			output += makeminlength("[*"+key+"]:"+str(value),24)
		output = output[:-1]
		return output
Exemplo n.º 13
0
	def grid(self,nspace=10):



		GRID_LINES=[]

		for LINE in self.lines():
			# word line
			line_words=[]
			for i,wtok in enumerate(LINE):
				if wtok.is_punct: continue
				line_words+=[makeminlength(wtok.token,nspace)]
			line_words = ' '.join(line_words)

			# grid lines
			import math,numpy as np
			grid_lines=[]
			max_grid = max([wtok.phrasal_stress for wtok in self.children if wtok.phrasal_stress!=None])
			min_grid = min([wtok.phrasal_stress for wtok in self.children if wtok.phrasal_stress!=None])
			#for line_num in range(int(math.ceil(min_grid))+1,int(math.ceil(max_grid))+1):
			for line_num in range(1,int(math.ceil(max_grid))+1):
				grid_line=[]
				for i,wtok in enumerate(LINE):
					if wtok.is_punct: continue
					mark = 'X' if wtok.phrasal_stress!=None and wtok.phrasal_stress<=line_num else ''
					grid_line+=[makeminlength(mark,nspace)]
				grid_lines+=[' '.join(grid_line)]

			# lines data
			data_lines=[]
			for datak in ['mean','mean_line','phrasal_stress_peak','phrasal_head']:
				data_line=[]
				for i,wtok in enumerate(LINE):
					if wtok.is_punct: continue

					v=wtok.feats.get(datak,'')

					if v==None:
						v=''
					elif type(v) in [float,np.float64]:
						if np.isnan(v):
							v=''
						else:
							v=round(v,1)
					elif v in [True,False]:
						v='+' if v else '-'

					if datak == 'phrasal_stress_peak':
						v2=wtok.feats.get('phrasal_stress_valley')
						if v2 and v:
							v=v+'/-'
						elif v2 and not v:
							v='-'

					mark = str(v)

					data_line+=[makeminlength(mark,nspace)]
				datak_name = datak
				if datak=='mean': datak_name='stress [sent]'
				if datak=='mean_line': datak_name='stress [line]'
				if datak=='norm_mean': datak_name='stress [sent norm]'
				if datak=='norm_mean_line': datak_name='stress [line norm]'
				if datak=='phrasal_stress_peak': datak_name='peak/valley'
				if datak=='phrasal_head': datak_name='head/foot'
				data_line+=[makeminlength('('+datak_name+')',nspace)]
				data_lines+=[' '.join(data_line)]

			#grid_lines.insert(0,line_words)

			grid_lines.append(line_words)
			grid_lines.append('')
			grid_lines.extend(data_lines)

			maxlength = max([len(l) for l in grid_lines])

			hdrftr='#' * maxlength
			hdr='STRESS GRID: '+wordtoks2str(LINE)
			#grid_lines.insert(0,self.token)
			#grid_lines.insert(0,hdr)
			#grid_lines.insert(0,hdrftr)
			#grid_lines.append(hdrftr)

			GRID_LINES+=['\n'.join(grid_lines)]

		return '\n\n\n'.join(GRID_LINES)
Exemplo n.º 14
0
    def grid(self,nspace=10):



        GRID_LINES=[]

        for LINE in self.lines():
            # word line
            line_words=[]
            for i,wtok in enumerate(LINE):
                if wtok.is_punct: continue
                line_words+=[makeminlength(wtok.token,nspace)]
            line_words = ' '.join(line_words)

            # grid lines
            import math,numpy as np
            grid_lines=[]
            max_grid = max([wtok.phrasal_stress for wtok in self.children if wtok.phrasal_stress!=None])
            min_grid = min([wtok.phrasal_stress for wtok in self.children if wtok.phrasal_stress!=None])
            #for line_num in range(int(math.ceil(min_grid))+1,int(math.ceil(max_grid))+1):
            for line_num in range(1,int(math.ceil(max_grid))+1):
                grid_line=[]
                for i,wtok in enumerate(LINE):
                    if wtok.is_punct: continue
                    mark = 'X' if wtok.phrasal_stress!=None and wtok.phrasal_stress<=line_num else ''
                    grid_line+=[makeminlength(mark,nspace)]
                grid_lines+=[' '.join(grid_line)]

            # lines data
            data_lines=[]
            for datak in ['mean','mean_line','phrasal_stress_peak','phrasal_head']:
                data_line=[]
                for i,wtok in enumerate(LINE):
                    if wtok.is_punct: continue

                    v=wtok.feats.get(datak,'')

                    if v==None:
                        v=''
                    elif type(v) in [float,np.float64]:
                        if np.isnan(v):
                            v=''
                        else:
                            v=round(v,1)
                    elif v in [True,False]:
                        v='+' if v else '-'

                    if datak == 'phrasal_stress_peak':
                        v2=wtok.feats.get('phrasal_stress_valley')
                        if v2 and v:
                            v=v+'/-'
                        elif v2 and not v:
                            v='-'

                    mark = str(v)

                    data_line+=[makeminlength(mark,nspace)]
                datak_name = datak
                if datak=='mean': datak_name='stress [sent]'
                if datak=='mean_line': datak_name='stress [line]'
                if datak=='norm_mean': datak_name='stress [sent norm]'
                if datak=='norm_mean_line': datak_name='stress [line norm]'
                if datak=='phrasal_stress_peak': datak_name='peak/valley'
                if datak=='phrasal_head': datak_name='head/foot'
                data_line+=[makeminlength('('+datak_name+')',nspace)]
                data_lines+=[' '.join(data_line)]

            #grid_lines.insert(0,line_words)

            grid_lines.append(line_words)
            grid_lines.append('')
            grid_lines.extend(data_lines)

            maxlength = max([len(l) for l in grid_lines])

            hdrftr='#' * maxlength
            hdr='STRESS GRID: '+wordtoks2str(LINE)
            #grid_lines.insert(0,self.token)
            #grid_lines.insert(0,hdr)
            #grid_lines.insert(0,hdrftr)
            #grid_lines.append(hdrftr)

            GRID_LINES+=['\n'.join(grid_lines)]

        return '\n\n\n'.join(GRID_LINES)