Exemplo n.º 1
0
    def SampleUtterance(self, target):
        """
        Return a baseline utterance relying on the bias.

        Args:
        target: PhysicalObject type of object.
        """
        # build a basic utterance
        SampledUtterance = Utterance.Utterance(target.name)
        # Now iterate over each feature, and sample a probability of using it.
        for currentfeature in target.features:
            basebias = self.Bias.GetBias(currentfeature)
            if random.random() < basebias:
                SampledUtterance.InsertFeature(currentfeature)
        return SampledUtterance
Exemplo n.º 2
0
    def make_utterances(self, outdir, clear_old_data=False):
        """
        Initialise utt structures for utterances with both text and speech.
        Save to directory, return list of (absolute) utterance filenames.
        """
        filelist = []
        
        utt_list_speech = self.get_aligned()

        utts_speech = [Utterance(self.utterances[utt]["text"], \
            speech_file=self.utterances[utt]["speech"], utterance_location=outdir) \
                                                                for utt in utt_list_speech]
                                                                
        utt_list = utt_list_speech 
        utts = utts_speech

        for (name, utt_struct) in sorted(zip(utt_list, utts)):
            fname = os.path.join(outdir, name + ".utt")
            filelist.append(fname)
            if clear_old_data or not os.path.isfile(fname):
                utt_struct.save() 
        return filelist
                 ["small", "triangle"], ["large", "rectangle"], ["", "star"]]
Utterance_Rel = [["", "square"], ["", "circle"], ["small", "triangle"],
                 ["", "rectangle"], ["", "star"]]

Utterance_List = Utterance_Unr

Verbose = False

BetaParameters = [0.32, 1.12]
SpeakerRationalityNoise = 0.057

MyFilter = Filter.Filter(3, 3)  # Common ground must have three objects.
BiasPriors = [SF.BuildBeta(BetaParameters[0], BetaParameters[1], "size")]
RawUtterance = Utterance_List[0]
if RawUtterance[0] != "":
    FirstUtterance = UT.Utterance(RawUtterance[1],
                                  [OF.ObjectFeature(RawUtterance[0], "size")])
else:
    FirstUtterance = UT.Utterance(RawUtterance[1])
TL = PO.PhysicalObject(TL_Keys[0][1],
                       [OF.ObjectFeature(TL_Keys[0][0], "size")], "TL")
TR = PO.PhysicalObject(TR_Keys[0][1],
                       [OF.ObjectFeature(TR_Keys[0][0], "size")], "TR")
BL = PO.PhysicalObject(BL_Keys[0][1],
                       [OF.ObjectFeature(BL_Keys[0][0], "size")], "BL")
BR = PO.PhysicalObject(BR_Keys[0][1],
                       [OF.ObjectFeature(BR_Keys[0][0], "size")], "BR")
CGPrior = BF.Belief(4, [TL.Id, TR.Id, BL.Id, BR.Id],
                    [0.5, 0.5, 0.5, 0.5])  # Priors from common ground
World = VW.VisualWorld([TL, TR, BL, BR])
MyListener = CL.ComplexListener(World, CGPrior, BiasPriors, MyFilter,
                                SpeakerRationalityNoise)
