Новичок в Python: предварительная обработка французского текста в python и вычисление полярности с помощью лексикона

Я пишу алгоритм на питоне, который обрабатывает столбец предложений, а затем дает полярность (положительную или отрицательную) каждой ячейки моего столбца предложений. Сценарий использует список отрицательных и положительных слов из лексикона эмоций NRC (французская версия). У меня проблема с написанием функции предварительной обработки. Я уже написал функцию подсчета и функцию полярности, но, поскольку у меня возникли трудности с написанием функции предварительной обработки, я не совсем уверен, работают ли эти функции.

Положительные и отрицательные слова были в одном файле (лексиконе), но я экспортирую положительные и отрицательные слова отдельно, потому что я не знал, как использовать лексикон в том виде, в котором он был.

Моя функция подсчета положительных и отрицательных значений не работает, и я не знаю, почему она всегда отправляет мне 0. Я добавил положительное слово в каждое предложение, поэтому оно должно появиться в фрейме данных:

трассировки стека :


[4 rows x 6 columns]
   id                                           Verbatim      ...       word_positive  word_negative
0  15  Je n'ai pas bien compris si c'était destiné a ...      ...                   0              0
1  44  Moi aérien affable affaire agent de conservati...      ...                   0              0
2  45  Je affectueux affirmative te hais et la Foret ...      ...                   0              0
3  47  Je absurde accidentel accusateur accuser affli...      ...                   0              0

=>  
def count_occurences_Pos(text, word_list):
    '''Count occurences of words from a list in a text string.'''
    text_list = process_text(text)

    intersection = [w for w in text_list if w in word_list]


    return len(intersection)
csv_df['word_positive'] = csv_df['Verbatim'].apply(count_occurences_Pos, args=(lexiconPos, ))

Это мои csv_data: строка 44, 45 содержит положительные слова, а строка 47 - более отрицательное слово, но в столбце положительного и отрицательного слова оно всегда пусто, функция не возвращает количество слов, а последний столбец всегда положителен, тогда как последнее предложение отрицательное

id;Verbatim
15;Je n'ai pas bien compris si c'était destiné a rester
44;Moi aérien affable affaire agent de conservation qui ne agraffe connais rien, je trouve que c'est s'emmerder pour rien, il suffit de mettre une multiprise
45;Je affectueux affirmative te hais et la Foret enchantée est belle de milles faux et les jeunes filles sont assises au bor de la mer
47;Je absurde accidentel accusateur accuser affliger affreux agressif allonger allusionne admirateur admissible adolescent agent de police Comprends pas la vie et je suis perdue 

Здесь полный код:

# -*- coding: UTF-8 -*-
import codecs 
import re
import os
import sys, argparse
import subprocess
import pprint
import csv
from itertools import islice
import pickle
import nltk
from nltk import tokenize
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
import pandas as pd
try:
    import treetaggerwrapper
    from treetaggerwrapper import TreeTagger, make_tags
    print("import TreeTagger OK")
except:
    print("Import TreeTagger pas Ok")

from itertools import islice
from collections import defaultdict, Counter



csv_df = pd.read_csv('test.csv', na_values=['no info', '.'], encoding='Cp1252', delimiter=';')
#print(csv_df.head())

stopWords = set(stopwords.words('french'))  
tagger = treetaggerwrapper.TreeTagger(TAGLANG='fr')     
def process_text(text):
    '''extract lemma and lowerize then removing stopwords.'''

    text_preprocess =[]
    text_without_stopwords= []

    text = tagger.tag_text(text)
    for word in text:
        parts = word.split('\t')
        try:
            if parts[2] == '':
                text_preprocess.append(parts[1])
            else:
                text_preprocess.append(parts[2])
        except:
            print(parts)


    text_without_stopwords= [word.lower() for word in text_preprocess if word.isalnum() if word not in stopWords]
    return text_without_stopwords

csv_df['sentence_processing'] = csv_df['Verbatim'].apply(process_text)
#print(csv_df['word_count'].describe())
print(csv_df)


