def predict(self, data, **kwargs):
        from preprocess import CHARACTERS

        vec_length = math.floor(MAX_FEATURES/3)

        algo = kwargs.get('algo')
        alg = algo()
        train_data = data['train_frame'].iloc[:,:-1]
        target = data['train_frame']['current_speaker']
        clf = alg.train(train_data,target, **algo.args)
        self.importances=clf.feature_importances_

        test_data = data['data']
        match_data = data['current_features']
        reverse_speaker_code_dict = {data['speaker_code_dict'][k] : k for k in data['speaker_code_dict']}

        speaker_list = []
        speaker_codes = reverse_speaker_code_dict.keys()
        for i in xrange(0,len(speaker_codes)):
            s_text = "\n".join(list(data['speakers'][data['speakers']['speaker']==reverse_speaker_code_dict[speaker_codes[i]]]['line']))
            speaker_list.append(s_text)
        speaker_features = data['vectorizer'].batch_get_features(speaker_list)

        self.predictions = []
        counter = 0
        for script in test_data['voice_script']:
            counter+=1
            log.info("On script {0} out of {1}".format(counter,len(test_data['voice_script'])))
            lines = script.split("\n")
            speaker_code = [-1 for i in xrange(0,len(lines))]
            for (i,line) in enumerate(lines):
                if i>0 and i%RESET_SCENE_EVERY!=0:
                    previous_line = lines[i-1]
                    previous_speaker = speaker_code[i-1]
                else:
                    previous_line = ""
                    previous_speaker=  -1

                if i>1 and i%RESET_SCENE_EVERY!=0:
                    two_back_speaker = speaker_code[i-2]
                else:
                    two_back_speaker = -1

                if i<(len(lines)-1):
                    next_line = lines[i+1]
                else:
                    next_line = ""

                prev_features = data['vectorizer'].get_features(previous_line)
                cur_features = data['vectorizer'].get_features(line)
                next_features = data['vectorizer'].get_features(next_line)

                meta_features = make_df([[two_back_speaker], [previous_speaker]],["two_back_speaker", "previous_speaker"])
                #meta_features = make_df([[two_back_speaker]],["two_back_speaker"])
                train_frame = pd.concat([pd.DataFrame(prev_features),pd.DataFrame(cur_features),pd.DataFrame(next_features), meta_features],axis=1)

                speaker_code[i] = alg.predict(train_frame)[0]

                nearest_match, distance = self.find_nearest_match(cur_features, speaker_features)
                if distance<CHARACTER_DISTANCE_MIN:
                    sc = speaker_codes[nearest_match]
                    speaker_code[i] = sc
                    continue

                for k in CHARACTERS:
                    for c in CHARACTERS[k]:
                        if c in previous_line:
                            speaker_code[i] = data['speaker_code_dict'][k]

                nearest_match, distance = self.find_nearest_match(cur_features,match_data)
                if distance<DISTANCE_MIN:
                    sc = data['speakers']['speaker_code'][nearest_match]
                    speaker_code[i] = sc
                    continue

            df = make_df([lines,speaker_code,[reverse_speaker_code_dict[round(s)] for s in speaker_code]],["line","speaker_code","speaker"])
            self.predictions.append(df)
        return data
Change the password
dataset/ETHPy150Open VikParuchuri/simpsons-scripts/tasks/train.py/KNNRF.predict--crossover with--dataset/ETHPy150Open kdart/pycopia/QA/pycopia/QA/ssh_controller.py/ShellController.passwd