def prime_gen():
    """A generator function that yields prime numbers using the `Sieve of
    Eratosthenes <http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes>`_
    algorithm.
       
    .. note::
    
        This function is based on the erat2a function which can be found
        `here <http://stackoverflow.com/a/3796442>`_. Variable names were
        changed and comments added for clarity.
    """
    yield 2
    comp_dict = {}
    for num in it_islice(it_count(3), 0, None, 2):
        p = comp_dict.pop(num, None)
        if p is not None:
            # num is a composite. Get the next composite that is not already
            # in the dictionary and that has p as prime factor. Add it to
            # the dictionary. The composite number is thus "sieved" out.
            # By taking a 2*p step, we avoid checking if test is even.
            test = num + 2 * p
            while test in comp_dict:
                test = test + 2 * p
            comp_dict[test] = p
        else:
            # num is a prime.
            # Add the first composite number that has 'num' as the
            # only prime factor to the composite numbers dictionary
            comp_dict[num * num] = num
            # return num
            yield num
    def create_question_maps(self):
        question_id_map = dict()
        ques_to_altq_id_map = dict()
        try:
            logger.info('Creating question maps')
            id_generator = it_count(start=10001, step=1)
            qna_record = namedtuple('qna', [
                'question', 'normalized_ques', 'answer', 'subAnswers',
                'response_type'
            ])
            for qna in self.faq_payload.get('faqs'):
                primary_ques = qna['question']
                alt_ques_payload = qna.get('alternateQuestions', list())
                answer_payload = qna.get('answer', list())
                sub_answer_payload = qna.get('alternateAnswers', list())

                primary_ques_id = next(id_generator)
                question_id_map[primary_ques_id] = qna_record(
                    primary_ques, self.normalize_string(primary_ques),
                    answer_payload, sub_answer_payload,
                    qna.get('responseType'))
                ques_to_altq_id_map[primary_ques_id] = []
                for sub_ques in alt_ques_payload:
                    alt_ques_id = next(id_generator)
                    alt_ques = sub_ques.get('question')
                    question_id_map[alt_ques_id] = qna_record(
                        alt_ques, self.normalize_string(alt_ques), [], [], '')
                    ques_to_altq_id_map[primary_ques_id].append(alt_ques_id)

            return question_id_map, ques_to_altq_id_map
        except Exception:
            logger.error(traceback.format_exc())
            self.print_verbose('Failed in pre processing input')
Example #3
0
def prime_gen():
    """A generator function that yields prime numbers using the `Sieve of
    Eratosthenes <http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes>`_
    algorithm.
       
    .. note::
    
        This function is based on the erat2a function which can be found
        `here <http://stackoverflow.com/a/3796442>`_. Variable names were
        changed and comments added for clarity.
    """
    yield 2
    comp_dict = {}
    for num in it_islice(it_count(3), 0, None, 2):
        p = comp_dict.pop(num, None)
        if p is not None:
            # num is a composite. Get the next composite that is not already
            # in the dictionary and that has p as prime factor. Add it to
            # the dictionary. The composite number is thus "sieved" out.
            # By taking a 2*p step, we avoid checking if test is even.
            test = num + 2 * p
            while test in comp_dict:
                test = test + 2 * p
            comp_dict[test] = p
        else:
            # num is a prime.
            # Add the first composite number that has 'num' as the
            # only prime factor to the composite numbers dictionary
            comp_dict[num * num] = num
            # return num
            yield num
    def create_question_maps(self):
        logger.info('Creating question maps')
        question_id_map = dict()
        ques_to_altq_id_map = dict()
        try:
            id_generator = it_count(start=10001, step=1)
            qna_record = namedtuple('qna', [
                'question', 'normalized_ques', 'answer', 'subAnswers',
                'response_type'
            ])
            for qna in self.faq_payload:
                primary_ques = qna[0]
                answer_obj = self.prepare_answer_object(qna[1])
                answer_payload = copy.deepcopy(answer_obj)
                sub_answer_payload = list()

                primary_ques_id = next(id_generator)
                question_id_map[primary_ques_id] = qna_record(
                    primary_ques, self.normalize_string(primary_ques),
                    answer_payload, sub_answer_payload, 'message')
                ques_to_altq_id_map[primary_ques_id] = []
        except Exception:
            logger.error(traceback.format_exc())
        finally:
            return question_id_map, ques_to_altq_id_map
def prime_wheel_fact_gen():
    """A generator function that yields prime numbers using the `wheel
    factorized <http://en.wikipedia.org/wiki/Wheel_factorization>`_ `Sieve of 
    Eratosthenes`_.
    
    .. note::
    
        This function is based on the erat3 function which can be found
        `here <http://stackoverflow.com/a/3796442>`_. Variable names were
        changed and comments added for clarity.
    """
    yield 2
    yield 3
    yield 5
    # The mask is used with itertools.compress method to generate prime
    # candidates after eliminating numbers using the wheel. The mask
    # contains a 1 when the corresponding number has 2,3 or 5 as a factor
    # only odd numbers are considered so the mask has only 15 values instead
    # of 30.
    wheel_mask = [
        0 if x % 3 == 0 or x % 5 == 0 else 1 for x in range(7, 37, 2)
    ]
    modulos = frozenset(
        [x % 30 for x in range(31, 61, 2) if x % 3 != 0 and x % 5 != 0])
    comp_dict = {}
    for num in it_compress(it_islice(it_count(7), 0, None, 2),
                           it_cycle(wheel_mask)):
        p = comp_dict.pop(num, None)
        if p is not None:
            # num is a composite. Get the next composite that is not already
            # in the dictionary, that meets the wheel criteria and that has p
            # as prime factor. Add it to the dictionary. The composite number
            # is thus "sieved" out.
            # By taking a 2*p step, we avoid checking if test is even.
            test = num + 2 * p
            while test in comp_dict or test % 30 not in modulos:
                test = test + 2 * p
            comp_dict[test] = p
            # delete 'num' from comp_dict to free memory.
        else:
            # num is a prime.
            # Add the first composite number that has 'num' as the
            # only prime factor to the composite numbers dictionary
            comp_dict[num * num] = num
            # return num
            yield num
Example #6
0
def prime_wheel_fact_gen():
    """A generator function that yields prime numbers using the `wheel
    factorized <http://en.wikipedia.org/wiki/Wheel_factorization>`_ `Sieve of 
    Eratosthenes`_.
    
    .. note::
    
        This function is based on the erat3 function which can be found
        `here <http://stackoverflow.com/a/3796442>`_. Variable names were
        changed and comments added for clarity.
    """
    yield 2
    yield 3
    yield 5
    # The mask is used with itertools.compress method to generate prime
    # candidates after eliminating numbers using the wheel. The mask
    # contains a 1 when the corresponding number has 2,3 or 5 as a factor
    # only odd numbers are considered so the mask has only 15 values instead
    # of 30.
    wheel_mask = [0 if x % 3 == 0 or x % 5 == 0 else 1 for x in range(7, 37, 2)]
    modulos = frozenset([x % 30 for x in range(31, 61, 2) if x % 3 != 0 and x % 5 != 0])
    comp_dict = {}
    for num in it_compress(it_islice(it_count(7), 0, None, 2), it_cycle(wheel_mask)):
        p = comp_dict.pop(num, None)
        if p is not None:
            # num is a composite. Get the next composite that is not already
            # in the dictionary, that meets the wheel criteria and that has p
            # as prime factor. Add it to the dictionary. The composite number
            # is thus "sieved" out.
            # By taking a 2*p step, we avoid checking if test is even.
            test = num + 2 * p
            while test in comp_dict or test % 30 not in modulos:
                test = test + 2 * p
            comp_dict[test] = p
            # delete 'num' from comp_dict to free memory.
        else:
            # num is a prime.
            # Add the first composite number that has 'num' as the
            # only prime factor to the composite numbers dictionary
            comp_dict[num * num] = num
            # return num
            yield num
Example #7
0
    def create_question_maps(self):
        logger.info('Creating question maps')
        question_id_map = dict()
        ques_to_altq_id_map = dict()
        faq_row_count = 0
        prev_primary_ques_id = None
        try:
            qna_record = namedtuple('qna', [
                'question', 'normalized_ques', 'answer', 'subAnswers',
                'response_type'
            ])
            id_generator = it_count(start=10001, step=1)
            for row in self.faq_payload:
                if row[0] == 'faq':
                    if row[2] == 'primary':
                        primary_ques_id = next(id_generator)
                        prev_primary_ques_id = copy.deepcopy(primary_ques_id)
                        primary_ques = row[3]
                        answer_obj = self.prepare_answer_object(row[4])
                        answer_payload = copy.deepcopy(answer_obj)
                        sub_answer_payload = list()
                        question_id_map[primary_ques_id] = qna_record(
                            primary_ques, self.normalize_string(primary_ques),
                            answer_payload, sub_answer_payload, 'message')
                        ques_to_altq_id_map[primary_ques_id] = []

                    elif row[2] == 'alternate':
                        alt_ques_id = next(id_generator)
                        alt_ques = row[3]
                        question_id_map[alt_ques_id] = qna_record(
                            alt_ques, self.normalize_string(alt_ques), [], [],
                            '')
                        ques_to_altq_id_map[prev_primary_ques_id].append(
                            alt_ques_id)
                else:
                    break
                faq_row_count += 1
            return question_id_map, ques_to_altq_id_map, faq_row_count
        except Exception:
            logger.error(traceback.format_exc())
            self.print_verbose('Failed in pre processing input')