Exemplo n.º 4
0
    def train(self, corpus):

        if self.trained:
            sys.exit(
                'A trained voice exists for this language/speaker/recipe combination.'
            )

        ## For a while, all_corpus included both text and speech-and-text utterances,
        ## but this has been reverted to original set-up, text_corpus has been restored
        ## to train calls at positional (not kw as before) arg:
        #all_corpus = corpus.make_utterances(self.res.make_dir(c.TRAIN, "utt"), \
        #                                                clear_old_data=self.clear_old_data)
        speech_corpus = corpus.make_utterances(self.res.make_dir(c.TRAIN, "utt"), \
                                                    clear_old_data=self.clear_old_data)
        text_corpus = corpus.all_text_files()

        ## tmporary fix for error:
        '''
        Traceback (most recent call last):
          File "./scripts/train.py", line 115, in <module>
            main_work()
          File "./scripts/train.py", line 110, in main_work
            voice.train(corpus)
          File "/afs/inf.ed.ac.uk/group/cstr/projects/simple4all_2/alessandra_dissertation/tool/Ossian/scripts/main/Voice.py", line 291, in train
            result = pool.apply_async(processor, args=(utterance_file, self.res.make_dir(c.TRAIN, "utt"), self.run_mode))
          File "<string>", line 2, in apply_async
          File "/afs/inf.ed.ac.uk/user/o/owatts/tool/python/ActivePython-2.7/lib/python2.7/multiprocessing/managers.py", line 763, in _callmethod
            conn.send((self._id, methodname, args, kwds))
        cPickle.PicklingError: Can't pickle <type '_sre.SRE_Match'>: attribute lookup _sre.SRE_Match failed
        '''
        unparallelisable_classes = ['BasicStanfordCoreNLP', 'Lexicon'
                                    ]  ## lexicon parallelises very slowly --
        ## see: http://stackoverflow.com/questions/20727375/multiprocessing-pool-slower-than-just-using-ordinary-functions
        ### ^---- TODO: this is now unsed

        i = 1
        for processor in self.processors:
            #print processor
            #print dir(processor)
            #print type(processor)
            print "\n\n== Train voice (proc no. %s (%s))  ==" % (
                i, processor.processor_name)

            if not processor.trained:
                ## has a suitable component already been trained?
                if os.path.isdir(processor.component_path):
                    print "Copy existing component for processor " + processor.processor_name
                    processor.reuse_component(self.res)
                else:
                    print "Train processor " + processor.processor_name
                    processor.train(speech_corpus, text_corpus)

            print "          Applying processor " + processor.processor_name
            if self.max_cores > 1:
                pool = multiprocessing.Manager().Pool(self.max_cores)
            for utterance_file in speech_corpus:
                if self.max_cores > 1 and processor.parallelisable:
                    result = pool.apply_async(processor,
                                              args=(utterance_file,
                                                    self.res.make_dir(
                                                        c.TRAIN,
                                                        "utt"), self.run_mode))
                else:
                    utterance = Utterance(utterance_file,
                                          utterance_location=self.res.make_dir(
                                              c.TRAIN, "utt"))
                    processor.apply_to_utt(utterance, voice_mode=self.run_mode)
                    utterance.save()

    #               utterance.pretty_print()
            if self.max_cores > 1:
                pool.close()
                pool.join()
            i += 1
        self.save()
Exemplo n.º 5
0
    def synth_utterance(self,
                        input_string,
                        output_wavefile=None,
                        output_labfile=None,
                        basename=None,
                        input_wavefile=None,
                        output_uttfile=None,
                        output_extensions=[]):

        output_location = self.res.make_dir(c.VOICE, "output")
        test_utterance_location = self.res.make_dir(c.VOICE, "output/utt")
        test_utterance_name = "temp"

        utt = Utterance(input_string,
                        utterance_location=test_utterance_location)
        utt.set("utterance_name", test_utterance_name)
        if basename:
            utt.set("utterance_name", basename)
        ## For synthesis with natural durations:
        if input_wavefile:
            assert os.path.isfile(input_wavefile)
            utt.set("waveform", input_wavefile)

        ## clear up previous test synthesis:
#        utterance_path = os.path.join(test_utterance_location, test_utterance_name)
#        if os.path.isfile(utterance_path):
#            os.remove(utterance_path)
## Do it like to remove ALL old files (including cmp etc in case of using natural durations...)
        old_files = glob.glob(output_location + '/*/' + test_utterance_name +
                              '.*')
        for fname in old_files:
            os.remove(fname)
        ## ---------------------------------
        i = 1
        if self.make_archive:
            utt.archive()
        for processor in self.processors:
            print "\n==  proc no. %s (%s)  ==" % (i, processor.processor_name)
            #print " == Before:"
            #utt.pretty_print()
            processor.apply_to_utt(
                utt, voice_mode=self.run_mode)  ## utt is changed in place

            #print " == After:"
            if self.make_archive:
                utt.archive()
            #utt.pretty_print()
            i += 1
        #utt.pretty_print()
        ## Save wave to named file?
        if output_wavefile:
            if not utt.has_external_data("wav"):
                print "Warning: no wave produced for this utt"
            else:
                temp_wave = utt.get_filename("wav")

                ## Check files are different; realpath so that e.g. / and // are equivalent:
                if os.path.realpath(temp_wave) != os.path.realpath(
                        output_wavefile):
                    shutil.copyfile(temp_wave, output_wavefile)

        if output_labfile:
            if not utt.has_external_data("lab"):
                print "Warning: no lab produced for this utt"
            else:
                temp_lab = utt.get_filename("lab")
                shutil.copyfile(temp_lab, output_labfile)

        if output_extensions != []:
            for ext in output_extensions:
                if not utt.has_external_data(ext):
                    print "Warning: no %s produced for this utt" % (ext)
                else:
                    assert output_wavefile
                    output_file = re.sub('wav\Z', ext, output_wavefile)
                    temp_file = utt.get_filename(ext)
                    shutil.copyfile(temp_file, output_file)

        utt.save()

        if output_uttfile:
            utterance_path = os.path.join(test_utterance_location,
                                          test_utterance_name + '.utt')
            shutil.copyfile(utterance_path, output_uttfile)
