Пример #1
0
    def post(self):
        """


        :return:
        """
        pComment = unicode(self.request.get('comment'))
        pCategoryName = categoryName=unicode(self.request.get('category'))
        #TODO change it to more optimal


        categories = db.GqlQuery('select * from OurCategory where categoryName = :1', pCategoryName)
        category = None
        if categories.count() == 0:
            category = OurCategory(categoryName = pCategoryName)
            category.put()
        else:
            category = categories.get()
        pSentiment = "Positive" #TODO get probabilities and set sentiment
        pLat = float(self.request.get('lat'))
        pLon = float(self.request.get('lon'))
        pLocation = db.GeoPt(lat=pLat, lon=pLon)
        geocoder = Geocoder()
        details = geocoder.getDetailedPosition(pLat, pLon)
        if pComment is u"":
            self.response.write("Comment cannot be empty.")
            return

        sentiment = getSentimentOffline(pComment)

        # if sentiment[0] == 'pos':
        #     pSentiment = "Positive"
        # elif sentiment[0] == 'neg':
        #     pSentiment = "Negative"
        # else:
        #     pSentiment = 'Neutral'
        pSentiment = sentiment[0]
        pProbabilityPos = sentiment[1]
        pProbabilityNeg = sentiment[2]
        pProbabilityNeu = float(0)

        opinion = Opinion(comment=pComment, sentiment=pSentiment, location=pLocation, city=details.city,
                          continent=details.continent, country=details.country, region=details.region, category=category,
                          probabilityPos=pProbabilityPos, probabilityNeg=pProbabilityNeg, probabilityNeu=pProbabilityNeu)
        for prop in details.properties():
            if prop in ['city', 'continent']:
                continue
            counter = Counter.all()
            key = getattr(details, prop)
            if not key:
                continue
            counter.filter("areaName =", key)
            entity = counter.get()
            parentProp = parentPropOf[prop]
            if entity is None:
                parentKey = getattr(details, str(parentProp), 'root')
                entity = Counter(areaName=key, parentKey=parentKey)

            if pSentiment == 'pos':
                entity.countPos += 1
            elif pSentiment == 'neg':
                entity.countNeg += 1
            entity.put()

        opinion.put()
        self.redirect('/home')
Пример #2
0
def addOpinion(comment, pCategoryName, lat, lon):
    pLocation = db.GeoPt(lat=lat, lon=lon)
    geocoder = Geocoder()
    details = geocoder.getDetailedPosition(lat, lon)
    comment = unicode(comment)
    categories = db.GqlQuery('select * from OurCategory where categoryName = :1', pCategoryName)
    category = None
    if categories.count() == 0:
        category = OurCategory(categoryName=pCategoryName)
        category.put()
    else:
        category = categories.get()

    if comment is u"":
        raise Exception("comment cant be empty")

    sentiment = getSentimentOffline(comment)
    pSentiment = sentiment[0]
    pProbabilityPos = sentiment[1]
    pProbabilityNeg = sentiment[2]
    pProbabilityNeu = sentiment[3]

    logging.info(str((
        comment, pSentiment, pLocation, details.city, details.continent, details.country, details.region, category,
        pProbabilityPos, pProbabilityNeg, pProbabilityNeu)))


    highInformation = False
    for prob in sentiment[1:]:
        if prob > 0.6:
            highInformation = True
            break
    if math.fabs(pProbabilityNeg - pProbabilityPos) > 0.1:
        highInformation = True

    if not highInformation:
        logging.info("discarding opinion because of low information")
        return

    # if pSentiment == "neutral":
    #     logging.info("discarding opinion because of netral sentiment")
    #     return


    opinion = Opinion(comment=comment, sentiment=pSentiment, location=pLocation, city=details.city,
                      continent=details.continent, country=details.country, region=details.region, category=category,
                      probabilityPos=pProbabilityPos, probabilityNeg=pProbabilityNeg, probabilityNeu=pProbabilityNeu)
    for prop in details.properties():
        if prop in ['city', 'continent']:
            continue
        counter = Counter.all()
        key = getattr(details, prop)
        if not key:
            continue
        counter.filter("areaName =", key)
        entity = counter.get()
        parentProp = parentPropOf[prop]
        if entity is None:
            parentKey = getattr(details, str(parentProp), 'root')
            entity = Counter(areaName=key, parentKey=parentKey)

        if pSentiment == 'pos':
            entity.countPos += 1
        elif pSentiment == 'neg':
            entity.countNeg += 1
        entity.put()
    opinion.put()