def post(self):
     username = self.get_current_user()
     
     if username is None:
         self.result("error", "Access denied.")
         return
         
     user = User.get_user(username)
             
     if user.permission_level < constants.PERMISSION_LEVEL_USER:
         self.result("error", "Access denied.")
         return
     
     try:
         displayname = strip_tags(self.get_argument("displayname"))
         email = strip_tags(self.get_argument("email"))
         showemail = bool(self.get_argument("showemail", False))
     except:
         self.result("error", "Invalid arguments. Check that all fields are filled out correctly.")
         return
     
     session = SessionFactory()
     try:
         user.displayname = displayname
         user.email = email
         user.showemail = showemail
         session.add(user)
         session.commit()
         self.result("success", "Settings saved successfully.", user)
     finally:
         session.close()
Example #2
0
def content_ner():
    config = load_config(FLAGS.config_file)
    logger = get_logger(FLAGS.log_file)
    # limit GPU memory
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    with open(FLAGS.map_file, "rb") as f:
        char_to_id, id_to_char, tag_to_id, id_to_tag = pickle.load(f)
    with tf.Session(config=tf_config) as sess:
        model = create_model(sess, Model, FLAGS.ckpt_path, load_word2vec,
                             config, id_to_char, logger)

        for i in range(302):
            newslist = []
            m = i + 1
            f = open('newsLists/%d.txt' % m, 'r')
            for line in f.readlines():
                newslist.append(int(line))
            fout = codecs.open('content_ner/%d.json' % m,
                               'w',
                               encoding='utf-8')
            for i in range(len(newslist)):
                day = newslist[i]
                f = codecs.open('D:/PycharmProjects/news_data/%d.json' % day,
                                encoding='utf-8')
                f_d = json.load(f)
                content = f_d["content"]
                content = strip_tags(content)
                result = model.evaluate_line(
                    sess, input_from_line(content, char_to_id), id_to_tag)
                dicObj = json.dumps(result)
                fout.write(dicObj)
                fout.write("\n")
            fout.close()
Example #3
0
def download_news(start, end, totalPage):
    '''
    根据列表页资讯id索引资讯详情页,下载资讯标题和正文
    '''
    fout = codecs.open('news_for_day/5.txt', 'w', encoding='utf8')
    try:
        url = "http://info.zq88.cn:9085//news/advancedSearch.do?symbol=&page=1&size=%d&start=%d&end=%d&theme=&keyword=&fulltext=&title=&searchType=2&classify=&isFacet=false&group=false" % (
            totalPage, start, end)
        data = request.urlopen(url).read().decode('utf8')
    except Exception as e:
        print(e)
    linedata = json.loads(data)
    newsList = linedata['newsList']
    count = 0
    for newsdict in newsList:
        count += 1
        newsid = newsdict['id']
        print(count, newsid)
        try:
            url = "http://info.zq88.cn:9085//news/detail.do?id=%s" % newsid
            newsdata = request.urlopen(url).read().decode('utf8')
        except Exception as e:
            print(e)
        lineNews = json.loads(newsdata)
        title = lineNews['title']
        content = strip_tags(lineNews['content']).replace('\n', '').replace(
            '\r', '')
        strObj = str(count) + '#' + title + '#' + content + '\n'
        fout.write(strObj)
    fout.close()
 def __generate_game_printout(self, new_def):
     targ, spch = self.__process_step(
         StepDef(type='pose_both', id=1, time=1))
     neutral = {'speech': spch, 'targets': targ}
     # Eventually we probably want to make this cleaner, but for now I need
     # to get a demo going, so we will manually load in the games
     if new_def.game_type == 'simon_says':
         actions_list = simon_says(new_def, self.__process_step,
                                   self.__check_side_seq, neutral)
         self.repeat_shift = 1
     elif new_def.game_type == 'target_touch':
         actions_list = target_touch(new_def, self.__process_step, neutral)
         self.repeat_shift = 0
     elif new_def.game_type == 'stream':  # process_step,neutral
         actions_list = stream(self.__process_step, neutral)
         self.repeat_shift = 0
     path = os.path.expanduser('~/flo_games/')
     if not os.path.exists(path):
         os.makedirs(path)
     path = os.path.join(
         path,
         '{}-{}'.format(new_def.game_type,
                        datetime.datetime.now().strftime("%Y%m%d-%H%M%S")))
     rospy.loginfo('generating new game printout at %s', path)
     with open(path, 'w') as fhd:
         for action in actions_list:
             if 'speech' in action.keys():
                 fhd.write(strip_tags(action['speech']))
                 fhd.write('\n')
