27
\$\begingroup\$

What is the most frequent word?

Given a sentence, your program must make its way through it, counting the frequencies of each word, then output the most used word. Because a sentence has no fixed length, and so can get very long, your code must be as short as possible.

Rules/Requirements

  • Each submission should be either a full program or function. If it is a function, it must be runnable by only needing to add the function call to the bottom of the program. Anything else (e.g. headers in C), must be included.
  • There must be a free interpreter/compiler available for your language.
  • If it is possible, provide a link to a site where your program can be tested.
  • Your program must not write anything to STDERR.
  • Your program should take input from STDIN (or the closest alternative in your language).
  • Standard loopholes are forbidden.
  • Your program must be case-insensitive (tHe, The and the all contribute to the count of the).
  • If there is no most frequent word (see test case #3), your program should output nothing.

Definition of a 'word':

You get the list of words by splitting the input text on spaces. The input will never contain any other type of whitespace than plain spaces (in particular no newlines). However, the final words should only contain alphanumerics (a-z, A-Z, 0-9), hyphens (-) and apostrophes ('). You can make that so by removing all other characters or by replacing them by space before doing the word splitting. To remain compatible with previous versions of the rules, apostrophes are not required to be included.

Test Cases

The man walked down the road.
==> the

-----

Slowly, he ate the pie, savoring each delicious bite. He felt like he was truly happy.
==> he

-----

This sentence has no most frequent word.
==> 

-----

"That's... that's... that is just terrible!" he said.
==> that's / 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

-----

This sentence with words has at most two equal most frequent words.
==>

Note: The third and seventh test cases have no output, you may choose either on the fourth.

Scoring

Programs are scored according to bytes. The usual character set is UTF-8, if you are using another please specify.

When the challenge finishes, the program with the least bytes (it's called ), will win.

Submissions

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

Leaderboard

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

/* Configuration */

var QUESTION_ID = 79576; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 53406; // This should be the user ID of the challenge author.

/* App */

var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;

function answersUrl(index) {
  return "https://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

function commentUrl(index, answers) {
  return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}

function getAnswers() {
  jQuery.ajax({
    url: answersUrl(answer_page++),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      answers.push.apply(answers, data.items);
      answers_hash = [];
      answer_ids = [];
      data.items.forEach(function(a) {
        a.comments = [];
        var id = +a.share_link.match(/\d+/);
        answer_ids.push(id);
        answers_hash[id] = a;
      });
      if (!data.has_more) more_answers = false;
      comment_page = 1;
      getComments();
    }
  });
}

function getComments() {
  jQuery.ajax({
    url: commentUrl(comment_page++, answer_ids),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      data.items.forEach(function(c) {
        if (c.owner.user_id === OVERRIDE_USER)
          answers_hash[c.post_id].comments.push(c);
      });
      if (data.has_more) getComments();
      else if (more_answers) getAnswers();
      else process();
    }
  });  
}

getAnswers();

var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;

var OVERRIDE_REG = /^Override\s*header:\s*/i;

function getAuthorName(a) {
  return a.owner.display_name;
}

function process() {
  var valid = [];
  
  answers.forEach(function(a) {
    var body = a.body;
    a.comments.forEach(function(c) {
      if(OVERRIDE_REG.test(c.body))
        body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
    });
    
    var match = body.match(SCORE_REG);
    if (match)
      valid.push({
        user: getAuthorName(a),
        size: +match[2],
        language: match[1],
        link: a.share_link,
      });
    
  });
  
  valid.sort(function (a, b) {
    var aB = a.size,
        bB = b.size;
    return aB - bB
  });

  var languages = {};
  var place = 1;
  var lastSize = null;
  var lastPlace = 1;
  valid.forEach(function (a) {
    if (a.size != lastSize)
      lastPlace = place;
    lastSize = a.size;
    ++place;
    
    var answer = jQuery("#answer-template").html();
    answer = answer.replace("{{PLACE}}", lastPlace + ".")
                   .replace("{{NAME}}", a.user)
                   .replace("{{LANGUAGE}}", a.language)
                   .replace("{{SIZE}}", a.size)
                   .replace("{{LINK}}", a.link);
    answer = jQuery(answer);
    jQuery("#answers").append(answer);

    var lang = a.language;
    if (/<a/.test(lang)) lang = jQuery(lang).text();
    
    languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link};
  });

  var langs = [];
  for (var lang in languages)
    if (languages.hasOwnProperty(lang))
      langs.push(languages[lang]);

  langs.sort(function (a, b) {
    if (a.lang > b.lang) return 1;
    if (a.lang < b.lang) return -1;
    return 0;
  });

  for (var i = 0; i < langs.length; ++i)
  {
    var language = jQuery("#language-template").html();
    var lang = langs[i];
    language = language.replace("{{LANGUAGE}}", lang.lang)
                       .replace("{{NAME}}", lang.user)
                       .replace("{{SIZE}}", lang.size)
                       .replace("{{LINK}}", lang.link);
    language = jQuery(language);
    jQuery("#languages").append(language);
  }

}
body { text-align: left !important}

#answer-list {
  padding: 10px;
  width: 290px;
  float: left;
}

