Added mechanism for splitting long output into a thread
This commit is contained in:
parent
90e42c691e
commit
ab873fb6c9
2
env/prod/resources/config.edn
vendored
2
env/prod/resources/config.edn
vendored
|
@ -4,5 +4,5 @@
|
|||
:app_key "applicationkey"
|
||||
:app_secret "applicationsecret"
|
||||
:user_token "simon-brooke-ireadit"
|
||||
:user_secret "somesortofnonsense"
|
||||
:user_secret "Tartillyan"
|
||||
:bot_account "IReadIt"}
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
;;;; USA.
|
||||
;;;;
|
||||
;;;; Copyright (C) 2016 Simon Brooke for Radical Independence Campaign
|
||||
;;;; Copyright (C) 2019 Simon Brooke
|
||||
;;;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
|
|
@ -4,6 +4,29 @@
|
|||
[ring.util.http-response :as response]
|
||||
[clojure.java.io :as io]))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;;;
|
||||
;;;; ireadit.routes.home: User-oriented web interface.
|
||||
;;;;
|
||||
;;;; This program is free software; you can redistribute it and/or
|
||||
;;;; modify it under the terms of the GNU General Public License
|
||||
;;;; as published by the Free Software Foundation; either version 2
|
||||
;;;; of the License, or (at your option) any later version.
|
||||
;;;;
|
||||
;;;; This program is distributed in the hope that it will be useful,
|
||||
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;;;; GNU General Public License for more details.
|
||||
;;;;
|
||||
;;;; You should have received a copy of the GNU General Public License
|
||||
;;;; along with this program; if not, write to the Free Software
|
||||
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
;;;; USA.
|
||||
;;;;
|
||||
;;;; Copyright (C) 2019 Simon Brooke
|
||||
;;;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(defn home-page []
|
||||
(layout/render "home.html"))
|
||||
|
||||
|
|
|
@ -6,6 +6,30 @@
|
|||
[ring.util.http-response :refer :all]
|
||||
[schema.core :as s]))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;;;
|
||||
;;;; ireadit.routes.services: JSON API.
|
||||
;;;;
|
||||
;;;; This program is free software; you can redistribute it and/or
|
||||
;;;; modify it under the terms of the GNU General Public License
|
||||
;;;; as published by the Free Software Foundation; either version 2
|
||||
;;;; of the License, or (at your option) any later version.
|
||||
;;;;
|
||||
;;;; This program is distributed in the hope that it will be useful,
|
||||
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;;;; GNU General Public License for more details.
|
||||
;;;;
|
||||
;;;; You should have received a copy of the GNU General Public License
|
||||
;;;; along with this program; if not, write to the Free Software
|
||||
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
;;;; USA.
|
||||
;;;;
|
||||
;;;; Copyright (C) 2019 Simon Brooke
|
||||
;;;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
||||
(def service-routes
|
||||
(api
|
||||
{:swagger {:ui "/swagger-ui"
|
||||
|
|
72
src/clj/ireadit/splitter.clj
Normal file
72
src/clj/ireadit/splitter.clj
Normal file
|
@ -0,0 +1,72 @@
|
|||
(ns ireadit.splitter
|
||||
(:require [clojure.string :as s]))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;;;
|
||||
;;;; ireadit.splitter: split OCR output into individual tweets.
|
||||
;;;;
|
||||
;;;; This program is free software; you can redistribute it and/or
|
||||
;;;; modify it under the terms of the GNU General Public License
|
||||
;;;; as published by the Free Software Foundation; either version 2
|
||||
;;;; of the License, or (at your option) any later version.
|
||||
;;;;
|
||||
;;;; This program is distributed in the hope that it will be useful,
|
||||
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;;;; GNU General Public License for more details.
|
||||
;;;;
|
||||
;;;; You should have received a copy of the GNU General Public License
|
||||
;;;; along with this program; if not, write to the Free Software
|
||||
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
;;;; USA.
|
||||
;;;;
|
||||
;;;; Copyright (C) 2019 Simon Brooke
|
||||
;;;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
||||
(def prologue
|
||||
"Prologue for the first tweet in a thread of tweets."
|
||||
"I read it as: \"")
|
||||
|
||||
(def continuation
|
||||
"Epilogue of a single tweet for with there will be a successor."
|
||||
">>>")
|
||||
|
||||
(def usable-length
|
||||
"The maximum length of the payload of a single tweet"
|
||||
(- 220 (+ (count prologue) (count continuation))))
|
||||
|
||||
(defn count-tweet
|
||||
"Count the number of characters in a tweet constructed from this list of tokens."
|
||||
[tokens]
|
||||
(+ (count (rest tokens)) (reduce + (map count tokens))))
|
||||
|
||||
(defn construct-tweets
|
||||
"Take this list of tokens, and return a lost of strings representing
|
||||
individual tweets"
|
||||
([tokens]
|
||||
(let [tweets (map
|
||||
#(s/join " " (reverse %))
|
||||
(construct-tweets tokens nil))]
|
||||
(cons (str prologue (first tweets)) (rest tweets))))
|
||||
([tokens tweet]
|
||||
(cond
|
||||
(empty? tokens) (list (cons "\"" tweet))
|
||||
(<
|
||||
(+
|
||||
(count-tweet tweet)
|
||||
(count (first tokens)))
|
||||
usable-length)
|
||||
(construct-tweets (rest tokens) (cons (first tokens) tweet))
|
||||
true
|
||||
(cons
|
||||
(cons continuation tweet)
|
||||
(construct-tweets tokens nil)))))
|
||||
|
||||
(defn split-into-tweets
|
||||
[input]
|
||||
(if
|
||||
(string? input)
|
||||
(split-into-tweets (s/split input #"\s+"))
|
||||
(construct-tweets input)))
|
|
@ -28,7 +28,7 @@
|
|||
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
;;;; USA.
|
||||
;;;;
|
||||
;;;; Copyright (C) 2016 Simon Brooke for Radical Independence Campaign
|
||||
;;;; Copyright (C) 2019 Simon Brooke
|
||||
;;;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
|
33
test/clj/ireadit/test/splitter.clj
Normal file
33
test/clj/ireadit/test/splitter.clj
Normal file
|
@ -0,0 +1,33 @@
|
|||
(ns ireadit.test.splitter
|
||||
(:require [clojure.string :as s]
|
||||
[clojure.test :refer :all]
|
||||
[ring.mock.request :refer :all]
|
||||
[ireadit.splitter :refer :all]
|
||||
[ireadit.middleware.formats :as formats]
|
||||
[muuntaja.core :as m]
|
||||
[mount.core :as mount]))
|
||||
|
||||
(deftest test-splitter
|
||||
(testing "Splitting a string into tweets"
|
||||
(let [test-input "We believe that the Scottish Parliament should have the
|
||||
right to hold another referendum if there is clear and sustained
|
||||
evidence that independence has become the preferred option of a
|
||||
majority of the Scottish people — or if there is a significant
|
||||
and material change in the circumstances that prevailed in 2014,
|
||||
such as Scotland being taken out of the EU against our will."
|
||||
expected (list "I read it as: \"We believe that the Scottish Parliament should have the right to hold another referendum if there is clear and sustained evidence that independence has become the preferred option of a majority of the >>>"
|
||||
"Scottish people — or if there is a significant and material change in the circumstances that prevailed in 2014, such as Scotland being taken out of the EU against our will. \"")
|
||||
actual (split-into-tweets test-input)]
|
||||
(is (= actual expected)))
|
||||
(let [test-input "We believe that the Scottish Parliament should have the
|
||||
right to hold another referendum if there is clear and sustained
|
||||
evidence that independence has become the preferred option of a
|
||||
majority of the Scottish people — or if there is a significant
|
||||
and material change in the circumstances that prevailed in 2014,
|
||||
such as Scotland being taken out of the EU against our will."
|
||||
tweets (split-into-tweets test-input)
|
||||
expected true
|
||||
actual (s/starts-with? (first tweets) prologue)]
|
||||
(is (= actual expected)))
|
||||
))
|
||||
|
Loading…
Reference in a new issue