def run_sentiment_analysis(with_graphs=False):
    conn = db_conn(db_config())
    sql = 'SELECT * FROM reviews'
    df = db_table(conn, sql)
    df['lang'] = df.apply(lambda x: detect_language(x['comments']), axis=1)
    df_english = df.loc[df['lang'] == 'english']
    df_scores = get_sentiment_scores(df_english)

    if with_graphs:
        plot_score_histograms(df_scores['positive'],
                              score_type='Positive',
                              filename='pos_sentiment.png')
        plot_score_histograms(df_scores['neutral'],
                              score_type='Neutral',
                              filename='neu_sentiment.png')
        plot_score_histograms(df_scores['negative'],
                              score_type='Negative',
                              filename='neg_sentiment.png')
        plot_score_histograms(df_scores['compound'],
                              score_type='Compound',
                              filename='compound_sentiment.png')

    df_avg = sentiment_by_listing(df_scores)
    dtypes = {'listing_id': INTEGER,
              'compound': FLOAT,
              'positive': FLOAT,
              'neutral': FLOAT,
              'negative': FLOAT}

    write_to_db(conn, df_avg, name='listings_sentiment', dtypes=dtypes)
Beispiel #2
0
def detectLanguage():
    """ REST API call to detect language of text """
    query_parameters = request.args
    text = query_parameters.get('text')
    if not text:
        return ""
    result = detect_language(text)
    return result
Beispiel #3
0
def Enter_pressed():

    input_get = You.get()
    input_test = input_get.lower()
    if (translate.detect_language(input_test) != "en"):
        input_test = translate.translate_text("EN", input_test)
    if input_test in bye_synonyms:
        quit()
    else:
        messages.insert(END, "You: " + '%s\n' % input_get)
        # this gets the response from the Processor.py file and then prints them out
        get_response = Processor.process(input_get, question_list, responses)
        messages.insert(INSERT, "Nova: " + '%s\n' % get_response)
        You.set('')
        messages.see(END)
        return "break"
def process(sentence, doc_2,
            answer):  # Processes user input and outputs the correct response
    invalid_responses = [
        "I do not understand the question",
        "That question is not in my database", "I cannot answer "
        "that question", "I am not familiar with that question",
        "I am sorry. Could you please ask another question?"
    ]
    similarity_index = 0
    index = 0
    nlp = en_core_web_lg.load()
    foreign_indicator = 0
    #This is where input is translated into English
    detected_lang = translate.detect_language(sentence)
    if (detected_lang != "en"):
        sentence = translate.translate_text("EN", sentence)
        foreign_indicator = 1

    doc_1 = nlp(preprocess(sentence))
    similarity = 0

    for i in range(len(doc_2)):
        if doc_2[i].vector_norm and doc_1.vector_norm:
            similarity = doc_1.similarity(doc_2[i])

        if similarity > similarity_index:
            similarity_index = similarity
            index = i
    if similarity_index > 0.60:
        if (foreign_indicator == 1):
            return translate.translate_text(detected_lang, answer[index])
        else:
            return answer[index]

    else:
        if (foreign_indicator == 1):
            return (translate.translate_text(
                detected_lang, random.choice(invalid_responses)) + "\n" +
                    wikipedia.search(sentence))
        else:
            return (random.choice(invalid_responses) + "\n" +
                    wikipedia.search(sentence))
Beispiel #5
0
 def getResponse(self):
     # Get user message
     userMessage = self.userInput.get()
     userMessageInEng = userMessage
     #translate the user message into english if its not in english
     detected_language = translate.detect_language(userMessage)
     if detected_language != 'en':
         userMessageInEng = translate.translate_to_English(userMessage)
     # Ignore empty messages
     if userMessageInEng == "":
         return
     # Clear the user input
     self.userInput.delete(0, "end")
     # Get our reply
     reply = "BakerAI: " + m.getFinalOutput(
         self.loaded_clf, userMessageInEng,
         detected_language['language']) + "\n"
     # Send reply to client
     storedUserMessage = "You: " + userMessage + "\n"
     self.outputBox.configure(state="normal")
     self.outputBox.insert(tk.END, storedUserMessage)
     self.outputBox.insert(tk.END, reply)
     self.outputBox.configure(state="disabled")
