Commit a draft from after the first working session

This commit is contained in:
Tissevert 2022-11-27 22:24:29 +01:00
commit ce861d4537
4 changed files with 89 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
__pycache__
config.py

42
bot.py Normal file
View file

@ -0,0 +1,42 @@
from mastodon import Mastodon
import config
import memory
import time
secret_file = config.user + '.secret'
def register():
Mastodon.create_app(
config.user,
api_base_url = config.instance,
to_file = secret_file
)
def connect(password):
api = Mastodon(
client_id = secret_file,
api_base_url = config.instance
)
api.log_in(
)
def scalar(a, b):
return len(a.intersection(b))
def answer(db, notifications):
for notification in notifications:
if notification.type == 'mention':
indexed_input = db.index(notification.status.content)
reply = memory.find_best_quote(db, indexed_input)
def main():
api = connect(???)
db = memory.init('pratchett')
last = None
while True:
notifications = api.notifications(since_id=last)
if len(notifications) > 0:
last = notifications[0]
answer(db, notifications)
else:
time.sleep(config.delay)

4
config.py.default Normal file
View file

@ -0,0 +1,4 @@
instance = HOSTNAME_OF_THE_INSTANCE_HOSTING_THE_BOT
user = USER_ACCOUNT_OF_THE_BOT
email = EMAIL_ACCOUNT_OF_THE_BOT
delay = HOW_OFTEN_DOES_THE_BOT_LOOK_FOR_NEW_MESSAGES

41
memory.py Normal file
View file

@ -0,0 +1,41 @@
import csv
import json
WORD_THRESHOLD = 4
def build_db(inputCSV, outputJSON):
db = []
with open(inputCSV, 'r') as file:
csv_reader = csv.reader(file, delimiter=',')
data = False
for row in csv_reader:
if data:
db.append(row + (index(row[1]),))
else:
data = True
with open(outputJSON, 'w') as file:
json.dump(serialize(db), file)
return db
def serialize(db):
return list(map(lambda row: (row[0], row[1], list(row[2])), db))
def unserialize(db):
return list(map(lambda row: (row[0], row[1], set(row[2])), db))
def open_db(filePath):
with open(filePath, 'r') as file:
return unserialize(json.load(file))
def scalar(a, b):
return len(a.intersection(b))
def find_best_quote(db, indexed_input)
max_score = None
for entry in db:
def index(text):
words = map(lambda w: ''.join([c for c in w if c.isalpha()]), text.split(' '))
important_words = set([w for w in words if len(w) > WORD_THRESHOLD])
return important_words