情绪分析 Python 标记化

Sentiment analysis Python tokenization

我的问题如下:我想对意大利语推文进行情感分析,我会对我的意大利语文本进行标记化和词形还原,以便为我的论文找到新的分析维度。问题是我想标记我的主题标签,同时拆分组成的标签。例如,如果我有#nogreenpass,我也会没有# 符号,因为用文本的所有单词可以更好地理解该短语的情感。我怎么能这样做?我尝试使用 sapCy,但没有结果。我创建了一个功能来清理我的文本,但我无法按照我想要的方式使用主题标签。我正在使用此代码:

import re
import spacy
from spacy.tokenizer import Tokenizer

nlp = spacy.load('it_core_news_lg')

# Clean_text function
def clean_text(text):
    text = str(text).lower()
    doc = nlp(text)
    text = re.sub(r'#[a-z0-9]+', str(' '.join(t in nlp(doc))), str(text))
    text = re.sub(r'\n', ' ', str(text)) # Remove /n
    text = re.sub(r'@[A-Za-z0-9]+', '<user>', str(text)) # Remove and replace @mention
    text = re.sub(r'RT[\s]+', '', str(text)) # Remove RT
    text = re.sub(r'https?:\/\/\S+', '<url>', str(text)) # Remove and replace links
    return text

例如,我不知道如何添加第一个 < 和最后一个 > 来替换 # 符号,并且标记化过程无法正常工作。感谢您为我花费的时间和耐心。我希望在 Jupiter 分析和 python 编码方面变得更强,这样我也可以为您的问题提供帮助。谢谢大家!

您可以将当前的 clean_code 调整为

def clean_text(text):
    text = str(text).lower()
    text = re.sub(r'#(\w+)', r'<>', text)
    text = re.sub(r'\n', ' ', text) # Remove /n
    text = re.sub(r'@[A-Za-z0-9]+', '<user>', text) # Remove and replace @mention
    text = re.sub(r'RT\s+', '', text) # Remove RT
    text = re.sub(r'https?://\S+\b/?', '<url>', text) # Remove and replace links
    return text

参见Python demo online

下面一行代码:

print(clean_text("@Marcorossi hanno ragione I #novax htt"+"p://www.asfag.com/"))

将产生

<user> hanno ragione i <novax> <url>

请注意,没有简单的方法可以将粘合的字符串拆分为其组成词。请参阅 How to split text without spaces into list of words 了解如何做到这一点。