Created
October 30, 2014 18:11
-
-
Save adambard/cfb7ca0ca20712e0162d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns whatlang.core | |
(:require [clojure.java.io :as io] | |
[clojure.string :as str] | |
[clojure.set :as sets] | |
)) | |
(defn load-data | |
"Read the country data file from the resource. The data has been | |
mangled in to lines like so: | |
1|English|68|American Samoa, Antigua and Barbuda, ... | |
So, some secondary splitting on the country values is necessary. | |
" | |
[file] | |
(with-open [reader (io/reader (io/resource file))] | |
(into {} | |
(for [line (line-seq reader)] | |
(let [[rank language country-count country-str] (str/split line #"\|")] | |
[language {:rank rank :countries (set (str/split country-str #", "))}])))) ) | |
(defn all-countries | |
"Not used, but handy in the repl" | |
[langs-data] | |
(into #{} (mapcat :countries (vals langs-data)))) | |
(defn countries-covered | |
"The union of the sets of countries for each language provided" | |
[data languages-learned] | |
(apply sets/union (map #(:countries (get data %)) languages-learned))) | |
(defn all-pairs-result | |
"Get the set of all countries covered by each pair of languages" | |
[] | |
(let [data (load-data "languages.txt") | |
langs (keys data)] | |
(for [lang1 langs | |
lang2 langs | |
:when (not (= lang1 lang2)) ] | |
{:langs #{lang1 lang2} :countries (countries-covered data #{lang1 lang2})} | |
))) | |
(defn all-triples-result | |
"The same as the other one except for triplets" | |
[] | |
(let [data (load-data "languages.txt") | |
langs (keys data) | |
] | |
(for [lang1 langs | |
lang2 langs | |
lang3 langs | |
:when (and (not (= lang1 lang2)) | |
(not (= lang2 lang3)) | |
(not (= lang1 lang3))) | |
] | |
{:langs #{lang1 lang2 lang3} :countries (countries-covered data #{lang1 lang2 lang3})}))) | |
(defn report [result] | |
(->> result | |
(map #(vector (:langs %) (count (:countries %)))) ; Just want counts | |
(into {}) ; To remove duplicate keys | |
(into []) ; Because we don't want a map | |
(sort-by last) | |
(reverse) ; Results from best to worst | |
(map #(str (apply str (interpose ", " (first %))) ": " (last %))) ; Pretty rendering | |
)) | |
(defn -main [] | |
(report (all-pairs-result)) | |
(report (all-triples-result)) | |
nil) |
noisesmith
commented
Oct 30, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment