Пример #1
0
    def _rec_start(self, speaker, fname):
        """Adds the optional recorded input/output element to the last
        "speaker" turn.

        """
        els = self._doc.getElementsByTagName("turn")

        for i in range(els.length - 1, -1, -1):
            if els[i].getAttribute("speaker") == speaker:
                da = els[i].appendChild(self._doc.createElement("rec"))
                da.setAttribute("fname", fname)
                da.setAttribute("starttime", self._get_time_str())
                break
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                ("Missing turn element for the {spkr} speaker".format(
                    spkr=speaker)))

        self._write_session_xml()

        self._rec_started[fname] = wave.open(
            os.path.join(self._session_dir_name, fname), 'w')
        self._rec_started[fname].setnchannels(1)
        self._rec_started[fname].setsampwidth(2)
        self._rec_started[fname].setframerate(self.cfg['Audio']['sample_rate'])
Пример #2
0
 def _rec_write(self, fname, data_rec):
     """Write into open file recording.
     """
     try:
         self._rec_started[fname].writeframes(bytearray(data_rec))
     except KeyError:
         raise SessionLoggerException("rec_write: missing rec element %s" %
                                      fname)
Пример #3
0
    def _rec_end(self, fname):
        """ Stores the end time in the rec element with fname file.
        """
        try:
            els = self._doc.getElementsByTagName("rec")

            for i in range(els.length - 1, -1, -1):
                if els[i].getAttribute("fname") == fname:
                    els[i].setAttribute("endtime", self._get_time_str())
                    break
            else:
                raise SessionLoggerException(
                    ("Missing rec element for the {fname} fname.".format(
                        fname=fname)))

            self._write_session_xml()
            self._rec_started[fname].close()
            self._rec_started[fname] = None
        except KeyError:
            raise SessionLoggerException("rec_end: missing rec element %s" %
                                         fname)
Пример #4
0
    def _last_turn_element(self, speaker):
        """ Finds the XML element in the given open XML session
        which corresponds to the last turn for the given speaker.

        Closes the XML and throws an exception if the element cannot be found.
        """
        els = self._doc.getElementsByTagName("turn")

        for i in range(els.length - 1, -1, -1):
            if els[i].getAttribute("speaker") == speaker:
                return els[i]
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                ("Missing turn element for %s speaker") % speaker)
Пример #5
0
    def _hangup(self, speaker):
        """ Adds the user hangup element to the last user turn.
        """
        els = self._doc.getElementsByTagName("turn")

        for i in range(els.length - 1, -1, -1):
            if els[i].getAttribute("speaker") == speaker:
                els[i].appendChild(self._doc.createElement("hangup"))
                break
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                ("Missing turn element for %s speaker") % speaker)

        self._write_session_xml()
Пример #6
0
    def _dialogue_rec_end(self, fname):
        """ Stores the end time in the dialogue_rec element with fname file.
        """
        els = self._doc.getElementsByTagName("dialogue_rec")

        for i in range(els.length - 1, -1, -1):
            if els[i].getAttribute("fname") == fname:
                els[i].setAttribute("endtime", self._get_time_str())
                break
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                "Missing dialogue_rec element for %s fname" % fname)

        self._write_session_xml()
Пример #7
0
    def _barge_in(self, speaker, tts_time=False, asr_time=False):
        """Add the optional barge-in element to the last speaker turn."""
        els = self._doc.getElementsByTagName("turn")

        for i in range(els.length - 1, -1, -1):
            if els[i].getAttribute("speaker") == speaker:
                da = els[i].appendChild(self._doc.createElement("barge-in"))
                da.setAttribute("time", self._get_time_str())
                if tts_time:
                    da.setAttribute("tts_time", self._get_time_str())
                if asr_time:
                    da.setAttribute("asr_time", self._get_time_str())
                break
        else:
            raise SessionLoggerException(
                ("Missing turn element for %s speaker") % speaker)

        self._write_session_xml()
Пример #8
0
    def _dialogue_act(self, speaker, dialogue_act):
        """ Adds the dialogue_act element to the last "speaker" turn.
        """
        els = self._doc.getElementsByTagName("turn")

        for i in range(els.length - 1, -1, -1):
            if els[i].getAttribute("speaker") == speaker:
                da = els[i].appendChild(
                    self._doc.createElement("dialogue_act"))
                da.setAttribute("time", self._get_time_str())
                da.appendChild(self._doc.createTextNode(unicode(dialogue_act)))
                break
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                ("Missing turn element for %s speaker") % speaker)

        self._write_session_xml()
