Exemple #1
0
class ImgCreator(object):
    def __init__(self, mongo=None, **kwargs):
        if not mongo:
            self._mongo = MongoConnector(host=kwargs["H"], port=kwargs["mongoport"], db=kwargs["db"])
        else:
            self._mongo = mongo

    def get_cursor(self, word):
        return self._mongo.cursor(word)

    def web_query(self, word):
        word = word.lower()
        return self.analyse(self.get_cursor(word), word)

    def query(self, *args):
        if len(args) == 0:
            self._show_all()
        else:
            for word in args:
                self.analyse(self.get_cursor(word), word)

    def _show_all(self):
        tools = self._mongo.find_all()
        print tools
        for word in tools:
            self.analyse(self.get_cursor(word), word)

    def close(self):
        self._mongo.close()

    def analyse(self, cursor, tool):
        sentiments = cursor.distinct("sentiment")
        data = []
        colours = []
        for sentiment in sentiments:
            count = 0
            for tag in cursor:
                if "sentiment" in tag:
                    if sentiment == tag["sentiment"]:
                        count += 1
            data.append([str(sentiment), count])
            cursor.rewind()
            if sentiment == "neutral":
                colours.append("blue")
            elif sentiment == "positive":
                colours.append("green")
            elif sentiment == "negative":
                colours.append("red")
        cursor.close()
        return data, colours