LINUX.ORG.RU

История изменений

Исправление Nervous, (текущая версия) :

Прикол №2: поиск слов с наибольшим количеством согласных в алфавитном порядке.

Я смотрю, уже пора песать универсальный механизм для различных поисков. Извольте:

(defn re-some-matches
  "Returns true if any of `regexes` matches `string`."
  [regexes string]
  (boolean (some #(re-matches % string) regexes)))

(defn file-word-seq
  "Returns a sequence of words in `file`, matched by `word-regex`."
  [file word-regex]
  (->> (line-seq (io/reader file))
       (keep #(re-seq word-regex %))
       flatten))

(defn find-words
  "Returns a sequence of words in `file` that match at least one of
  `regexes`."
  ([regexes file]
   (find-words regexes file #"[\p{L}\p{N}]+"))
  ([regexes file word-regex]
   (->> (file-word-seq file word-regex)
        (filter #(re-some-matches regexes %)))))

Задача упрощается — остаётся только найти способ перечислить последовательности символов, которые нужно найти, и наделать из них регулярок. Можно сочинить вручную, можно сгенерировать по какому-нибудь безумному алгоритму.

(def successive-russian-vowels-regexes
  "Match words with 5 (Russian) vowels in alphabetical order."
   ;; add more character sequences --- by hand or generate
   (let [vowel-variants ["аеиоу" "аеуюя"]]
     (map successive-chars-regex vowel-variants)))

(find-words successive-russian-vowels-regexes "/tmp/Звёздная пехота.txt")
;; => ("знаменитому" "экзаменующийся")

Исходная версия Nervous, :

Прикол №2: поиск слов с наибольшим количеством согласных в алфавитном порядке.

Я смотрю, уже пора песать универсальный механизм для различных поисков. Извольте:

(defn re-some-matches
  "Returns true if any of `regexes` matches `string`."
  [regexes string]
  (boolean (some #(re-matches % string) regexes)))

(defn file-word-seq
  "Returns a sequence of words in `file`, matched by `word-regex`."
  [file word-regex]
  (->> (line-seq (io/reader file))
       (keep #(re-seq word-regex %))
       flatten))

(defn find-words
  "Returns a sequence of words in `file` that match at least one of
  `regexes`."
  ([regexes file]
   (find-words regexes file #"[\p{L}\p{N}]+"))
  ([regexes file word-regex]
   (->> (file-word-seq file word-regex)
        (filter #(re-some-matches regexes %)))))

Задача упрощается — остаётся только найти способ перечислить последовательности символов, которые нужно найти. Можно сочинить вручную, можно сгенерировать по какому-нибудь безумному алгоритму.

(def successive-russian-vowels-regexes
  "Match words with 5 (Russian) vowels in alphabetical order."
   ;; add more character sequences --- by hand or generate
   (let [vowel-variants ["аеиоу" "аеуюя"]]
     (map successive-chars-regex vowel-variants)))

(find-words successive-russian-vowels-regexes "/tmp/Звёздная пехота.txt")
;; => ("знаменитому" "экзаменующийся")