Analyse text by sentiment analysis & language detection :return: Boolean indicating if the text is appropriate (after analysis) or None (if analysis isn't enabled)
(text, text_type, logger)
| 31 | |
| 32 | |
| 33 | def text_analysis(text, text_type, logger): |
| 34 | """ |
| 35 | Analyse text by sentiment analysis & language detection |
| 36 | |
| 37 | :return: |
| 38 | Boolean indicating if the text is appropriate (after analysis) or |
| 39 | None (if analysis isn't enabled) |
| 40 | """ |
| 41 | |
| 42 | # convert emojis in text into plain words for better analysis |
| 43 | text, emojiless_text = deform_emojis(text) |
| 44 | text_type_c = text_type.capitalize() |
| 45 | inap_msg = "--> Content is inappropriate!" # generic negative result |
| 46 | # message |
| 47 | language_of_text = None |
| 48 | text_is_printed = None |
| 49 | |
| 50 | if ( |
| 51 | not YANDEX_CONFIG |
| 52 | or YANDEX_CONFIG["enabled"] is not True |
| 53 | or ( |
| 54 | YANDEX_CONFIG["match_language"] is not True |
| 55 | and (not MEANINGCLOUD_CONFIG or MEANINGCLOUD_CONFIG["enabled"] is not True) |
| 56 | ) |
| 57 | ): |
| 58 | |
| 59 | # No analysis will be held |
| 60 | print("") |
| 61 | logger.info('{} text: "{}"'.format(text_type_c, text.encode("utf-8"))) |
| 62 | return None |
| 63 | |
| 64 | if YANDEX_CONFIG["match_language"] is True: |
| 65 | # Language detection & match will take place |
| 66 | if has_any_letters(emojiless_text): |
| 67 | language_of_text = detect_language(emojiless_text) |
| 68 | else: |
| 69 | # text contains only emojis |
| 70 | language_of_text = "en" |
| 71 | |
| 72 | # output the text to be analysed |
| 73 | print("") |
| 74 | logger.info( |
| 75 | "{} text ['{}']: \"{}\"".format( |
| 76 | text_type_c, language_of_text, text.encode("utf-8") |
| 77 | ) |
| 78 | ) |
| 79 | text_is_printed = True |
| 80 | |
| 81 | if language_of_text and YANDEX_CONFIG["language_code"] != language_of_text: |
| 82 | logger.info( |
| 83 | "{}\t~language of the text is '{}'".format(inap_msg, language_of_text) |
| 84 | ) |
| 85 | return False |
| 86 | |
| 87 | elif not language_of_text: |
| 88 | logger.info("{}\t~language of text couldn't be detected!".format(inap_msg)) |
| 89 | return False |
| 90 |
no test coverage detected