Exemplo n.º 6
0
import itertools
import Table
import Utterance
import PossibleWorld
import Rsa
import Speaker
import World
import InitialListener
import Listener
import matplotlib.pyplot as plt
import numpy as np

U = [
    Utterance.Utterance('some', ['all', 'notall']),
    Utterance.Utterance('all', ['all'])
]
W = [World.World('all', 0.5), World.World('notall', 0.5)]
rationality = 3.4
cost = 0


def run_experiment(u, w, depth, cost, rationality):
    #creates an Rsa instance to run experiment
    rsa = Rsa.Rsa(u, w, cost, rationality)
    #compute the recursion
    rsa.recurse(depth, 0)
    #print out your final results
    for element in rsa.layers[depth].listener.table.possible_worlds:
        print "The utterance is " + str(element.utterance.u)
        print "The world is " + str(element.world.world)
        print "The column number is " + str(element.column)
Exemplo n.º 7
0
    def synth_utterance(self,
                        input_string,
                        output_wavefile=None,
                        output_labfile=None,
                        basename=None,
                        input_wavefile=None,
                        output_uttfile=None,
                        output_extensions=[]):

        output_location = self.res.make_dir(c.VOICE, "output")
        test_utterance_location = self.res.make_dir(c.VOICE, "output/utt")
        test_utterance_name = "temp"

        utt = Utterance(input_string,
                        utterance_location=test_utterance_location)
        utt.set("utterance_name", test_utterance_name)
        if basename:
            utt.set("utterance_name", basename)
        ## For synthesis with natural durations:
        if input_wavefile:
            assert os.path.isfile(input_wavefile)
            utt.set("waveform", input_wavefile)

        ## clear up previous test synthesis:
#        utterance_path = os.path.join(test_utterance_location, test_utterance_name)
#        if os.path.isfile(utterance_path):
#            os.remove(utterance_path)
## Do it like to remove ALL old files (including cmp etc in case of using natural durations...)
        old_files = glob.glob(output_location + '/*/' + test_utterance_name +
                              '.*')
        for fname in old_files:
            os.remove(fname)
        ## ---------------------------------
        i = 1
        if self.make_archive:
            utt.archive()
        for processor in self.processors:
            ########################################################
            # if processor.processor_name == "pause_predictor":
            #     continue
            #######################################################
            print "\n==  proc no. %s (%s)  ==" % (i, processor.processor_name)
            #print " == Before:"
            #utt.pretty_print()
            processor.apply_to_utt(
                utt, voice_mode=self.run_mode)  ## utt is changed in place

            #print " == After:"
            if self.make_archive:
                utt.archive()
            ####################################################
            # if (i == 1):
            #     all_node = utt.all_nodes()
            #     for node in all_node:
            #         if node.get("text") == "_END_":
            #             utt.remove(node)
            ###################################################
            i += 1
            ###################################################
            # if processor.processor_name == "pause_predictor":
            #     nodes = utt.all_nodes()
            #     for node in nodes:
            #         if (node.get("token_class") == "space"):
            #             node.set("silence_predicted", "0")
            #             node.remove_children()
            #     print(utt.pretty_print())
            if processor.processor_name == "duration_predictor":
                print(utt.pretty_print())
                nodes = utt.all_nodes()
                bb_e = 0
                for node in nodes:
                    if node.get("text") == "BB":
                        b_node = node
                        bb_e = b_node[-1][-1].get("end")
                        break
            #####################################################

        ############################ EXTENDED CODE ####################################################
        # acoustic_predictor = self.processors[-1]
        # label = utt.get_filename(acoustic_predictor.input_label_filetype)
        # owave = utt.get_filename(acoustic_predictor.output_filetype)

        # print(label)
        # print(owave)

        # streams = acoustic_predictor.model.generate(label, variance_expansion=acoustic_predictor.variance_expansion, \
        #                                         fill_unvoiced_gaps=acoustic_predictor.fill_unvoiced_gaps, name=utt.get('utterance_name'))

        # print(streams)
        ###############################################################################################
        ## Save wave to named file?
        if output_wavefile:
            if not utt.has_external_data("wav"):
                print "Warning: no wave produced for this utt"
            else:
                temp_wave = utt.get_filename("wav")
                ##########################################################################
                signal, sr = sf.read(temp_wave)
                s = int((int(bb_e) / 1000.0) * sr)
                if (bb_e == 0):
                    sf.write(output_wavefile, signal[s:], sr)
                else:
                    sf.write(output_wavefile, signal[s:-1600], sr)
                ##########################################################################
                ## Check files are different; realpath so that e.g. / and // are equivalent:
                # if os.path.realpath(temp_wave) != os.path.realpath(output_wavefile):
                #     shutil.copyfile(temp_wave, output_wavefile)

        if output_labfile:
            if not utt.has_external_data("lab"):
                print "Warning: no lab produced for this utt"
            else:
                temp_lab = utt.get_filename("lab")
                shutil.copyfile(temp_lab, output_labfile)

        if output_extensions != []:
            for ext in output_extensions:
                if not utt.has_external_data(ext):
                    print "Warning: no %s produced for this utt" % (ext)
                else:
                    assert output_wavefile
                    output_file = re.sub('wav\Z', ext, output_wavefile)
                    temp_file = utt.get_filename(ext)
                    shutil.copyfile(temp_file, output_file)

        utt.save()

        if output_uttfile:
            utterance_path = os.path.join(test_utterance_location,
                                          test_utterance_name + '.utt')
            shutil.copyfile(utterance_path, output_uttfile)
    [SIZE_C_B_TL, SIZE_C_B_TR, SIZE_C_B_BL, SIZE_C_B_BR])
