#!/usr/bin/env python import json from argparse import ArgumentParser import numpy as np import skfuzzy as fz import skfuzzy.control as ctrl from matplotlib import pyplot as plt if __name__ == '__main__': parser = ArgumentParser(description='calculate a score from posting and voting counters') #parser.add_argument('postings', type=int, help='count of postings') parser.add_argument('positive', type=int, help='count of positive votings') parser.add_argument('negative', type=int, help='count of negative votings') args = parser.parse_args() # inputs, output #postings = ctrl.Antecedent(np.arange(0, 21, 1), 'postings') positive = ctrl.Antecedent(np.arange(0, 11, 1), 'positive') negative = ctrl.Antecedent(np.arange(0, 11, 1), 'negative') score = ctrl.Consequent(np.arange(0, 11, 1), 'score') # populate inputs with auto membership functions #postings.automf(3) positive.automf(3) negative.automf(3) # populate output with auto membership function score.automf(3) #score.view() #plt.waitforbuttonpress() rules = ( #ctrl.Rule(postings['poor'], score['poor']), #ctrl.Rule(postings['average'], score['average']), #ctrl.Rule(postings['good'], score['good']), ctrl.Rule(positive['poor'] & negative['good'], score['poor']), ctrl.Rule(positive['good'] & negative['poor'], score['good']), ctrl.Rule(positive['poor'] & negative['poor'], score['good']), ctrl.Rule(positive['good'] & negative['good'], score['average']), ctrl.Rule(negative['good'], score['poor']), ctrl.Rule(negative['average'], score['average']), ctrl.Rule(negative['poor'], score['good']), ctrl.Rule(positive['good'], score['good']), ctrl.Rule(positive['average'], score['average']), ctrl.Rule(positive['poor'], score['poor']), ) #rules[0].view() #plt.waitforbuttonpress() score_ctrl = ctrl.ControlSystem(rules) score_calc = ctrl.ControlSystemSimulation(score_ctrl) #score_calc.input['postings'] = args.postings score_calc.input['positive'] = args.positive score_calc.input['negative'] = args.negative # compute result score_calc.compute() # retrieve and output result result = score_calc.output['score'] print(json.dumps({'score': round(result, 2)})) #score.view(sim=score_calc) #plt.waitforbuttonpress()