Beispiel #6
0
async def on_message(message):
    global player
    global active
    global music_list

    if message.author == client.user:
        return

    u = message.author.id

    # cmd
    if message.content.startswith("!!"):
        u = message.author.id
        _cmd = message.content[2::]
        c = re.split(r"\s", _cmd)
        cmd = c[0].lower()
        arg = c[1::] if len(c) >= 2 else []
        cvc = client.voice_client_in(message.server)
        channel = message.channel

        # master commands, ignore active state
        if u == ADMIN_USERID or u == SUB_ADMIN_USERID:
            if cmd == "view_log":
                log_str = build_debug_log()
                await client.send_message(
                    find_text_channel(option.get('debug_log_channel'))["obj"],
                    '```' + log_str + '```')

            if cmd == "change_active":
                if active:
                    active = False
                    DEBUG("bot deactivated.")
                    await deactivate_bot()
                    await client.send_message(
                        find_text_channel(
                            option.get('debug_log_channel'))["obj"],
                        "[%s] bot機能を OFF にしましたよ。" % time_str())
                else:
                    active = True
                    DEBUG("bot activated.")
                    await client.send_message(
                        find_text_channel(
                            option.get('debug_log_channel'))["obj"],
                        "[%s] bot機能を ON にしましたよ。" % time_str())

        if cmd == "status":
            if active:
                await client.send_message(message.channel,
                                          "現在 bot機能は ON になっていますよ。")
            else:
                await client.send_message(message.channel,
                                          "現在 bot機能は OFF になっていますよ。")

        if cmd == "option":
            for a in arg:
                key = None
                val = None
                try:
                    key, val = a.split('=')
                    option.set(key, val)
                except:
                    option.set(key, None)

                await client.send_message(
                    find_text_channel(option.get('debug_log_channel'))["obj"],
                    "Set option %s = %s" % (key, val))

        # need active state commands
        if not active:
            return

        if cmd == "join_only":
            await kot_player.join(arg, cvc, False)

        elif cmd == "join":
            await kot_player.join(arg, cvc, True)

        elif cmd == "madd":
            await kot_player.add(channel, arg, cvc)

        elif cmd == "maddqueue":
            await kot_player.add(channel, arg, cvc, True)

        elif cmd == "maddtop":
            await kot_player.add(channel, arg, cvc, False, True)

        elif cmd == "mnow":
            await kot_player.now(channel)

        elif cmd == "mplay":
            await kot_player.play(client.voice_client_in(message.server))
            return

        elif cmd == "mpause":
            kot_player.pause()
            return

        elif cmd == "mresume":
            kot_player.resume()
            return

        elif cmd == "mskip":
            kot_player.skip()

        elif cmd == "mqclear":
            await kot_player.mqclear(channel, arg)

        elif cmd == "view_list":
            await kot_player.view_list()

        elif cmd == "mqstate":
            await kot_player.mqstate(channel)

        elif cmd == "leave":
            await kot_player.leave()

        elif cmd == "update_list":
            kot_player.update_list()
            await client.send_message(
                find_text_channel(
                    option.get('default_bot_text_channel'))["obj"],
                "楽曲リストを更新しましたよ。")

        # lang translation
        elif cmd == "translate":
            tocode = None
            fromcode = None
            f = []
            for s in arg:
                if s.startswith("code="):
                    dummy, tocode = s.split("=")
                    if not translate.isLangCode(tocode):
                        tocode = None
                elif s.startswith("from="):
                    dummy, fromcode = s.split("=")
                    if not translate.isLangCode(fromcode):
                        fromcode = None
                else:
                    f.append(s)
            if f == []:
                return
            fromstr = " ".join(f)

            if fromcode == None:
                fromcode = translate.detect_language(fromstr)

            if tocode == fromcode:
                return

            if not translate.isLangCode(tocode):
                tocode = None

            if tocode == None:
                if fromcode == "ja":
                    tocode = "ko"
                else:
                    tocode = "ja"

            trans = translate.translateJAKOWithPAPAGO(fromstr, tocode,
                                                      fromcode)
            if "error" in trans:
                DEBUG("Error occured, duaring translation.")
                await client.send_message(
                    message.channel,
                    "<@%s> うまく翻訳できませんでした。" % message.author.id)

            else:
                DEBUG("Translate %s -> %s" % (fromcode, tocode))
                await client.send_message(
                    message.channel,
                    "<@%s> %s から %s への翻訳ですよ。\n原文: %s\n翻訳: %s" %
                    (message.author.id, translate.code2lang(fromcode),
                     translate.code2lang(tocode), fromstr, trans["text"]))

        elif cmd == "help":
            try:
                with codecs.open("help.txt", "r") as fp:
                    s = fp.read()
                    await client.send_message(message.channel,
                                              "```" + s + "```")
            except:
                pass

        elif cmd == "role":
            if len(arg) < 1:
                return

            add_roles = []
            rem_roles = []
            add_roles_name = []
            rem_roles_name = []
            for a in arg:
                rs = re.search(r"(\+|\-)(.*)", a)
                if not rs:
                    continue

                rg = rs.groups()
                role_name = rg[1]

                role = find_idol_role(role_name)
                if role == None:
                    continue

                if rg[0] == "+":
                    await client.add_roles(message.author, role)
                    add_roles_name.append(role.name)
                elif rg[0] == "-":
                    await client.remove_roles(message.author, role)
                    rem_roles_name.append(role.name)

            if len(add_roles_name) > 0 or len(rem_roles_name) > 0:
                await client.send_message(
                    message.channel, "役職を適用しました。\n追加した役職:%s\n外した役職:%s\n" %
                    (', '.join(add_roles_name), ', '.join(rem_roles_name)))

        elif cmd == "purge_role":
            if len(message.author.roles) > 0:
                for r in message.author.roles:
                    await client.remove_roles(message.author, r)
                await client.send_message(message.channel, "役職をすべて削除しました。")

        elif cmd == "search":
            if len(arg) < 1:
                return

            res = search_idols(arg)
            if len(res) == 0:
                await client.send_message(
                    message.channel,
                    "\"%s\" で検索しましたが、該当するアイドルがいませんでした。" % " ".join(arg))

            else:
                s = ""
                for d in res:
                    s += "```" + build_stat_str(d) + "```\n"
                await client.send_message(
                    message.channel, "\"%s\" での検索で %d人のアイドルが見つかりました。\n%s" %
                    (" ".join(arg), len(res), s))

        elif cmd == "reload":
            await reload()