Example #5
0
def dirify(s, replace='-'):
    '''
    Transforms an UTF8 string to a string usable in a file name.
    Port of an ddaptation from a script found at http://www.phpit.net/code/dirify/,	2005.12.22 02:24
    @param str The string to dirify.
    @param replace Replacement character (or string) for space characters
    @return The transformed string
    '''
    import sys
    from os.path import abspath, dirname
    path = dirname(dirname(abspath(__file__)))
    sys.path.append(path)
    import re
    from strip_tags import strip_tags
    from remove_accents import remove_accents
    try:
        s = remove_accents(s)
    except UnicodeDecodeError:
        s = remove_accents(s.encode('utf-8'))
    s = s.lower()                       ## lower-case.
    s = strip_tags(s)                   ## remove HTML tags.
    s = re.sub('&#038;[^;\s]+;','', s)  ## remove HTML entities.
    s = re.sub('[^\w\s]',replace, s)    ## remove non-word/space chars.
    s = re.sub('\s+', replace, s)       ## change space chars to replace
    # remove multiple replace
    tmp = replace + replace
    while s.find(tmp) != -1:
        s = s.replace(tmp, replace)
    # if last character is replace, remove it
    if s[-1:] == replace :
        s = s[0:-1]
    # if first character is replace, remove it
    if s[0:1] == replace:
        s = s[1:]
    #
    return s		
    def __run_step(self, idx):
        """Runs a specified step, sending out the necessary actions
        and setting up the callbacks.

        Args:
            idx: the id of the step to run.
        """
        this_step = self.actions_list[idx]
        command_sent = False
        action = GameAction()
        action.step_id = idx
        # each step is a dict with:
        # speach, movement or pose
        if 'speech' in this_step and this_step['speech'] != '':
            if self.humanoid:
                self.__say_plain_text(this_step['speech'])
            self.game_text_pub.publish(strip_tags(this_step['speech']))
            command_sent = True
            action.speech = this_step['speech']
        if 'targets' in this_step:
            move_goal = MoveGoal(this_step['targets'])
            if self.humanoid:
                self.move_server.send_goal(move_goal,
                                           done_cb=self.__moving_done,
                                           active_cb=self.__moving_active,
                                           feedback_cb=self.__moving_feedback)
                self.moving_state = self.action_states.sent
            command_sent = True
            action.targets = this_step['targets']
        if not command_sent:
            rospy.logerr(
                'tried to run a step that did not have any useful info')
        else:
            self.game_action_pub.publish(action)
            self.__set_state(self.states.acting)
            self.__set_options([])
    def do_speak(self, goal):
        """The action handler.

        Note that although it responds to client after the audio play is
        finished, a client can choose
        not to wait by not calling ``SimpleActionClient.waite_for_result()``.
        """

        self.goal_text = strip_tags(goal.text)

        self.state_pub.publish(state=TTSState.SYNTHESIZING,
                               text=self.goal_text)
        res = do_synthesize(goal)
        rospy.loginfo('synthesizer returns: %s', res)

        try:
            res = json.loads(res.result)
        except ValueError as err:
            syn = 'Expecting JSON from synthesizer but got {}'.format(
                res.result)
            rospy.logerr('%s. Exception: %s', syn, err)
            self.state_pub.publish(state=TTSState.ERROR, text=syn)
            self.finish_with_result(syn)
            return

        if 'Audio File' in res:
            audio_file = res['Audio File']
            rospy.loginfo('Will play %s', audio_file)

            mut_d = mutagen.File(audio_file)
            self.length = mut_d.info.length

            self.utterance_pub.publish(self.goal_text)
            msg = SoundRequest()
            # self.sendMsg(SoundRequest.PLAY_FILE, SoundRequest.PLAY_ONCE, sound,
            #      vol=volume, **kwargs)
            msg.sound = SoundRequest.PLAY_FILE
            msg.volume = rospy.get_param("/flo_hum_vol")
            msg.command = SoundRequest.PLAY_ONCE
            msg.arg = audio_file
            msg.arg2 = ""
            goal = SoundRequestGoal()
            goal.sound_request = msg
            self.done = False
            self.sound_client.send_goal(goal,
                                        active_cb=self.sound_received,
                                        feedback_cb=self.sound_fb,
                                        done_cb=self.sound_done)
            # self.result = audio_file
            t_rate = rospy.Rate(10)
            success = True
            while not self.done:
                if self.server.is_preempt_requested():
                    self.sound_client.cancel_goal()
                    self.server.set_preempted()
                    success = False
                    break
                t_rate.sleep()
            self.state_pub.publish(state=TTSState.WAITING)
            if success:
                self.finish_with_result('completed sound play in')
        else:
            rospy.logerr('No audio file in synthesize voice result')
            self.state_pub.publish(state=TTSState.ERROR, text='no audio file')
            self.finish_with_result('no audio file')
Example #8
0
if args['file']:
    f = open(args['file'])
    text = f.read()

#################################################################################
## Handle arguments
#################################################################################
if text is not None:
    if args['lc']:
        print lower_case(text, output_file)
 
    if args['uc']:
        print upper_case(text, output_file)

    if args['strip']:
        print strip_tags(text, output_file)
    
    if args['compress']:
        print compress_text(text, output_file)
    
    if args['count']:
        print character_count(text, output_file)
        
    if args['format']:
        print format_html(text, output_file)
        
    if args['sentiment']:
        print sentiment_analysis(text, output_file)
        
    if args['translate']:
        if args['from'] and args['to'] is not None: