forked from jianxiongxiao/ProfXkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indefiniteArticle.m
79 lines (67 loc) · 2.47 KB
/
indefiniteArticle.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function article=indefiniteArticle(phrase)
% A simple function that returns the indefinite articles "a" or "an" based on a given word or phrase.
% based on the javascript implementation: https://github.com/rigoneri/indefinite-article.js/blob/master/indefinite-article.js
% indefiniteArticle('')
% indefiniteArticle('apple pie')
% indefiniteArticle('vision group')
% indefiniteArticle('Priceton local restaurant')
% Getting the first word
[~,match] = regexp(phrase,'\w+');
if ~isempty(match)
word = phrase(1:match(1));
else
article=''; return;
end
l_word = lower(word);
% Specific start of words that should be preceeded by 'an'
alt_cases = {'honest', 'hour', 'hono'};
for i=1:length(alt_cases)
t = strfind(l_word, alt_cases{i});
if (~isempty(t) && t(1) == 1)
article='an'; return;
end
end
% Single letter word which should be preceeded by 'an'
if length(l_word) == 1
if ~isempty(strfind('aedhilmnorsx',l_word))
article='an'; return;
else
article='a'; return;
end
end
% Capital words which should likely be preceeded by 'an'
match = regexp(word,'(?!FJO|[HLMNS]Y.|RY[EO]|SQU|(F[LR]?|[HL]|MN?|N|RH?|S[CHKLMNPTVW]?|X(YL)?)[AEIOU])[FHLMNRSX][A-Z]', 'once');
if ~isempty(match)
article='an'; return;
end
% Special cases where a word that begins with a vowel should be preceeded by 'a'
regexes = {'^e[uw]', '^onc?e\b', '^uni([^nmd]|mo)', '^u[bcfhjkqrst][aeiou]'};
for i=1:length(regexes)
match = regexp(l_word,regexes{i}, 'once');
if ~isempty(match)
article='a'; return;
end
end
% Special capital words (UK, UN)
match = regexp(word,'^U[NK][AIEO]', 'once');
if ~isempty(match)
article='a'; return;
else
if strcmp(word, upper(word))
if ~isempty(strfind('aedhilmnorsx',l_word(1)))
article='an'; return;
else
article='a'; return;
end
end
end
% Basic method of words that begin with a vowel being preceeded by 'an'
if ~isempty(strfind('aeiou',l_word(1)))
article='an'; return;
end
% Instances where y follwed by specific letters is preceeded by 'an'
match = regexp(l_word,'^y(b[lor]|cl[ea]|fere|gg|p[ios]|rou|tt)', 'once');
if ~isempty(match)
article='an'; return;
end
article='a'; return;