<commit_before><?php

namespace Colligator\Console\Commands;

use Colligator\Ontosaur;
use Colligator\Subject;
use EasyRdf\Graph;
use EasyRdf\Resource;
use Illuminate\Console\Command;

class ImportOntosaur extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'colligator:import-ontosaur
                            {--url=https://dl.dropboxusercontent.com/u/1007809/42/ont42.rdf : URL to the RDF file}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import Ontosaur from RDF file.';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param Resource $res
     * @return array
     */
    public function getLabels(Resource $res)
    {
        $languages = ['en', 'nb'];

        $spl = explode('#', $res->getUri());

        // TODO: Remove this when all nodes have labels in the RDF file
        //$defaultLabel = (count($spl) > 1) ? '[' . $spl[1] . ']' : '(no label)';
        $defaultLabel = (count($spl) > 1) ? ucfirst(str_replace('_', ' ', $spl[1])) : '(no label)';

        $labels = [];
        foreach ($res->all('skos:prefLabel') as $label) {
            $labels[$label->getLang()] = $label->getValue();
        }
        foreach ($languages as $lang) {
            $lab = array_get($labels, $lang);
            $labels[$lang] = empty($lab) ? $defaultLabel : $lab;
        }
        return $labels;
    }

    /**
     * @param Resource $res
     * @return array
     */
    public function getNode(Resource $res)
    {
        $labels = $this->getLabels($res);

        $node = [
            'label_nb' => $labels['nb'],
            'label_en' => $labels['en'],
            'id' => $res->getUri(),
            'local_id' => null,
            'documents' => null,
            'document_count' => 0,
        ];

        $subject = Subject::where('term', '=', $labels['nb'])->where('vocabulary', '=', 'noubomn')->first();
        if (is_null($subject)) {
            $this->error('[ImportOntosaur] Subject not found: "' . $labels['nb'] . '"');
        } else {
            $node['local_id'] = $subject->id;
            $node['documents'] = action('DocumentsController@index', [
                'q' => 'subjects.noubomn.id:' . $subject->id,
            ]);

            // Can be used e.g. to determine bubble size in a visualization
            $node['document_count'] = $subject->documents()->count();
        }

        return $node;<commit_msg>[#20] Connect Ontosaur with genres during import<commit_after><?php

namespace Colligator\Console\Commands;

use Colligator\Genre;
use Colligator\Ontosaur;
use Colligator\Subject;
use EasyRdf\Graph;
use EasyRdf\Resource;
use Illuminate\Console\Command;

class ImportOntosaur extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'colligator:import-ontosaur
                            {--url=https://dl.dropboxusercontent.com/u/1007809/42/ont42.rdf : URL to the RDF file}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import Ontosaur from RDF file.';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param Resource $res
     * @return array
     */
    public function getLabels(Resource $res)
    {
        $languages = ['en', 'nb'];

        $spl = explode('#', $res->getUri());

        // TODO: Remove this when all nodes have labels in the RDF file
        //$defaultLabel = (count($spl) > 1) ? '[' . $spl[1] . ']' : '(no label)';
        $defaultLabel = (count($spl) > 1) ? ucfirst(str_replace('_', ' ', $spl[1])) : '(no label)';

        $labels = [];
        foreach ($res->all('skos:prefLabel') as $label) {
            $labels[$label->getLang()] = $label->getValue();
        }
        foreach ($languages as $lang) {
            $lab = array_get($labels, $lang);
            $labels[$lang] = empty($lab) ? $defaultLabel : $lab;
        }
        return $labels;
    }

    /**
     * @param Resource $res
     * @return array
     */
    public function getNode(Resource $res)
    {
        $labels = $this->getLabels($res);

        $node = [
            'label_nb' => $labels['nb'],
            'label_en' => $labels['en'],
            'id' => $res->getUri(),
            'local_id' => null,
            'documents' => null,
            'document_count' => 0,
        ];

        $authority = Subject::where('term', '=', $labels['nb'])->where('vocabulary', '=', 'noubomn')->first();
        $field = 'subjects';
        if (is_null($authority)) {
            $field = 'genres';
            $authority = Genre::where('term', '=', $labels['nb'])->where('vocabulary', '=', 'noubomn')->first();
            if (is_null($authority)) {
                $this->error('[ImportOntosaur] Authority not found: "' . $labels['nb'] . '"');
            }
        }
        if (!is_null($authority)) {
            $node['local_id'] = $authority->id;
            $node['documents'] = action('DocumentsController@index', [
                'q' => $field . '.noubomn.id:' . $authority->id,
            ]);
            // Can be used e.g. to determine bubble size in a visualization
            $node['document_count'] = $authority->documents()->count();
        }

        return $node;
{'fraction_non_alphanumeric': 0.17292418772563176, 'fraction_numerical': 0.006137184115523465, 'mean_word_length': 2.0378289473684212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4318173', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1683, 'n_words': 371}