codingdojo/markov/filter.py
2023-09-28 23:51:38 +02:00

101 lines
2.6 KiB
Python
Executable file

#!/usr/bin/env python3
#
# Copyright 2023 David Soulayrol.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
import args
import json
import sys
class Store:
def __init__(self):
self.__store = {}
self.__tags = {}
self.__tag_generator = 1000
def add_word(self, ante, word):
self.__store.setdefault(word, {'n': 0, 'sums': {}})
if len(ante):
props = self.__store[ante]
props['n'] += 1
if word in props['sums']:
props['sums'][word] += 1
else:
props['sums'][word] = 1
def get_tag(self, word):
tag = self.__tags.get(word)
if tag is None:
self.__tag_generator += 1
tag = self.__tags.setdefault(word, self.__tag_generator)
return tag
def export(self):
exported = {}
def compute_statistics(n, sums):
stats = {}
for w, count in sums.items():
stats[w] = count / n
return stats
for word, props in self.__store.items():
# Compute statistics
exported[word] = compute_statistics(props['n'], props['sums'])
return exported
def export_with_tags(self):
exported = {}
def compute_statistics(n, sums):
stats = {}
for w, count in sums.items():
stats[self.get_tag(w)] = count / n
return stats
for word, props in self.__store.items():
# Compute statistics
exported[self.get_tag(word)] = {
'w': word,
's': compute_statistics(props['n'], props['sums'])
}
return exported
if __name__ == '__main__':
properties = args.parse('t:tags:b', sys.argv[1:])
store = Store()
ante = ''
word = ''
for c in iter(lambda: sys.stdin.read(1), ''):
if c == ' ':
store.add_word(ante, word)
ante = word
word = ''
else:
word += c
# Handle the last word
store.add_word(ante, word)
if properties.tags:
sys.stdout.write(json.dumps(store.export_with_tags()))
else:
sys.stdout.write(json.dumps(store.export()))