def make_topic(slug): ts = TopicSet({'slug': slug}) if ts.count() > 0: ts.delete() t = Topic({ 'slug': slug, 'titles': [{ 'text': slug, 'primary': True, 'lang': 'en' }] }) t.save() return t
def choose_existing_topic_for_title(title): """ Returns the best existing topic to match with `title` or None if none matches. """ existing_topics = TopicSet.load_by_title(title) if existing_topics.count() == 0: return None from functools import cmp_to_key def is_title_primary(title, topic): all_primary_titles = [ topic.get_primary_title(lang) for lang in topic.title_group.langs ] return title in all_primary_titles def compare(t1, t2): if is_title_primary(title, t1) == is_title_primary(title, t2): # If both or neither match primary title, prefer greater number of sources return getattr(t1, "numSources", 0) - getattr(t2, "numSources", 0) else: # Prefer matches to primary title return 1 if is_title_primary(title, t1) else -1 return max(list(existing_topics), key=cmp_to_key(compare))
def add_langs_to_topics(topic_list: list, use_as_typed=True, backwards_compat_lang_fields: dict = None) -> list: """ adds primary en and he to each topic in topic_list and returns new topic_list :param list topic_list: list of topics where each item is dict of form {'slug': required, 'asTyped': optional} :param dict backwards_compat_lang_fields: of shape {'en': str, 'he': str}. Defines lang fields for backwards compatibility. If None, ignore. :param bool use_as_typed: """ new_topic_list = [] if len(topic_list) > 0: topic_set = {topic.slug: topic for topic in TopicSet({'$or': [{'slug': topic['slug']} for topic in topic_list]})} for topic in topic_list: topic_obj = topic_set.get(topic['slug'], None) if topic_obj is None: continue new_topic = topic.copy() tag_lang = 'en' if use_as_typed: tag_lang = 'he' if is_hebrew(new_topic['asTyped']) else 'en' new_topic[tag_lang] = new_topic['asTyped'] if not use_as_typed or tag_lang == 'en': new_topic['he'] = topic_obj.get_primary_title('he') if not use_as_typed or tag_lang == 'he': new_topic['en'] = topic_obj.get_primary_title('en') if backwards_compat_lang_fields is not None: for lang in ('en', 'he'): new_topic[backwards_compat_lang_fields[lang]] = new_topic[lang] new_topic_list += [new_topic] return new_topic_list