def start(self):
        location_service = LocationService()

        locations = location_service.get_locations_indonesia()

        for i, location in enumerate(locations):
            hotels = self.hotel_service.get_hotels_by_locationid(
                location['location_id'])

            for j, hotel in enumerate(hotels):
                print("[", self.now, "]", self.count, ")========== ",
                      hotel['name'])
                self.calculate_sentiment_score(hotel)
                self.count += 1

            #     break
            break
Exemplo n.º 2
0
    def start(self):
        datenow = datetime.datetime.now()
        reviewtranslate_service = ReviewTranslatedService()

        hotel_service = HotelService()
        review_service = ReviewService()
        location_service = LocationService()
        # locations = location_service.get_all_locations()
        locations = location_service.get_locations_indonesia()

        for i, location in enumerate(locations):
            hotels = hotel_service.get_hotels_by_locationid(
                location['location_id'])

            for j, hotel in enumerate(hotels):
                reviews = review_service.get_review_by_hotel_locationid(
                    hotel['location_id'])

                for r, review in enumerate(reviews):
                    text_to_translate = review['text']

                    try:
                        isexist_review = reviewtranslate_service.isexist_review_by_hotel_locid(
                            hotel['location_id'], review['id'])
                        # print(isexist_review)
                        if isexist_review.count() == 0:
                            print(
                                "[", datetime.datetime.now(), "] Review (",
                                review['id'],
                                ") on table Translated Review is not exist. Saving Review ..."
                            )

                            # gTranslator = Translator()
                            language_translator = LanguageTranslator()
                            text_translated = language_translator.translate_yandex(
                                text_to_translate)

                            data = {
                                "hotel": hotel,
                                "review": review,
                                "location_id": location['location_id'],
                                "hotel_id": hotel['location_id'],
                                "review_id": review['id'],
                                "text_to_translate": text_to_translate,
                                "text_translated": text_translated,
                                "created_at": datenow
                            }
                            if text_translated != None:
                                reviewtranslate_service.create(data)
                            else:
                                print(
                                    str("-----> Err : Failed to translate review !"
                                        ))
                        else:
                            print(
                                "[", datetime.datetime.now(), "] Review (",
                                review['id'],
                                ") on table Translated Review is already exist"
                            )

                    except Exception as err:
                        print(str("Err : ", err))
                        continue
    def start(self):
        datenow = datetime.now()

        sentimentreview_service = SentimentReviewService()
        sentiment_analyzer = SentimentAnalyzer()

        location_service = LocationService()
        hotel_service = HotelService()
        reviewtranslated_service = ReviewTranslatedService()
        review_service = ReviewService()

        language_translator = LanguageTranslator()

        locations = location_service.get_locations_indonesia()

        for i, location in enumerate(locations):
            hotels = hotel_service.get_hotels_by_locationid(
                location['location_id'])

            for j, hotel in enumerate(hotels):
                reviews_translated = reviewtranslated_service.get_review_by_hotel_locid(
                    hotel['location_id'])
                sentimentreviews_on_hotel = sentimentreview_service.get_review_by_hotel_locid(
                    hotel['location_id'])

                print(reviews_translated.count())

                if reviews_translated.count() > 0:
                    print("[", datetime.now(), "] Data translated")
                    for r, review_translated in enumerate(reviews_translated):
                        text_to_sentiment = review_translated[
                            'text_translated']

                        try:
                            isexist_review = any(
                                x['review_id'] ==
                                review_translated['review_id']
                                for x in sentimentreviews_on_hotel)
                            if not isexist_review:
                                vader = sentiment_analyzer.get_vader(
                                    text_to_sentiment)
                                wordnet = sentiment_analyzer.get_sentiwordnet(
                                    text_to_sentiment)

                                subratings = self.map_subratings(
                                    review_translated['review'])
                                subratings_normalized = self.normalize_subratings(
                                    subratings)

                                date_publish = dateutil.parser.parse(
                                    review_translated['review']
                                    ['published_date'])

                                data = {
                                    "hotel":
                                    hotel,
                                    "review_translated":
                                    review_translated,
                                    "publish_date":
                                    review_translated['review']
                                    ['published_date'],
                                    "month":
                                    date_publish.month,
                                    "year":
                                    date_publish.year,
                                    "location_id":
                                    location['location_id'],
                                    "hotel_id":
                                    hotel['location_id'],
                                    "review_id":
                                    review_translated['review_id'],
                                    "subratings":
                                    subratings,
                                    "subratings_normalized":
                                    subratings_normalized,
                                    "text_review":
                                    review['text'],
                                    "text_to_sentiment":
                                    text_to_sentiment,
                                    "vader_sentiment":
                                    vader,
                                    "wordnet_sentiment":
                                    wordnet,
                                    "wordnet_normalized": (wordnet - (-1)) / 2,
                                    "created_at":
                                    datenow
                                }
                                # pprint.pprint(data)

                                sentimentreview_service.create(data)
                            else:
                                print(
                                    "[", datetime.now(), "] Review (",
                                    review_translated['review_id'],
                                    ") on table Sentiment Review is already exist"
                                )

                        except Exception as err:
                            print(str("[", datetime.now(), "]  Err : ", err))
                            continue
                else:
                    print("[", datetime.now(), "] No translated data")
                    reviews = review_service.get_review_by_hotel_locationid(
                        hotel['location_id'])

                    for r, review in enumerate(reviews):

                        try:
                            isexist_review = any(
                                x['review_id'] == review['id']
                                for x in sentimentreviews_on_hotel)

                            if not isexist_review:
                                subratings = self.map_subratings(review)
                                subratings_normalized = self.normalize_subratings(
                                    subratings)

                                date_publish = dateutil.parser.parse(
                                    review['published_date'])

                                data = {
                                    "hotel": hotel,
                                    "review": review,
                                    "publish_date": review['published_date'],
                                    "month": date_publish.month,
                                    "year": date_publish.year,
                                    "location_id": location['location_id'],
                                    "hotel_id": hotel['location_id'],
                                    "review_id": review['id'],
                                    "subratings": subratings,
                                    "subratings_normalized":
                                    subratings_normalized,
                                    "text_to_sentiment": "",
                                    "vader_sentiment": {
                                        'neg': 0,
                                        'pos': 0,
                                        'neu': 0,
                                        'compound': 0.5
                                    },
                                    "wordnet_sentiment": 0,
                                    "wordnet_normalized": 0.5,
                                    "created_at": datenow
                                }
                                # pprint.pprint(data)

                                sentimentreview_service.create(data)
                            else:
                                print(
                                    "[", datetime.now(), "] Review (",
                                    review['id'],
                                    ") on table Sentiment Review is already exist"
                                )

                        except Exception as err:
                            print(str("[", datetime.now(), "] Err : ", err))
                            continue