График частоты эмодзи и слов - PullRequest
0 голосов
/ 27 мая 2020

Я пытаюсь определить частоту эмодзи и слов. После импорта библиотек

import emoji
import regex

я использовал следующую функцию, чтобы подсчитать, сколько смайликов и слов в текстах.

def split_count(text):
    emoji_counter = 0
    data = regex.findall(r'\X', text)
    for word in data:
        if any(char in emoji.UNICODE_EMOJI for char in word):
            emoji_counter += 1
            # Remove from the given text the emojis
            text = text.replace(word, '') 

    words_counter = len(text.split())

    return emoji_counter, words_counter

Приведенный выше код был предложен в качестве ответа другому пользователю в этом сообществе. Поскольку у меня есть список строк, мне нужно было бы l oop через них:

sent=["I know it's possible to match a word and then reverse the matches using other tools (e.g. grep -v)?. However, is it possible to match lines that do not contain a specific word, e.g. hede, using a regular expression?","?I'm trying to iterate over the words of a string. The string can be assumed to be composed of words separated by whitespace?. Note that I'm not interested in C string functions or that kind of character manipulation/access?","I currently have a list of words within a text file, all the words within the document are on a separate line.?",...]

Я пробовал следующее:

for line in sent:
        counter = split_count(line)
        print("Emojis - {}, Words - {}".format(counter[0], counter[1]))

он работает довольно хорошо, но Я не умею отображать эти результаты (смайлики и слова) в двух отдельных диаграммах (гистограммах), где по оси Y - частота, а по оси X - метка для каждого текста (например, первого слова). Я надеюсь в вашу помощь и советы.

1 Ответ

1 голос
/ 27 мая 2020

Я построил простой график, используя функцию построения в 'pandas'. Данные создаются путем добавления их в DF из уже представленных переменных.

df = pd.DataFrame(index=[], columns=[])
for line in sent:
        counter = split_count(line)
        print("Emojis - {}, Words - {}".format(counter[0], counter[1]))
        tmp = pd.DataFrame({'Emoji_cnt':counter[0], 'Word_cnt':counter[1], 'First word':str(line[:15])}, index=[1])
        df = pd.concat([df, tmp], axis=0, ignore_index=True)

 df
    Emoji_cnt   Word_cnt    First word
0   1   39  I know it's pos
1   3   38  ?I'm trying to
2   1   22  I currently hav

fig = plt.figure( figsize=(8,6))
ax = fig.add_subplot(1, 1, 1)
df.plot(kind='bar', ax=ax, subplots=True, layout=(2,1), sharey=True, sharex=True)

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...