# Path: matcher/model.py
# class IsA(Base):
#     __tablename__ = 'isa'
#     item_id = Column(Integer, primary_key=True, autoincrement=False)
#     entity = Column(postgresql.JSON)
#     qid = column_property('Q' + cast(item_id, String))
#     label = Column(String)
# 
#     def __repr__(self):
#         return f'<matcher.model.IsA object: {self.qid}>'
# 
#     def url(self):
#         return f'https://www.wikidata.org/wiki/Q{self.item_id}'
# 
#     def label_best_language(self, languages, plural=False):
#         if not self.entity:
#             return
# 
#         labels = self.entity['labels']
#         for lang in languages:
#             code = lang if isinstance(lang, str) else lang.wikimedia_language_code
#             if code in labels:
#                 if plural:
#                     return utils.pluralize_label(labels[code])
#                 else:
#                     return labels[code]['value']
# 
#         if plural and 'en' in labels:
#             return utils.pluralize_label(labels['en'])
# 
#         return self.entity_label()
# 
#     def label_and_description_list(self, want_languages):
#         language_codes = {l.wikimedia_language_code for l in want_languages}
#         try:
#             labels = self.entity['labels']
#         except (TypeError, KeyError):
#             return []
#         descriptions = self.entity['descriptions']
# 
#         ret = {}
#         for lang, label in labels.items():
#             if lang not in language_codes:
#                 continue
#             description = descriptions[lang]['value'] if lang in descriptions else None
#             ret[lang] = {'label': label['value'], 'description': description}
#         return ret
# 
#     def label_and_description(self, languages):
#         try:
#             labels = self.entity['labels']
#         except (TypeError, KeyError):
#             return {'lang': None, 'label': None, 'description': None}
#         descriptions = self.entity['descriptions']
#         for lang in languages:
#             code = lang.wikimedia_language_code
#             if code not in labels:
#                 continue
# 
#             description = descriptions[code]['value'] if code in descriptions else None
#             return {
#                 'lang': lang,
#                 'label': labels[code]['value'],
#                 'description': description,
#             }
#         return {'lang': None, 'label': None, 'description': None}
# 
#     def entity_label(self, lang='en'):
#         labels = self.entity['labels']
#         if lang in labels:
#             return labels[lang]['value']
#         elif 'en' in labels:
#             return labels['en']['value']
#         elif labels:
#             return list(labels.values())[0]['value']
# 
#     def label_and_qid(self):
#         if self.entity and 'labels' not in self.entity:
#             subject = f'missing labels: {self.qid}'
#             body = f'Wikidata entity is missing labels\n\n{self.url}'
#             mail.send_mail(subject, body)
#             return self.qid
#         else:
#             return f'{self.entity_label()} ({self.qid})'
# 
#     def labels(self):
#         return self.entity['labels']
# 
# class Language(Base):
#     __tablename__ = 'language'
#     item_id = Column(Integer, primary_key=True, autoincrement=False)
#     iso_639_1 = Column(String(2))
#     iso_639_2 = Column(String(3))
#     iso_639_3 = Column(String(3))
#     wikimedia_language_code = Column(String, unique=True)
#     qid = column_property('Q' + cast(item_id, String))
#     labels = relationship('LanguageLabel',
#                           lazy='dynamic',
#                           foreign_keys=lambda: LanguageLabel.item_id)
# 
#     def english_name(self):
#         return self.labels.filter_by(wikimedia_language_code='en').one().label
# 
#     def self_name(self):
#         ''' Name of this language in this language. '''
#         name = self.labels.filter_by(language=self).one_or_none()
#         if name:
#             return name.label
# 
#     def label(self, with_code=True):
#         name = self.self_name()
#         if not name:  # self label missing for language
#             name = self.english_name()
#         elif self.wikimedia_language_code != 'en':  # add name in English
#             name = capfirst(name) + ' / ' + capfirst(self.english_name())
#         return f'{name} [{self.wikimedia_language_code}]' if with_code else name
# 
#     @property
#     def site_name(self):
#         return f'{self.wikimedia_language_code}wiki'
# 
#     @classmethod
#     def get_by_code(cls, code):
#         return cls.query.filter_by(wikimedia_language_code=code).one()


from collections import Counter
from .model import IsA, Language
    return top_facets

def get_isa_facets2(items, languages=None, min_count=4):
    if languages is None:
        languages = [Language.get_by_code('en')]

    isa_counts = Counter()
    isa_labels = {}

    for item in items:
        for isa in item.isa:
            isa_counts[isa.qid] += 1
            if isa.qid not in isa_labels:
                isa_labels[isa.qid] = isa.label_and_description_list(languages)
            try:
                super_list = [claim['mainsnak']['datavalue']['value']['id']
                              for claim in isa.entity['claims'].get('P279', [])]
            except TypeError:
                super_list = []
            for super_isa in super_list:
                isa_counts[super_isa] += 1

    top_facets = []

    for qid, count in isa_counts.most_common():
        if min_count and count < min_count:
            continue
        if qid in isa_labels:
            label = isa_labels[qid]
        else:

