Exemple #1
0
class Sentiment:
    def __init__(self, modelfile = ''):
        self.Review = Review(modelfile)

    def predict(self,Review, content):
        print "content:",content
        dic_content = json.loads(content)
        s = dic_content['body']
        bodyList = re.split(u';|;|,|,|。|\?|?|!|!', s)
        bodyList = [i for i in bodyList if i != ""]
        f_sentiment = []
        for i in bodyList:
            print i
            f_sentiment.append(self.Review.predictPraise(i.strip()))
        print "f_sentiment:", f_sentiment
        length = len(bodyList)
        f_position = []
        for i in range(length):
            cal = pow(float(i-length/2), 2)/pow(length/2, 2)
            f_position.append(cal)
        print "f_position:", f_position
        #f_keyword = findKeyword(bodyList)
        f = []
        for i in range(length):
            f.append(f_sentiment[i] * 0.6 + f_position[i] * 0.4)
        #f = [i*0.6 for i in f_sentiment] + [i*0.4 for i in f_position]
        print "f:", f
        return max(f)
Exemple #2
0
class PythonServiceServer:
     def __init__(self):
         self.Review = Review(reviewModel)
         self.Sentiment = Sentiment(reviewModel)

     #return positive(1), negative(-1) or neutral(0) sentiment
     def getSentiment(self, content):
        func = "getSentiment"
        try:
            res = self.Sentiment.predict(self.Review, str(content.strip()))
            print res
        except Exception as e:
            logging.error("Error: host: %s \t func: %s \t content: %s \t ",self.host, func, content)
            res = 0
        #logging.info("host: %s \t func: %s \t content: %s \t result: %s ",self.host, func, content, res)
        return res

     #return the degree various from 0 to 1 about bad review to good review
     def getReview(self, content):
        func = "getReview"
        try:
            content = "".join(content.split("\n"))
            res = self.Review.predictPraise(str(content.strip()))
        except Exception as e:
            logging.error("Error: host: %s \t func: %s \t content: %s \t ",self.host, func, content)
            res = 0
        #logging.info("host: %s \t func: %s \t content: %s \t result: %s ",self.host, func, content, res)
        return res
Exemple #3
0
 def __init__(self, modelfile = ''):
     self.Review = Review(modelfile)
Exemple #4
0
 def __init__(self):
     self.Review = Review(reviewModel)
     self.Sentiment = Sentiment(reviewModel)