%% Identify Major Categories of Ontology
% Create a digraph object that describes the AudioSet ontology.

ygraph = yamnetGraph

%
% Visualize the ontology. The ontology consists of 632 separate classes with 670 connections.

p = plot(ygraph);
layout(p,'layered')

%
% Get the name of each sound class. If the sound class has no predecessors, identify it as a major category of the ontology.

nodeNames = ygraph.Nodes.Name;
topCategories = {};
for index = 1:numel(nodeNames)
    pre = predecessors(ygraph,nodeNames{index});
    if isempty(pre)
        topCategories{end+1} = nodeNames{index};
    end
end

%
% Display the categories as an array of strings.

topCategories = string(topCategories)

%
% Highlight and label the top categories on the digraph plot.

highlight(p,topCategories,"NodeColor","red","MarkerSize",8)
labelnode(p,topCategories,topCategories)

% Copyright 2020 The MathWorks, Inc.