# Path: matcher/model.py
# class IsA(Base):
#     __tablename__ = 'isa'
#     item_id = Column(Integer, primary_key=True, autoincrement=False)
#     entity = Column(postgresql.JSON)
#     qid = column_property('Q' + cast(item_id, String))
#     label = Column(String)
# 
#     def __repr__(self):
#         return f'<matcher.model.IsA object: {self.qid}>'
# 
#     def url(self):
#         return f'https://www.wikidata.org/wiki/Q{self.item_id}'
# 
#     def label_best_language(self, languages, plural=False):
#         if not self.entity:
#             return
# 
#         labels = self.entity['labels']
#         for lang in languages:
#             code = lang if isinstance(lang, str) else lang.wikimedia_language_code
#             if code in labels:
#                 if plural:
#                     return utils.pluralize_label(labels[code])
#                 else:
#                     return labels[code]['value']
# 
#         if plural and 'en' in labels:
#             return utils.pluralize_label(labels['en'])
# 
#         return self.entity_label()
# 
#     def label_and_description_list(self, want_languages):
#         language_codes = {l.wikimedia_language_code for l in want_languages}
#         try:
#             labels = self.entity['labels']
#         except (TypeError, KeyError):
#             return []
#         descriptions = self.entity['descriptions']
# 
#         ret = {}
#         for lang, label in labels.items():
#             if lang not in language_codes:
#                 continue
#             description = descriptions[lang]['value'] if lang in descriptions else None
#             ret[lang] = {'label': label['value'], 'description': description}
#         return ret
# 
#     def label_and_description(self, languages):
#         try:
#             labels = self.entity['labels']
#         except (TypeError, KeyError):
#             return {'lang': None, 'label': None, 'description': None}
#         descriptions = self.entity['descriptions']
#         for lang in languages:
#             code = lang.wikimedia_language_code
#             if code not in labels:
#                 continue
# 
#             description = descriptions[code]['value'] if code in descriptions else None
#             return {
#                 'lang': lang,
#                 'label': labels[code]['value'],
#                 'description': description,
#             }
#         return {'lang': None, 'label': None, 'description': None}
# 
#     def entity_label(self, lang='en'):
#         labels = self.entity['labels']
#         if lang in labels:
#             return labels[lang]['value']
#         elif 'en' in labels:
#             return labels['en']['value']
#         elif labels:
#             return list(labels.values())[0]['value']
# 
#     def label_and_qid(self):
#         if self.entity and 'labels' not in self.entity:
#             subject = f'missing labels: {self.qid}'
#             body = f'Wikidata entity is missing labels\n\n{self.url}'
#             mail.send_mail(subject, body)
#             return self.qid
#         else:
#             return f'{self.entity_label()} ({self.qid})'
# 
#     def labels(self):
#         return self.entity['labels']
# 
# class Language(Base):
#     __tablename__ = 'language'
#     item_id = Column(Integer, primary_key=True, autoincrement=False)
#     iso_639_1 = Column(String(2))
#     iso_639_2 = Column(String(3))
#     iso_639_3 = Column(String(3))
#     wikimedia_language_code = Column(String, unique=True)
#     qid = column_property('Q' + cast(item_id, String))
#     labels = relationship('LanguageLabel',
#                           lazy='dynamic',
#                           foreign_keys=lambda: LanguageLabel.item_id)
# 
#     def english_name(self):
#         return self.labels.filter_by(wikimedia_language_code='en').one().label
# 
#     def self_name(self):
#         ''' Name of this language in this language. '''
#         name = self.labels.filter_by(language=self).one_or_none()
#         if name:
#             return name.label
# 
#     def label(self, with_code=True):
#         name = self.self_name()
#         if not name:  # self label missing for language
#             name = self.english_name()
#         elif self.wikimedia_language_code != 'en':  # add name in English
#             name = capfirst(name) + ' / ' + capfirst(self.english_name())
#         return f'{name} [{self.wikimedia_language_code}]' if with_code else name
# 
#     @property
#     def site_name(self):
#         return f'{self.wikimedia_language_code}wiki'
# 
#     @classmethod
#     def get_by_code(cls, code):
#         return cls.query.filter_by(wikimedia_language_code=code).one()


# Path: matcher/isa_facets.py
from collections import Counter
from .model import IsA, Language

    return top_facets

def get_isa_facets2(items, languages=None, min_count=4):
    if languages is None:
        languages = [Language.get_by_code('en')]

    isa_counts = Counter()
    isa_labels = {}

    for item in items:
        for isa in item.isa:
            isa_counts[isa.qid] += 1
            if isa.qid not in isa_labels:
                isa_labels[isa.qid] = isa.label_and_description_list(languages)
            try:
                super_list = [claim['mainsnak']['datavalue']['value']['id']
                              for claim in isa.entity['claims'].get('P279', [])]
            except TypeError:
                super_list = []
            for super_isa in super_list:
                isa_counts[super_isa] += 1

    top_facets = []

    for qid, count in isa_counts.most_common():
        if min_count and count < min_count:
            continue
        if qid in isa_labels:
            label = isa_labels[qid]
        else:

            isa = IsA.query.get(qid[1:])