Пример #9
0
    def _slu(self, speaker, fname, nblist, confnet=None):
        """ Adds the slu nbest list to the last speaker turn.

        alex Extension: It can also store the confusion network representation.
        The confnet must be an instance of DialogueActConfusionNetwork.

        """
        els = self._doc.getElementsByTagName("turn")

        for i in range(els.length - 1, -1, -1):
            if els[i].getAttribute("speaker") == speaker and self._include_rec(
                    els[i], fname):
                asr = els[i].appendChild(self._doc.createElement("slu"))

                for p, h in nblist:
                    hyp = asr.appendChild(
                        self._doc.createElement("interpretation"))
                    hyp.setAttribute("p", "%.3f" % p)
                    hyp.appendChild(self._doc.createTextNode(unicode(h)))

                if confnet:
                    cn = asr.appendChild(self._doc.createElement("confnet"))

                    for p, dai in confnet:
                        sas = cn.appendChild(
                            self._doc.createElement("dai_alternatives"))

                        daia = sas.appendChild(self._doc.createElement("dai"))
                        daia.setAttribute("p", "%.3f" % p)
                        daia.appendChild(self._doc.createTextNode(
                            unicode(dai)))

                        daia = sas.appendChild(self._doc.createElement("dai"))
                        daia.setAttribute("p", "%.3f" % (1 - p))
                        daia.appendChild(self._doc.createTextNode("null()"))

                break
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                ("Missing turn element for %s speaker") % speaker)

        self._write_session_xml()
Пример #10
0
    def _text(self, speaker, text, cost=None):
        """ Adds the text (prompt) element to the last "speaker" turn.
        """
        els = self._doc.getElementsByTagName("turn")

        for i in range(els.length - 1, -1, -1):
            if els[i].getAttribute("speaker") == speaker:
                da = els[i].appendChild(self._doc.createElement("text"))
                da.setAttribute("time", self._get_time_str())
                if cost:
                    da.setAttribute("cost", unicode(cost))
                da.appendChild(self._doc.createTextNode(unicode(text)))
                break
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                "Missing turn element for {spkr} speaker".format(spkr=speaker))

        self._write_session_xml()
Пример #11
0
    def _asr(self, speaker, fname, nblist, confnet=None):
        """ Adds the ASR nblist to the last speaker turn.

        alex Extension: It can also store the confusion network representation.
        """
        els = self._doc.getElementsByTagName("turn")

        for el_idx in range(els.length - 1, -1, -1):
            if els[el_idx].getAttribute(
                    "speaker") == speaker and self._include_rec(
                        els[el_idx], fname):
                asr = els[el_idx].appendChild(self._doc.createElement("asr"))

                for prob, hyp in nblist:
                    hyp_el = asr.appendChild(
                        self._doc.createElement("hypothesis"))
                    hyp_el.setAttribute("p", "{0:.3f}".format(prob))
                    hyp_el.appendChild(self._doc.createTextNode(unicode(hyp)))

                if confnet:
                    cn = asr.appendChild(self._doc.createElement("confnet"))

                    for alts in confnet:
                        was = cn.appendChild(
                            self._doc.createElement("word_alternatives"))

                        for prob, word in alts:
                            wa = was.appendChild(
                                self._doc.createElement("word"))
                            wa.setAttribute("p", "{0:.3f}".format(prob))
                            wa.appendChild(
                                self._doc.createTextNode(unicode(word)))

                break
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                ("Missing turn element for %s speaker") % speaker)

        self._write_session_xml()
Пример #12
0
    def _dialogue_rec_start(self, speaker, fname):
        """ Adds the optional recorded input/output element to the last
        "speaker" turn.

        FIXME: It can happen that the session.xml is not created when this
        function is called.

        """
        els = self._doc.getElementsByTagName("dialogue")

        if els:
            da = els[0].appendChild(self._doc.createElement("dialogue_rec"))
            if speaker:
                da.setAttribute("speaker", speaker)
            da.setAttribute("fname", fname)
            da.setAttribute("starttime", self._get_time_str())
        else:
            self._write_session_xml()
            raise SessionLoggerException(
                ("Missing dialogue element for %s speaker") % speaker)

        self._write_session_xml()
Пример #13
0
 def _evaluation(self, num_turns, task_success, user_sat, score):
     """Adds the evaluation optional tag to the header."""
     raise SessionLoggerException("Not implemented")