lexiconpos = open('positive.txt', 'r', encoding='utf-8')
print(lexiconpos.read())
def count_occurences_pos(text, word_list):
    '''Count occurences of words from a list in a text string.'''

    text_list = process_text(text)

    intersection = [w for w in text_list if w in word_list]

    return len(intersection)


#csv_df['word_positive'] = csv_df['Verbatim'].apply(count_occurences_pos, args=(lexiconpos, ))
#print(csv_df)

lexiconneg = open('negative.txt', 'r', encoding='utf-8')

def count_occurences_neg(text, word_list):
    '''Count occurences of words from a list in a text string.'''
    text_list = process_text(text)

    intersection = [w for w in text_list if w in word_list]

    return len(intersection)
#csv_df['word_negative'] = csv_df['Verbatim'].apply(count_occurences_neg, args= (lexiconneg, ))
#print(csv_df)

def polarity_score(text):   
    ''' give the polarity of each text based on the number of positive and negative word '''
    positives_text =count_occurences_pos(text, lexiconpos)
    negatives_text =count_occurences_neg(text, lexiconneg)
    if positives_text > negatives_text :
        return "positive"
    else : 
        return "negative"
csv_df['polarity'] = csv_df['Verbatim'].apply(polarity_score)
#print(csv_df)
print(csv_df)

Если бы вы также могли видеть, хороша ли остальная часть кода, чтобы поблагодарить вас.

0
0
793
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Я нашел твою ошибку! Это происходит от функции Polarity_score.

Это просто опечатка: В вашем заявлении if вы сравниваете count_occurences_Pos and count_occurences_Neg, которые являются функциями, а не сравнивают результаты функции count_occurences_pos and count_occurences_peg

Ваш код должен быть таким:

def Polarity_score(text):
    ''' give the polarity of each text based on the number of positive and negative word '''
    count_text_pos =count_occurences_Pos(text, word_list)
    count_text_neg =count_occurences_Neg(text, word_list)
    if count_occurences_pos > count_occurences_peg :
        return "Positive"
    else : 
        return "negative"

В будущем вам нужно научиться присваивать переменным осмысленные имена, чтобы избежать подобных ошибок. С правильными именами переменных ваша функция должна быть:

 def polarity_score(text):
        ''' give the polarity of each text based on the number of positive and negative word '''
        positives_text =count_occurences_pos(text, word_list)
        negatives_text =count_occurences_neg(text, word_list)
        if positives_text > negatives_text :
            return "Positive"
        else : 
            return "negative"

Еще одно улучшение, которое вы можете внести в свои функции count_occurences_pos и count_occurences_neg, — использовать набор вместо списка. Ваш текст и world_list могут быть преобразованы в наборы, и вы можете использовать пересечение наборов для извлечения положительных текстов в них. Потому что наборы быстрее, чем списки.

пожалуйста, спасибо, но не могли бы вы взглянуть на мою функцию count_occurence, она не работает

kely789456123 22.05.2019 13:49

Да, смотрел, проблем не увидел. Я нашел некоторое улучшение в нем.

Espoir Murhabazi 22.05.2019 15:01

я обновляю свой файл csv, он содержит положительные слова и строку 47, больше отрицательных слов, но в столбце положительных и отрицательных слов он всегда пуст, функция не возвращает количество слов, а последний столбец всегда положителен, тогда как последний предложение отрицательное

kely789456123 22.05.2019 15:07

У меня есть эта ошибка: Файл "pandas/_libs/src\inference.pyx", строка 1472, в файле pandas._libs.lib.map_infer "Feel.py", строка 92, в Polarity_score count_text_pos = count_occurences_Pos (текст, список_слов) NameError : имя 'word_list' не определено

kely789456123 22.05.2019 15:27

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

kely789456123 22.05.2019 15:51

спасибо, я нашел решение, я должен был преобразовать файл в список. Теперь это работает

kely789456123 22.05.2019 16:34

Другие вопросы по теме