#language-list {
  padding: 10px;
  width: 290px;
  float: left;
}

table thead {
  font-weight: bold;
}

table td {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="answer-list">
  <h2>Leaderboard</h2>
  <table class="answer-list">
    <thead>
      <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
    </thead>
    <tbody id="answers">

    </tbody>
  </table>
</div>
<div id="language-list">
  <h2>Winners by Language</h2>
  <table class="language-list">
    <thead>
      <tr><td>Language</td><td>User</td><td>Score</td></tr>
    </thead>
    <tbody id="languages">

    </tbody>
  </table>
</div>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – Doorknob
    Commented May 11, 2016 at 11:28
  • 1
    \$\begingroup\$ So given your new definition of 'word', what is the most common word here don't d'ont dont a a? Would it be dont? \$\endgroup\$
    – DJMcMayhem
    Commented May 11, 2016 at 15:34
  • \$\begingroup\$ @DrGreenEggsandHamDJ If you have a submission that does remove apostrophes, dont. If not, a. but most submissions do, and so dont is a correct answer. \$\endgroup\$
    – user53406
    Commented May 11, 2016 at 15:36
  • 1
    \$\begingroup\$ Is the output case-sensitive? So is ipv6 valid output for the last test case? \$\endgroup\$ Commented May 13, 2016 at 22:16
  • 1
    \$\begingroup\$ An extra test case may be of use: "This sentence with words has at most two equal most frequent words." --> <nothing> \$\endgroup\$ Commented May 14, 2016 at 7:12

34 Answers 34

1
2
0
\$\begingroup\$

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

Try it online

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.

\$\endgroup\$
3
  • \$\begingroup\$ You're not allowed to print to STDERR, unless this program doesn't produce any error output? \$\endgroup\$
    – Okx
    Commented Jan 31, 2017 at 17:17
  • \$\begingroup\$ Your new program doesn't support hyphens! I tried the input i- test i- \$\endgroup\$
    – Okx
    Commented Jan 31, 2017 at 17:38
  • \$\begingroup\$ Fixed it all. Still short. \$\endgroup\$
    – mbomb007
    Commented Jan 31, 2017 at 17:54
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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:

Try it here.

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)
\$\endgroup\$
0
\$\begingroup\$

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]]
\$\endgroup\$
2
  • \$\begingroup\$ You can use s.split() \$\endgroup\$
    – mbomb007
    Commented Jan 31, 2017 at 17:41
  • \$\begingroup\$ split method by default uses spaces, so you can save 3 bytes by changing s.split(" ") to s.split() \$\endgroup\$
    – sagiksp
    Commented Feb 1, 2017 at 12:00
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.