Пример #1
0
    def _generate_triples_for_existing_song(self, new_song, song_uri, dataset_uri, ngram_repo):
        old_song = self._graph.get_song_by_uri(uri=song_uri)
        set_old_forms = set(old_song.identifying_forms)
        # Update canonical to include this source
        for triple in self.update_canonical_to_include_source(entity_uri=song_uri,
                                                              dataset_uri=dataset_uri):
            yield triple

        # #####################
        # Canonical
        if new_song.canonical == old_song.canonical:
            pass  # We don't need to do anything
        elif new_song.canonical not in set_old_forms:
            for triple in self._generate_alt_title_triples_for_existing_song(song_uri=song_uri,
                                                                             song=old_song,
                                                                             dataset_uri=dataset_uri,
                                                                             alt_title=new_song.canonical):
                yield triple
        else:
            for triple in self._generate_updating_triples_for_existing_alt_title(song_uri=song_uri,
                                                                                 dataset_uri=dataset_uri,
                                                                                 alt_title=new_song.canonical):
                yield triple

        # #####################
        # Artists
        # # Triples of the artists
        for artist in new_song.artists:
            artist_uri = self._artist_to_triples.get_existing_artist_uri(artist=artist)
            is_new_artist = False
            if artist_uri is None:
                artist_uri = self.generate_entity_uri(entity=artist,
                                                      alt_str=ArtistRawToTriplesUtils.prepare_alt_str_for_raw_artist(
                                                          artist))
                is_new_artist = True
            for triple in self._artist_to_triples. \
                    generate_needed_artist_of_unknown_type_triples(artist=artist,
                                                                   dataset_uri=dataset_uri,
                                                                   artist_uri=artist_uri,
                                                                   is_new_artist=is_new_artist):
                yield triple
            # # Linking artist and song
            yield (song_uri, FOAF.maker, artist_uri)  # It may be repeated, but it does not matter.

        # ########################
        # Alt_titles
        for an_alt_title in new_song.alternative_titles:
            if an_alt_title == old_song.canonical:
                pass  # Nothing to do. Work already done.
            elif an_alt_title not in set_old_forms:
                for triple in self._generate_alt_title_triples_for_existing_song(song=old_song,
                                                                                 song_uri=song_uri,
                                                                                 dataset_uri=dataset_uri,
                                                                                 alt_title=an_alt_title):
                    yield triple

            else:
                for triple in self._generate_updating_triples_for_existing_alt_title(song_uri=song_uri,
                                                                                     dataset_uri=dataset_uri,
                                                                                     alt_title=new_song.canonical):
                    yield triple


        # ########################
        # Country
        if new_song.country != old_song.country:
            pass  # TODO: NOT NECESSARY AT THIS POINT
        else:
            pass  # TODO: NOT NECESSARY AT THIS POINT

        # # Discogs elems
        if new_song.discogs_index is not None:
            for triple in SongToTriples._generate_discogs_id_triples(entity=new_song,
                                                                     entity_uri=song_uri):
                yield triple

        if new_song.discogs_id is not None:
            for triple in SongToTriples._generate_discogs_index_triples(entity=new_song,
                                                                        entity_uri=song_uri):
                yield triple

        ##USOS elems
        if new_song.usos_transaction_id is not None:
            for triple in SongToTriples._generate_usos_transaction_id(entity=new_song,
                                                                      entity_uri=song_uri):
                yield triple

        if new_song.usos_isrc is not None:
            for triple in SongToTriples._generate_usos_isrc(entity=new_song,
                                                            entity_uri=song_uri):
                yield triple
Пример #2
0
    def _generate_triples_for_new_song(self, song, dataset_uri, isolated_song=False):
        song_uri = self.generate_entity_uri(entity=song,
                                            alt_str=SongToTriples.prepare_alt_str_for_song(song))
        # # triple of type
        yield (song_uri, RDF.type, MO.track)

        # # canonical
        for triple in SongToTriples._generate_canonical_triples(song, song_uri, dataset_uri,
                                                                self._ngram_repo):
            yield triple


        # # Triples of the artists
        artist_uri = None
        for artist in song.artists:
            is_new_artist = False
            if not isolated_song:
                artist_uri = self._artist_to_triples.get_existing_artist_uri(artist=artist)
                is_new_artist = False
            else:
                artist_uri = None
            if artist_uri is None:
                artist_uri = self.generate_entity_uri(entity=artist,
                                                      alt_str=ArtistRawToTriplesUtils.prepare_alt_str_for_raw_artist(
                                                          artist))
                is_new_artist = True
            for triple in self._artist_to_triples. \
                    generate_needed_artist_of_unknown_type_triples(artist=artist,
                                                                   dataset_uri=dataset_uri,
                                                                   artist_uri=artist_uri,
                                                                   is_new_artist=is_new_artist):
                yield triple

            # # Linking artist and song
            yield (song_uri, FOAF.maker, artist_uri)


        # # Alternative titles
        for triple in SongToTriples._generate_alt_titles_triples_of_song(song=song,
                                                                         song_uri=song_uri,
                                                                         dataset_uri=dataset_uri,
                                                                         ngram_repo=self._ngram_repo,
        ):
            yield triple

        # #  Country
        if song.country is not None:
            for triple in SongToTriples._generate_country_triples(entity=song,
                                                                  entity_uri=song_uri,
                                                                  dataset_uri=dataset_uri):
                yield triple

        # # Discogs elems
        if song.discogs_id is not None:
            for triple in SongToTriples._generate_discogs_id_triples(entity=song,
                                                                     entity_uri=song_uri):
                yield triple

        if song.discogs_index is not None:
            for triple in SongToTriples._generate_discogs_index_triples(entity=song,
                                                                        entity_uri=song_uri):
                yield triple

        ##USOS elems
        if song.usos_transaction_id is not None:
            for triple in SongToTriples._generate_usos_transaction_id(entity=song,
                                                                      entity_uri=song_uri):
                yield triple

        if song.usos_isrc is not None:
            for triple in SongToTriples._generate_usos_isrc(entity=song,
                                                            entity_uri=song_uri):
                yield triple

        increase_song_count(entity_counter_repo=self._entity_counter_repo)