从列表中提取属性

发布于 2025-01-28 14:24:18 字数 454 浏览 3 评论 0原文

我是Python的新手,我对待从列表中提取情绪,并将每个属性放在其相应的系列中以训练机器学习算法 例如,列表中的一个实例,我想在新系列中放愤怒,其值= 0.013736263736263736等。

“{'愤怒':”, '0.013736263736263736,',, “'预期':”, '0.0027472527472527475,',, “'厌恶':”, '0.03296703296703297,',, “'害怕':”, '0.0027472527472527475,',, “'喜悦':”, '0.0,', “'消极的':”, '0.06043956043956044,',, “'积极的':”, '0.019230769230769232,',,, “'悲伤':”, '0.0027472527472527475,',, “'惊喜':”, '0.008241758241758242,',, “'相信':”, '0.019230769230769232}'] 提前致谢

I am a novice in python and I treat to extract emotions from list and put each attribute to its corresponding series to train machine learning algorithms
for example this one instance in list and I want put anger in new series with its value= 0.013736263736263736 and so on.

"{'anger':",
'0.013736263736263736,',
"'anticipation':",
'0.0027472527472527475,',
"'disgust':",
'0.03296703296703297,',
"'fear':",
'0.0027472527472527475,',
"'joy':",
'0.0,',
"'negative':",
'0.06043956043956044,',
"'positive':",
'0.019230769230769232,',
"'sadness':",
'0.0027472527472527475,',
"'surprise':",
'0.008241758241758242,',
"'trust':",
'0.019230769230769232}']
thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2025-02-04 14:24:18

您可以通过多种方式操纵字典和情感,让我显示一些:

1。将项目添加到集合,样本

def calc_sentiment(word: str):
    return 0.5

def Add_to_Collection(sentense: str):
    sentiment={}
    for word in str.split(sentense):
        sentiment[word]=calc_sentiment(word)
    return sentiment

test='My name is John Cache. It is my real name.'
print(Add_to_Collection(test))

您可以看到输出:

{'My': 0.5, 'name': 0.5, 'is': 0.5, 'John': 0.5, 'Cache.': 0.5, 'It': 0.5, 'my': 0.5, 'real': 0.5, 'name.': 0.5}

2。将项目添加到收集中,以情感,示例

def calc_sentiment(word: str):
    return 0.5

def Add_to_Collection_Sum(sentense: str):
    sentiment={}
    for word in str.split(sentense):
        sentiment[word] = sentiment[word] + calc_sentiment(word)  if word in sentiment else calc_sentiment(word)
    return sentiment

test='My name is John Cache. It is my real name.'
print(Add_to_Collection_Sum(test))

您可以看到输出(请参阅“值” 1.0有关'is'):

{'My': 0.5, 'name': 0.5, 'is': 1.0, 'John': 0.5, 'Cache.': 0.5, 'It': 0.5, 'my': 0.5, 'real': 0.5, 'name.': 0.5}

3。将项目添加到收集中,并使用所有情感列表,示例

def calc_sentiment(word: str):
    return 0.5

def Add_to_Collection_AddToList(sentense: str):
    sentiment={}
    items=[]
    for word in str.split(sentense):
        if word in sentiment:
            items=sentiment[word]
            items.append(calc_sentiment(word))
        else:
            sentiment[word]=[calc_sentiment(word)]
    return sentiment

test='My name is John Cache. It is my real name.'
print(Add_to_Collection_AddToList(test))

您可以看到输出(请参阅double值[0.5,0.5]有关'iss'):

{'My': [0.5], 'name': [0.5], 'is': [0.5, 0.5], 'John': [0.5], 'Cache.': [0.5], 'It': [0.5], 'my': [0.5], 'real': [0.5], 'name.': [0.5]}

You can manipulate with dictionary and sentiment in many ways, let me show a few:

1. Add item to collection, sample

def calc_sentiment(word: str):
    return 0.5

def Add_to_Collection(sentense: str):
    sentiment={}
    for word in str.split(sentense):
        sentiment[word]=calc_sentiment(word)
    return sentiment

test='My name is John Cache. It is my real name.'
print(Add_to_Collection(test))

you can see outputs:

{'My': 0.5, 'name': 0.5, 'is': 0.5, 'John': 0.5, 'Cache.': 0.5, 'It': 0.5, 'my': 0.5, 'real': 0.5, 'name.': 0.5}

2. Add item to collection with sum of sentiments, sample

def calc_sentiment(word: str):
    return 0.5

def Add_to_Collection_Sum(sentense: str):
    sentiment={}
    for word in str.split(sentense):
        sentiment[word] = sentiment[word] + calc_sentiment(word)  if word in sentiment else calc_sentiment(word)
    return sentiment

test='My name is John Cache. It is my real name.'
print(Add_to_Collection_Sum(test))

you can see outputs (see value 1.0 for 'is'):

{'My': 0.5, 'name': 0.5, 'is': 1.0, 'John': 0.5, 'Cache.': 0.5, 'It': 0.5, 'my': 0.5, 'real': 0.5, 'name.': 0.5}

3. Add item to collection with list of all sentiments, sample

def calc_sentiment(word: str):
    return 0.5

def Add_to_Collection_AddToList(sentense: str):
    sentiment={}
    items=[]
    for word in str.split(sentense):
        if word in sentiment:
            items=sentiment[word]
            items.append(calc_sentiment(word))
        else:
            sentiment[word]=[calc_sentiment(word)]
    return sentiment

test='My name is John Cache. It is my real name.'
print(Add_to_Collection_AddToList(test))

you can see outputs (see double values [0.5, 0.5] for 'is'):

{'My': [0.5], 'name': [0.5], 'is': [0.5, 0.5], 'John': [0.5], 'Cache.': [0.5], 'It': [0.5], 'my': [0.5], 'real': [0.5], 'name.': [0.5]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文