コード例 #1
0
    def increase_worddetails_occured_num(self, word_text, wordtext):
        word_details_query = WordDetails.objects.filter(pub_date=get_occurence(self.datetime), origin=get_source(self.source_name), word=wordtext)
        word_query_length = len(word_details_query)

        if word_query_length == 0:
            worddetails = WordDetails.objects.create(pub_date=get_occurence(self.datetime), occured_num=1, origin=get_source(self.source_name), word=wordtext)
            logging.debug(
                'Created and saved a new WordDetails, for word {0}, datetime "{1}" and source {2} '\
                .format(word_text, worddetails.pub_date.timestamp, worddetails.origin.source))
        if word_query_length == 1:
            worddetails = word_details_query[0]
            worddetails.occured_num += 1
            worddetails.save()
            logging.debug(
                'Retrieved a WordDetails, for word {0}, datetime "{1}" and source {2} and increased occured_num up to {3}'\
                .format(word_text, worddetails.pub_date.timestamp, worddetails.origin.source, worddetails.occured_num))
        if word_query_length > 1:
            worddetails = word_details_query[0]
            logging.error(
                'QUERY LENGTH FOR WORD: {1} ID {0}. IT SHOULD NOT BE 1 OR 0. RETURNED THE FIRST ONE'\
                    .format(len(word_details_query), word_text))
コード例 #2
0
    def save_sentence_and_link(self, fixed_sentence, link, article_title):
        words_collocated_to_link = []
        # source = self.get_site_name()
        words_text_arr = fixed_sentence.split(' ')

        for word_text in words_text_arr:

            wordtext = self.create_wordtext_record_if_there_is_no_one_and_return(word_text)
            worddetails =  self.increase_worddetails_occured_num(word_text, wordtext)
            words_collocated_to_link.append(wordtext)

        link_ = Link.objects.create(pub_date=get_occurence(self.datetime), link=link, origin=get_source(self.source_name), title=article_title)
        logging.debug(
            'Created and saved a new link, link id is {0}, link is for "{1}" sentence '.format(link_.id, article_title))

        for word in words_collocated_to_link:
            link_.words.add(word)
        logging.debug(
            'Collocated {0} words to link  '.format(len(words_collocated_to_link)))