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)
def token(self): return wordtoks2str(self.children)