From 6e2d244bc92c52510274101bc192f7772a7efcd5 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Feb 2017 16:00:55 +0100 Subject: [PATCH 1/7] Quickly code a lyrics manager... Hopefully works... --- lyricsmanager.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lyricsmanager.py diff --git a/lyricsmanager.py b/lyricsmanager.py new file mode 100644 index 0000000..e7e3a6f --- /dev/null +++ b/lyricsmanager.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +def addLyrics(text, ref = 0, db = dbtools.dbHelper()): + db.executeQuery("INSERT INTO lyrics(text, ref, active) VALUES('%s', %i, %i);" % (text, ref, (1 if ref == 0 else 0)) + db.commit() + return db.cur.lastrowid + +def queryLyrics(ref = 0): + text = input("Text: ") + ref = int(input("Reference [%i]: " % ref) or 0) + + row = addLyrics(text, ref) + + ans = "" + + while ans.lower() not in ("y", "n"): + ans = input("Add follow-up lyrics? [Y/n] ") + + if ans.lower() != "n": + queryLyrics(row) + +if __name__ == "__main__": + queryLyrics() From 9b9dffe39416a9355bdf6c31cb6b26e7c0e6d8d5 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Feb 2017 16:03:45 +0100 Subject: [PATCH 2/7] Limit text length --- lyricsmanager.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lyricsmanager.py b/lyricsmanager.py index e7e3a6f..8d70329 100644 --- a/lyricsmanager.py +++ b/lyricsmanager.py @@ -7,7 +7,12 @@ def addLyrics(text, ref = 0, db = dbtools.dbHelper()): def queryLyrics(ref = 0): text = input("Text: ") - ref = int(input("Reference [%i]: " % ref) or 0) + + if len(text) > 130: + print("Text too long (%i characters)" % len(text)) + return queryLyrics(ref) + + ref = int(input("Reference [%i]: " % ref) or ref) row = addLyrics(text, ref) From 36351cadb138497445c433a8052b22c02b356085 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Feb 2017 16:07:24 +0100 Subject: [PATCH 3/7] Make lyrics manager executable... --- lyricsmanager.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 lyricsmanager.py diff --git a/lyricsmanager.py b/lyricsmanager.py old mode 100644 new mode 100755 From 374705d13248fc1e802dbe82ed87cfa04f13da95 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Feb 2017 16:08:09 +0100 Subject: [PATCH 4/7] Add matching ) --- lyricsmanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lyricsmanager.py b/lyricsmanager.py index 8d70329..c974def 100755 --- a/lyricsmanager.py +++ b/lyricsmanager.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 def addLyrics(text, ref = 0, db = dbtools.dbHelper()): - db.executeQuery("INSERT INTO lyrics(text, ref, active) VALUES('%s', %i, %i);" % (text, ref, (1 if ref == 0 else 0)) + db.executeQuery("INSERT INTO lyrics(text, ref, active) VALUES('%s', %i, %i);" % (text, ref, (1 if ref == 0 else 0))) db.commit() return db.cur.lastrowid From fa95db9bf40284e64e953b5a83fc3c31efcadeb0 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Feb 2017 16:09:40 +0100 Subject: [PATCH 5/7] Add missing import --- lyricsmanager.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lyricsmanager.py b/lyricsmanager.py index c974def..82d6fb4 100755 --- a/lyricsmanager.py +++ b/lyricsmanager.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 +import dbtools + def addLyrics(text, ref = 0, db = dbtools.dbHelper()): db.executeQuery("INSERT INTO lyrics(text, ref, active) VALUES('%s', %i, %i);" % (text, ref, (1 if ref == 0 else 0))) db.commit() From a8494ebb42113fad5ddf9b9e130e45576beb3751 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Feb 2017 16:10:14 +0100 Subject: [PATCH 6/7] Add lyrics ID to tweet --- lyricsbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lyricsbot.py b/lyricsbot.py index e467e26..afe1e01 100755 --- a/lyricsbot.py +++ b/lyricsbot.py @@ -26,4 +26,4 @@ def postprocess(lid, tid, db = dbtools.dbHelper()): if __name__ == "__main__": lid, text, ref = getLyrics() - postprocess(lid, twitools.tweet(text, ref, setuptools.LYRICS)) + postprocess(lid, twitools.tweet("%s (#%i)" % (text, int(lid)), ref, setuptools.LYRICS)) From ec37a8a88c9c0787c98fc5d5b0baae25aee50536 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Feb 2017 16:10:39 +0100 Subject: [PATCH 7/7] Optimize text sanitation --- markov.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/markov.py b/markov.py index 0eeb2ff..d220e3b 100755 --- a/markov.py +++ b/markov.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import dbtools, setuptools, twitools -import argparse, html, markovify, nltk, operator, random, re, sys +import argparse, html, markovify, nltk, operator, random, re, string, sys class Possy(markovify.NewlineText): def word_split(self, sentence): @@ -14,18 +14,22 @@ class Possy(markovify.NewlineText): return sentence def sanitizeText(text): + split = text.split() try: - if text[0] == "@": - return sanitizeText(text.partition(" ")[2]) - if text.split()[-1][0] == "@": - return sanitizeText(" ".join(text.split()[:-1])) + if "@" in (text[0], text[1]): + if split[1][0] not in string.ascii_lowercase: + return sanitizeText(text.partition(" ")[2]) + if split[-1][0] == "@": + return sanitizeText(" ".join(split[:-1])) + if text[:4] == "RT @": + return sanitizeText(text.partition(":")[2]) except: return "" return text def getText(db = dbtools.dbHelper()): text = "" - for string in db.executeQuery('SELECT text FROM tweets WHERE text NOT LIKE "RT %";'): + for string in db.executeQuery('SELECT text FROM tweets;'): text += sanitizeText(string[0]) + "\n" return html.unescape("".join([s for s in text.strip().splitlines(True) if s.strip()]))