function w = stopWords(varargin)
%

% Copyright 2016-2022 The MathWorks, Inc.

narginchk(0,2);

ip = inputParser;
addParameter(ip,'Language','en');
ip.parse(varargin{:});
lang = textanalytics.internal.validateLanguage(ip.Results.Language);

persistent words;
if isempty(words)
    languages = textanalytics.internal.languageCategories;
    fields = [languages; repmat({missing}, size(languages))];
    words = struct(fields{:});
end

if ismissing(words.(lang))
    fname = textanalytics.internal.fileInToolbox(...
        "+textanalytics", "+"+lang,"stopWords.txt");
    fid = fopen(fname,'r','n','UTF-8');
    tmp = onCleanup(@()fclose(fid));
    str = string(fread(fid,'*char').');
    lines = split(lower(str),newline);
    lines(startsWith(lines,"%")) = [];
    lines = string(lines);
    lines(lines=="") = [];
    words.(lang) = reshape(lines,1,[]);
end
w = words.(lang);
