eKorpus

Source code for ekorpus.controllers.textgrid

"""The :mod:`textgrid` contains various Praat textgrid related operations.
"""
import re, os, sys
import codecs
import locale
import types
import pylons
import shutil
import thread
from ekorpus.lib.base import *
from sqlalchemy import select,join,and_
from ekorpus.models.schema import segment_table, segmentation_table, segmentscore_table
from ekorpus.textgrid import TextGrid

root = 'ekorpus/public/'
upload_folder = 'files/'
rec_folder = 'recordings/'
textgrid_folder = 'textgrid/'
textgrid_ext = '.TextGrid'
#praat_path = pylons.config['praat_path']

textgrid_creation_lock = thread.allocate_lock()

[docs]def create_textgrid(first, last): print first, last name = upload_folder+textgrid_folder+str(first) if last: name = name + '-' + str(last) name = name+textgrid_ext file_path = root+name print file_path us = False if textgrid_creation_lock.locked(): print "### ACK:",`textgrid_creation_lock` us = textgrid_creation_lock.acquire() #us = True try: if not os.path.exists(file_path): if not us: print "### ACK:",`textgrid_creation_lock` us = textgrid_creation_lock.acquire() #us = True if not os.path.exists(file_path): missing = "none.html" print first, file_path bind=engine=model.ctx.current.get_bind(None); parent = segment_table.select(segment_table.c.id==first, engine=bind).execute().fetchone() if not parent: name = missing else: segmentations = segmentation_table.select(engine=bind).execute().fetchall() rec = [ s.recording_id for s in segmentations if s.id==parent.segmentation_id ] segmentations = [ s for s in segmentations if s.recording_id==rec[0] ] order = [ "phoneme", "word", "pos","sentence"] emotions = { "e10":"joy","e7":"anger","e3":"sadness","e0":"neutral"} tgd = TextGrid(start=parent.start, end=parent.end) for i in order: segmentation = [ s for s in segmentations if s.type==i ] if segmentation: segmentation = segmentation[0] inv = select([segment_table.c.label,segment_table.c.start,segment_table.c.end], and_( segment_table.c.segmentation_id==segmentation.id, segment_table.c.end>parent.start, segment_table.c.start<parent.end), order_by=[segment_table.c.start], engine=bind).execute().fetchall() if inv: tgd.appendTier(segmentation.type, inv) emo = segmentscore_table.select(segmentscore_table.c.segment_id==first, engine=bind).execute().fetchone() if emo: print tgd.xmin, tgd.xmax tgd.appendTier("emotion", [[emotions[emo.code], parent.start, parent.end]]) file = codecs.open(file_path, 'wt', 'utf-8') tgd.write(file) finally: if us: print "### REL:",`textgrid_creation_lock` us = False textgrid_creation_lock.release() return '/'+name
[docs]class TextgridController(BaseController):
[docs] def index(self): return 'nope'
[docs] def segment(self, first, last): if not first: return id = first if id.endswith(textgrid_ext): id = id[:-4] if first.startswith('i_'): id = id[2:] item = model.Item.get_by(id=id) if not item: return id = item.first_id; elif first.startswith('s_'): id = id[2:] return h.redirect_to(create_textgrid(id,None)) #----------------------------------------------------------------------------#
if __name__ == '__main__': tgd = TextGrid(open('16nov.txt', 'r')) tgd.write(sys.stdout) #print "%.4f %.4f %s" % (inv[1], inv[2], inv[0])