| Bytes | Lang | Time | Link |
|---|---|---|---|
| 098 | AWK | 250910T170459Z | xrs |
| 133 | Julia 1.0 | 230224T162029Z | Ashlin H |
| 054 | Perl | 160510T103145Z | Ton Hosp |
| 082 | Shell | 160520T003258Z | Adam Kat |
| 106 | Python 3 | 160518T052838Z | Hunter V |
| 020 | 05AB1E | 170131T124753Z | Okx |
| 291 | Java 7 | 170201T092113Z | Kevin Cr |
| 096 | R | 170131T180128Z | rturnbul |
| 100 | Python 3 | 170131T171540Z | mbomb007 |
| 025 | Pyke | 160508T120908Z | Blue |
| nan | bash | 160513T223243Z | philcolb |
| 030 | Pyth | 160508T082921Z | Maltysen |
| 122 | Rexx | 160509T220525Z | aja |
| 102 | Ruby | 160508T072834Z | Value In |
| 196 | Tcl 8.6 | 160514T164337Z | Dút |
| 225 | Matlab | 160508T183844Z | Abr001am |
| 158 | Python | 160509T124416Z | Wouldn |
| 245 | PostgreSQL | 160509T101039Z | lad2025 |
| 250 | Sqlserver 2008 | 160509T093941Z | t-clause |
| nan | Perl 5 | 160510T102422Z | Denis Ib |
| 175 | Lua | 160509T171845Z | Blab |
| 218 | Python 2 | 160508T120625Z | Blue |
| 127 | Python 3.5 | 160508T192934Z | R. Kap |
| 080 | Perl 6 | 160508T154206Z | Ven |
| 132 | Python | 160508T184623Z | RootTwo |
| 155 | JavaScript ES6 | 160508T191728Z | Neil |
| 094 | Octave | 160508T085832Z | Stewie G |
| 117 | PowerShell v4 | 160510T020907Z | Tessella |
| 099 | JavaScript ES6 | 160509T104300Z | George R |
| 030 | 05AB1E | 160508T074509Z | Adnan |
| 223 | PHP | 160509T130216Z | MonkeyZe |
| 032 | Pyth | 160508T073248Z | Leaky Nu |
| 097 | Retina | 160508T074606Z | Leaky Nu |
| 025 | Jelly | 160508T155133Z | Dennis |
| 115 | R | 160508T205233Z | Alex A. |
AWK, 98 bytes
{for(gsub(/\.|,/,X);i++<NF;)++b[tolower($i)]
for(j in b)if(y==x=b[j])t=X;else if(x>y&&y=x)t=j}$0=t
Julia 1.0, 140 133 bytes
~s=(d=Dict();split(replace(lowercase(s),r"[^\' 0-9a-z-]"=>"")).|>w->d[w]=get(d,w,0)+1;(x,y)=findmax(d);sum(x.==values(d))<2 ? y : "")
-7 bytes thanks to MarcMush: replace count(i->d[i]==x,keys(d)) with sum(x.==values(d))
Perl, 60 56 55 54 bytes
Includes +3 for -p
#!/usr/bin/perl -p
s/[\pL\d'-]+/$;[$a{lc$&}++]++or$\=$&/eg}{$\x=2>pop@
If a word cannot be just a number you can also drop the a for a score of 53.
Shell, 89 86 82 bytes
grep -Po "[\w'-]+"|sort -f|uniq -ci|sort -nr|awk 'c>$1{print w}c{exit}{c=$1;w=$2}'
This lists all words in the input, then sorts them with counts from most common to least common. The awk call merely ensures that the #2 word doesn't have the same count as the #1 word.
Unwrapped:
grep -Po "[\w'-]+" # get a list of the words, one per line
|sort -f # sort (case insensitive, "folded")
|uniq -ci # count unique entries while still ignoring case
|sort -nr # sort counted data in descending order
|awk '
count > $1 { # if count of most common word exceeds that of this line
print word # print the word saved from it
}
count { # if we have already saved a count (-> we are on line 2)
exit # we always exit on line 2 since we have enough info
}
{ # if true (run on line 1 only due to the above exit)
count = $1 # save the count of the word on this first line
word = $2 # save the word itself
}'
grep -o is the magic tokenizer here. It takes each word (as defined by a regex accepting word characters (letters, numbers, underscore), apostrophe, or hyphen using PCRE given -P) and puts it on its own line. This accepts underscores, as to many other answers here. To disallow underscores, add four characters to turn this portion into grep -oi "[a-z0-9'-]*"
alias cnt='sort -f |uniq -ci |sort -nr' is an old standby of mine. Without regards to case, it alphabetizes (erm, asciibetizes) the lines of the input counts occurrences of each entry, then reverse-sorts by the numeric occurrences so the most popular is first.
awk only looks at the first two lines of that descending ranked list. On line one, count is not yet defined, so it is evaluated as zero and therefore the first two stanzas are skipped (zero == false). The third stanza sets count and word. On the second line, awk has a defined and nonzero value for count, so it compares that count to the second best count. If it's not tied, the saved word is printed. Regardless, the next stanza exits for us.
Test implemented as:
for s in "The man walked down the road." "Slowly, he ate the pie, savoring each delicious bite. He felt like he was truly happy." "This sentence has no most frequent word." "\"That's... that's... that is just terrible\!\" he said." "The old-fashioned man ate an old-fashioned cake." "IPv6 looks great, much better than IPv4, except for the fact that IPv6 has longer addresses." "This sentence with words has at most two equal most frequent words."; do printf "%s\n==> " "$s"; echo "$s" |grep -io "[a-z0-9'-]*"|sort -f|uniq -ci|sort -nr|awk 'c>$1{print w}c{exit}{c=$1;w=$2}'; echo; done
Python 3, 106 bytes
def f(s):s=s.split();z=sorted([s.count(i)for i in set(s)]);return("",max(set(s),key=s.count))[z[-2]<z[-1]]
05AB1E, 22 21 20 bytes
žK„- JÃl#{D.MDgiJëõ?
Explanation:
žK # Push [a-zA-Z0-9]
„- # Push 2-char string containing a hyphen and a space
J # Join the stack into a single element
à # Removes all characters from implicit input except those specified above
l # Converts to lowercase
# # Split string by spaces
{ # Sorts array
D # Duplicates
.M # Finds most common element
Dg # Gets length of string without popping
iJ # If length == 1, then convert the array to a string (otherwise the output would be ['example'] instead of example
ëõ? # Else push an empty string.
Note: If you're fine with trailing newlines in the output for when you're not supposed to output anything, remove the ? at the end to save a byte.
Note #2: The program will not work with a single word, but I doubt this would be a problem. If you want to fix this, replace # with ð¡ for an extra byte.
05AB1E uses CP-1252 as the charset, not UTF-8.
Java 7, 291 bytes
import java.util.*;Object c(String s){List w=Arrays.asList(s.toLowerCase().split("[^\\w'-]+"));Object r=w;int p=0,x=0;for(Object a:w){p=Collections.frequency(w,r);if(Collections.frequency(w,a)>p)r=a;if(p>x)x=p;}for(Object b:w)if(!b.equals(r)&Collections.frequency(w,b)==p)return"";return r;}
The rule where it should output nothing when there are multiple words with the same occurrence took quite a bit of extra code..
Ungolfed:
import java.util.*;
Object c(String s){
List w = Arrays.asList(s.toLowerCase().split("[^\\w'-]+"));
Object r = w;
int p = 0,
x = 0;
for(Object a : w){
p = Collections.frequency(w, r);
if(Collections.frequency(w, a) > p){
r = a;
}
if(p > x){
x = p;
}
}
for(Object b : w){
if(!b.equals(r) & Collections.frequency(w, b) == p){
return "";
}
}
return r;
}
Test code:
import java.util.*;
class M{
static Object c(String s){List w=Arrays.asList(s.toLowerCase().split("[^\\w'-]+"));Object r=w;int p=0,x=0;for(Object a:w){p=Collections.frequency(w,r);if(Collections.frequency(w,a)>p)r=a;if(p>x)x=p;}for(Object b:w)if(!b.equals(r)&Collections.frequency(w,b)==p)return"";return r;}
public static void main(String[] a){
System.out.println(c("The man walked down the road."));
System.out.println(c("Slowly, he ate the pie, savoring each delicious bite. He felt like he was truly happy."));
System.out.println(c("This sentence has no most frequent word."));
System.out.println(c("\"That's... that's... that is just terrible!\" he said."));
System.out.println(c("The old-fashioned man ate an old-fashioned cake."));
System.out
.println(c("IPv6 looks great, much better than IPv4, except for the fact that IPv6 has longer addresses."));
System.out.println(c("This sentence with words has at most two equal most frequent words."));
}
}
Output:
the
he
(nothing)
that's
old-fashioned
ipv6
(nothing)
R, 96 bytes
19 bytes shorter than the existing R answer, with a somewhat different approach.
t=table(gsub("[^a-z0-9'-]","",tolower(scan(,''))))
`if`(sum(t==max(t))-1,'',names(which.max(t)))
Reads from stdin, so the input is automatically separated by spaces. We convert to lowercase and use gsub to remove all non-alphanumerics (plus - and '). We count the instances of each word with table and save the result to t. Next, we check if there is more than 1 maximum in t (by seeing if there is more than one element which is equal to max(t). If so, we return the empty string ''. If not, we return the word corresponding to the maximum in t.
Python 3, 76 98 100 bytes
import re,statistics as S
try:print(S.mode(re.split("([a-z0-9-]+)",input().lower())[1::2]))
except:1
Outputs the most common word as lowercase. Does not include apostrophes because "apostrophes are not required to be included."
statistics.mode requires Python 3.4
Unfortunately, no output to stderr is allowed, or it'd be much shorter.
Pyke, 26 25 bytes
l1dcD}jm/D3Sei/1qIi@j@
(;
Or 23 22 bytes (noncompeting, add node where kills stack if false)
l1cD}jm/D3Sei/1q.Ii@j@
Or with punctuation, 23 bytes (I think this competes? Commit was before the edit)
l1.cD}jm/D3Sei/1q.Ii@j@
Or 12 bytes (definitely noncompeting)
l1.cj.#jR/)e
l1 - input.lower()
.c - punc_split(^)
j - j = ^
.# ) - sort(V(i) for i in ^)
jR/ - j.count(i)
e - ^[-1]
bash, 153 146 131 154 149 137 bytes
declare -iA F
f(){ (((T=++F[$1])==M))&&I=;((T>M))&&M=$T&&I=$1;}
read L
L=${L,,}
L=${L//[^- a-z0-9]}
printf -vA "f %s;" $L
eval $A;echo $I
Operation:
declare an associative array F of integers (declare -iA F)
f is a function that, given a word parameter $1, increments frequency count for this word (T=++F[$1]) and compares to max count so far (M).
If equal, the we have a tie so we will not consider this word to be most frequent (I=)
If greater than max count so far (M), then set max count so far to frequency count of this word so far (M=$T) and remember this word (I=$1)
End function f
Read a line (read L) Make lowercase (L=${L,,}) Remove any character except a-z, 0-9, dash(-) and space (L=${L//[^- a-z0-9]}) Make a sequence of bash statements that calls f for each word (printf -vA "f %s;" $L). This is saved to variable A. eval A and print result (eval $a;echo$I)
Output:
This quick brown fox jumps over this lazy dog.
-->this
This sentence with the words has at most two equal most frequent the words.
-->
The man walked down the road.
-->the
This sentence has no most frequent word.
-->
Slowly, he ate the pie, savoring each delicious bite. He felt like he was truly happy.
-->he
"That's... that's... that is just terrible!" he said.
-->thats
The old-fashioned man ate an old-fashioned cake.
-->old-fashioned
IPv6 looks great, much better than IPv4, except for the fact that IPv6 has longer addresses.
-->ipv6
Bug: FIXED I have a bug that is not revealed in these test cases. If input is
This sentence with words has at most two equal most frequent words.
then my code should output nothing.
I have a fix but I seem to have hit a bash bug... I get very odd behaviour is M is not declared an integer: ++F[$1]==M (after a few repeated words) increments both F[$1] and M!! - my mistake.
Pyth - 23 30 bytes
There has to be a better way to include digits and hyphens, but I just want to fix this right now.
Kc@s+++GUTd\-rzZ)I!tJ.M/KZ{KhJ
Rexx, 109 128 122 bytes
pull s;g.=0;m=0;do i=1 to words(s);w=word(s,i);g.w=g.w+1;if g.w>=m then do;m=g.w;g.m=g.m+1;r=w;end;end;if g.m=1 then say r
Pretty printed...
pull s
g.=0
m=0
do i=1 to words(s)
w=word(s,i)
g.w=g.w+1
if g.w>=m
then do
m=g.w
g.m=g.m+1
r=w
end
end
if g.m=1 then say r
Ruby, 94 92 102 bytes
Gotta go fast (FGITW answer). Returns the word in all uppercase, or nil if there is no most frequent word.
Now updated to new specs, I think. However, I did manage to golf down a little so the byte count is the same!
->s{w=s.upcase.tr("_'",'').scan /[-\w]+/;q=->x{w.count x};(w-[d=w.max_by(&q)]).all?{|e|q[e]<q[d]}?d:p}
Tcl 8.6, 196 bytes
lmap s [join [read stdin] \ ] {dict incr d [regsub -all {[^\w-]} [string tol $s] {}]}
set y [dict fi $d v [lindex [lsort [dict v $d]] end]]
if {[llength $y]!=2} {set y {}}
puts "==> [lindex $y 0]"
(Alas, I can't figure out how to get it any smaller than that...)
Explanation
It uses several obscure Tcl idioms to do stuff.
[join [read stdin] " "]— input string→list of whitespace-separated wordslmap ...— iterate over every element of that list. (Shorter thanforeachand effectually identical since the result is discarded.)[regsub ... [string tolower ...]]— Convert the string to lowercase and strip all characters except for word characters and the hyphen.[dict incr d ...]— Create/modify a dictionary/word→count histogram.set y ...— Sort the dictionary values, take the largest one, and return all (key,value) pairs corresponding to it.if...— There must be exactly two elements: a single (key,value) pair, else there is nothing to print.puts...— Print the key in the key value pair, if any. (No word has spaces.)
You can play with it using CodeChef.
Matlab (225)
- Rules chaneged :/
.
function c=f(a),t=@(x)feval(@(y)y(y>32),num2str(lower(x)-0));f=@(x)num2str(nnz(x)+1);e=str2num(regexprep(a,'([\w''-]+)',' ${t($1)} ${f($`)} ${f([$`,$1])}'));[u,r,d]=mode(e);try c=find(e==d{:});c=a((e(c(1)+1)):(e(c(1)+2)));end
Toolbox is necessary to run this.
How does this work, one of the nicest privileges of regex replace in matlab this it field-executes tokens by calling external-environmental functions parameterized by the tokens caught in the inner environment, so any sequence of
"Word_A Word_B .."is replaced by integers"A0 A1 A2 B0 B1 B2 ..."where the first integer is the numerica ascii signature of the word, the second is the starting index, the third is the ending index, these last two integers dont reduplicate in the whole sequence so i took this advantage to transpose it to an array, then mode it then search the result in that array, so the starting/ending indices will consequently follow.Edit: after changing some details, the program is called function by a string parameter.
20 bytes saved thanks to @StewieGriffin, 30 bytes added reproaches to common-agreed loopholes.
Python, 158 bytes
def g(s):import collections as c,re;l=c.Counter(re.sub('[^\w\s-]',"",s.lower()).split());w,f=l.most_common(1)[0];return[w,""][all(f==i[1]for i in l.items())]
Takes its input like this:
g("Bird is the word")
Should match all the requirements, although it does fail on empty strings, is it necessary to check for those? Sorry for the delay.
Advice / feedback / black magic tips for saving bytes are always welcome
PostgreSQL, 246, 245 bytes
WITH z AS(SELECT DISTINCT*,COUNT(*)OVER(PARTITION BY t,m)c FROM i,regexp_split_to_table(translate(lower(t),'.,"''',''),E'\\s+')m)
SELECT t,CASE WHEN COUNT(*)>1 THEN '' ELSE MAX(m)END
FROM z WHERE(t,c)IN(SELECT t,MAX(c)FROM z GROUP BY t)
GROUP BY t
Output:
Input if anyone is interested:
CREATE TABLE i(t TEXT);
INSERT INTO i(t)
VALUES ('The man walked down the road.'), ('Slowly, he ate the pie, savoring each delicious bite. He felt like he was truly happy.'),
('This sentence has no most frequent word.'), ('"That''s... that''s... that is just terrible!" he said. '), ('The old-fashioned man ate an old-fashioned cake.'),
('IPv6 looks great, much better than IPv4, except for the fact that IPv6 has longer addresses.'), ('a a a b b b c');
Normally I would use
MODE() WITHIN GROUP(...) and it will be much shorter, but it will violate:
If there is no most frequent word (see test case #3), your program should output nothing.
EDIT:
Handling ':
WITH z AS(SELECT DISTINCT*,COUNT(*)OVER(PARTITION BY t,m)c FROM i,regexp_split_to_table(translate(lower(t),'.,"!',''),E'\\s+')m)
SELECT t,CASE WHEN COUNT(*)>1 THEN '' ELSE MAX(m)END
FROM z WHERE(t,c)IN(SELECT t,MAX(c)FROM z GROUP BY t)
GROUP BY t
Output:
╔═══════════════════════════════════════════════════════════════════════════════════════════════╦═══════════════╗
║ t ║ max ║
╠═══════════════════════════════════════════════════════════════════════════════════════════════╬═══════════════╣
║ a a a b b b c ║ ║
║ The old-fashioned man ate an old-fashioned cake. ║ old-fashioned ║
║ IPv6 looks great, much better than IPv4, except for the fact that IPv6 has longer addresses. ║ ipv6 ║
║ This sentence has no most frequent word. ║ ║
║ "That's... that's... that is just terrible!" he said. ║ that's ║
║ The man walked down the road. ║ the ║
║ Slowly, he ate the pie, savoring each delicious bite. He felt like he was truly happy. ║ he ║
╚═══════════════════════════════════════════════════════════════════════════════════════════════╩═══════════════╝
Sqlserver 2008, 250 bytes
DECLARE @ varchar(max) = 'That''s... that''s... that is just terrible!" he said.';
WITH c as(SELECT
@ p,@ x
UNION ALL
SELECT LEFT(x,k-1),STUFF(x,1,k,'')FROM
c CROSS APPLY(SELECT patindex('%[^a-z''-]%',x+'!')k)k
WHERE''<x)SELECT max(p)FROM(SELECT top 1with ties p
FROM c WHERE p>''GROUP BY p
ORDER BY count(*)DESC
)j HAVING count(*)=1
Sqlserver 2016, 174 bytes
Unable to handle data like this example(counting the equals as 3 words):
DECLARE @ varchar(max) = 'That''s... that''s... that is just terrible!" he said. = = ='
SELECT max(v)FROM(SELECT TOP 1WITH TIES value v
FROM STRING_SPLIT(REPLACE(REPLACE(REPLACE(@,'"',''),',',''),'.',''),' ')GROUP
BY value ORDER BY count(*)DESC)x HAVING count(*)=1
Perl 5, 96 92 84 + 2 (-p flag) = 86 bytes
++$h{+lc}for/\w(?:\S*\w)?/g}{$m>$e[1]||$e[1]>$m&&(($_,$m)=@e)||($_="")while@e=each%h
Using:
> echo "The man walked down the road." | perl -p script.pl
Lua, 232 199 175 bytes
w,m,o={},0;io.read():lower():gsub("[^-%w%s]",""):gsub("[%w-]+",function(x)w[x]=(w[x]or 0)+1 end)for k,v in pairs(w)do if m==v then o=''end if(v>m)then m,o=v,k end end print(o)
Python 2, 218 bytes
Assumes more than 2 words. Getting rid of punctuation destroyed me...
import string as z
def m(s):a=[w.lower()for w in s.translate(z.maketrans('',''),z.punctuation).split()];a=sorted({w:a.count(w)for w in set(a)}.items(),key=lambda b:b[1],reverse=1);return a[0][0]if a[0][1]>a[1][1]else''
Python 3.5, 142 137 134 112 117 110 127 bytes:
(+17 bytes, because apparently even if there are words more frequent than the rest, but they have the same frequency, nothing should still be returned.)
def g(u):import re;q=re.findall(r"\b['\-\w]+\b",u.lower());Q=q.count;D=[*map(Q,{*q})];return['',max(q,key=Q)][1in map(D.count,D)]
Should now satisfy all conditions. This submission assumes that at least 1 word is input.
Also, if you want one, here is another version of my function devoid of any regular expressions at the cost of about 43 bytes, though this one is non-competitive anyways, so it does not really matter. I just put it here for the heck of it:
def g(u):import re;q=''.join([i for i in u.lower()if i in[*map(chr,range(97,123)),*"'- "]]).split();Q=q.count;D=[*map(Q,{*q})];return['',max(q,key=Q)][1in map(D.count,D)]
Perl 6, 80 bytes
{$_>1&&.[0].value==.[1].value??""!!.[0].key given .lc.words.Bag.sort:{-.value}}
Let's split the answer into two parts...
given .lc.words.Bag.sort:{-.value}
given is a control statement (like if or for). In Perl 6, they're allowed as postfixes. (a if 1, or like here, foo given 3). given puts its topic (right-hand side) into the special variable $_ for its left-hand side.
The "topic" itself lowercases (lc), splits by word (words), puts the values into a Bag (set with number of occurences), then sorts by value (DESC). Since sort only knows how to operate on lists, the Bag is transformed into a List of Pairs here.
$_>1&&.[0].value==.[1].value??""!!.[0].key
a simple conditional (?? !! are used in Perl 6, instead of ? :).
$_ > 1
Just checks that the list has more than one element.
.[0].value==.[1].value
Accesses to $_ can be shortened... By not specifying the variable. .a is exactly like $_.a. So this is effectively "do both top elements have the same number of occurences" – If so, then we print '' (the empty string).
Otherwise, we print the top element's key (the count): .[0].key.
Python, 132 bytes
import collections as C,re
def g(s):(a,i),(b,j)=C.Counter(re.sub('[^\w\s-]','',s.lower()).split()).most_common(2);return[a,''][i==j]
Above code assumes that input has at least two words.
JavaScript (ES6), 155 bytes
s=>(m=new Map,s.toLowerCase().replace(/[^- 0-9A-Z]/gi,'').split(/\ +/).map(w=>m.set(w,-~m.get(w))),[[a,b],[c,d]]=[...m].sort(([a,b],[c,d])=>d-b),b==d?'':a)
Based on @Blue's Python answer.
Octave, 115 94 bytes
[a,b,c]=unique(regexp(lower(input('')),'[A-z]*','match'));[~,~,d]=mode(c); try disp(a{d{:}})
Accounts for the case with no most frequent word by using try. In this case it outputs nothing, and "takes a break" until you catch the exception.
Saved 21(!) bytes thanks to Luis Mendo's suggestion (using the third output from mode to get the most common word).
The rules have changed quite a bit since I posted my original answer. I'll look into the regex later.
PowerShell (v4), 117 bytes
$y,$z=@($input-replace'[^a-z0-9 \n-]'-split'\s'|group|sort Count)[-2,-1]
($y,($z,'')[$y.Count-eq$z.Count])[!!$z].Name
The first part is easy enough:
$inputis ~= stdin- Regex replace irrelevant characters with nothing, keep newlines so we don't mash two words from the end of a line and the beginning of the next line into one by mistake. (Nobody else has discussed multiple lines, could golf -2 if the input is always a single line).
- Regex split,
Groupby frequency (~= Python's collections.Counter),Sortto put most frequent words at the end. - PowerShell is case insensitive by default for everything.
Handling if there isn't a most frequent word:
- Take the last two items [-2,-1] into $y and $z;
- an N-item list, where N>=2, makes $y and $z the last two items
- a 1-item list makes $y the last item and $z null
- an Empty list makes them both null
Use the bool-as-array-index fake-ternary-operator golf (0,1)[truthyvalue], nested, to choose "", $z or $y as output, then take .Name.
PS D:\> "The man walked down the road."|.\test.ps1
The
PS D:\> "Slowly, he ate the pie, savoring each delicious bite. He felt like he was truly happy."|.\test.ps1
he
PS D:\> "`"That's... that's... that is just terrible!`" he said."|.\test.ps1
Thats
PS D:\> "The old-fashioned man ate an old-fashioned cake."|.\test.ps1
old-fashioned
PS D:\> "IPv6 looks great, much better than IPv4, except for the fact that IPv6 has longer addresses."|.\test.ps1
IPv6
JavaScript (ES6), 99 bytes
F=s=>(f={},w=c='',s.toLowerCase().replace(/[\w-']+/g,m=>(f[m]=o=++f[m]||1)-c?o>c?(w=m,c=o):0:w=''),w)
#input { width: 100%; }
<textarea id="input" oninput="output.innerHTML=F(this.value)"></textarea>
<div id="output"></div>
PHP, 223 bytes
$a=array_count_values(array_map(function($s){return preg_replace('/[^A-Za-z0-9]/','',$s);},explode(' ',strtolower($argv[1]))));arsort($a);$c=count($a);$k=array_keys($a);echo($c>0?($c==1?$k[0]:($a[$k[0]]!=$a[$k[1]]?$k[0]:'')):'');
Retina, 97 bytes
The rules keep changing...
T`L`l
[^-\w ]
O`[-\w]+
([-\w]+)( \1\b)*
$#2;$1
O#`[-\w;]+
.*\b(\d+);[-\w]+ \1;[-\w]+$
!`[-\w]+$
R, 115 bytes
function(s)if(sum(z<-(y=table(tolower((x=strsplit(s,"[^\\w']",,T)[[1]])[x>""])))==max(y))<2)names(which(z))else NULL
This is a function that accepts a string and returns a string if a single word appears more often than others and NULL otherwise. To call it, assign it to a variable.
Ungolfed:
f <- function(s) {
# Create a vector of words by splitting the input on characters other
# than word characters and apostrophes
v <- (x <- strsplit(s, "[^\\w']", perl = TRUE))[x > ""]
# Count the occurrences of each lowercased word
y <- table(tolower(v))
# Create a logical vector such that elements of `y` which occur most
# often are `TRUE` and the rest are fase
z <- y == max(y)
# If a single word occurs most often, return it, otherwise `NULL`
if (sum(z) < 2) {
names(which(z))
} else {
NULL
}
}