SIZE_C_C = VisualWorld.VisualWorld(
    [SIZE_C_C_TL, SIZE_C_C_TR, SIZE_C_C_BL, SIZE_C_C_BR])
SIZE_C_D = VisualWorld.VisualWorld(
    [SIZE_C_D_TL, SIZE_C_D_TR, SIZE_C_D_BL, SIZE_C_D_BR])
SIZE_D_A = VisualWorld.VisualWorld(
    [SIZE_D_A_TL, SIZE_D_A_TR, SIZE_D_A_BL, SIZE_D_A_BR])
SIZE_D_B = VisualWorld.VisualWorld(
    [SIZE_D_B_TL, SIZE_D_B_TR, SIZE_D_B_BL, SIZE_D_B_BR])
SIZE_D_C = VisualWorld.VisualWorld(
    [SIZE_D_C_TL, SIZE_D_C_TR, SIZE_D_C_BL, SIZE_D_C_BR])
SIZE_D_D = VisualWorld.VisualWorld(
    [SIZE_D_D_TL, SIZE_D_D_TR, SIZE_D_D_BL, SIZE_D_D_BR])

# load utterances
COL_A_A_Utterance = UT.Utterance('butterfly')
COL_B_A_Utterance = UT.Utterance('butterfly',
                                 [OF.ObjectFeature('orange', "color")])
COL_C_A_Utterance = UT.Utterance('butterfly',
                                 [OF.ObjectFeature('orange', "color")])
COL_D_A_Utterance = UT.Utterance('butterfly')
COL_A_B_Utterance = UT.Utterance('cap')
COL_B_B_Utterance = UT.Utterance('cap', [OF.ObjectFeature('green', "color")])
COL_C_B_Utterance = UT.Utterance('cap', [OF.ObjectFeature('green', "color")])
COL_D_B_Utterance = UT.Utterance('cap')
COL_A_C_Utterance = UT.Utterance('car')
COL_B_C_Utterance = UT.Utterance('car', [OF.ObjectFeature('black', "color")])
COL_C_C_Utterance = UT.Utterance('car', [OF.ObjectFeature('black', "color")])
COL_D_C_Utterance = UT.Utterance('car')
COL_A_D_Utterance = UT.Utterance('watch')
COL_B_D_Utterance = UT.Utterance('watch', [OF.ObjectFeature('red', "color")])
Exemplo n.º 9
0
rose = PO.PhysicalObject("flower", [OF.ObjectFeature("red", "color")], "rose")
sunflower = PO.PhysicalObject("flower", [OF.ObjectFeature("yellow", "color")],
                              "sunflower")

World = VW.VisualWorld([rose, sunflower])

# prior over the visual world
MyCGPrior = BF.Belief(2, [rose.Id, sunflower.Id], [0.5, 0.5])

# prior over the speaker's production biases. In this case, only color matters
# Use the Beta build in supporting functions
BiasPriors = [SF.BuildBeta(5, 3, "color", 0.5)]

MyListener = CL.ComplexListener(World, MyCGPrior, BiasPriors)

Utterance_flower = UT.Utterance("flower")  # Ambiguous
Utterance_yellowflower = UT.Utterance(
    "flower", [OF.ObjectFeature("yellow", "color")])  # Ambiguous

MyListener.Infer(Utterance_flower)
res = MyListener.ComputePosterior()

#####################################
# Two objects with two features each #
#####################################

rose = PO.PhysicalObject(
    "flower",
    [OF.ObjectFeature("yellow", "color"),
     OF.ObjectFeature("small", "size")], "rose")
sunflower = PO.PhysicalObject(