【问题标题】:Sentiment analysis - Pattern NLP情绪分析 - 模式 NLP
【发布时间】:2016-09-27 12:25:42
【问题描述】:

尝试使用模式创建主题对象极性图。

from pattern.en import parse,sentiment
print sentiment('The movie attempts to be surreal by incorporating various time paradoxes')

(0.125, 0.75)

from pattern.en import sentiment
print sentiment("He is good.") 

(0.7, 0.6000000000000001)

from pattern.en import sentiment
print sentiment("The movie attempts to be surreal by incorporating various time paradoxes. He is good.") 

(0.31666666666666665, 0.7000000000000001)

据我了解,分析会计算两个句子的极性并返回归一化值。是否可以逐行计算分数并返回,像这样

from pattern.en import sentiment
print sentiment("The movie attempts to be surreal by incorporating various time paradoxes. He is good.") 

(0.125, 0.75)
(0.7, 0.6000000000000001)

第二部分:我希望使用 numpy 和 amtplotlib 将这一系列 x1,y1 值映射到散点图。有可能吗?

以您的代码为指导,我尝试通过添加模态值来改进现有代码。但我面对

编辑 1

for sentence in sentences:
        modality(sentence)
    #mind the difference for the last sentence, which contains two dots.         
    for sentence in complete_text.split("."):
        modality(sentence)
    b = np.array([ modality(sentence) for sentence in complete_text.split(".") ])
    print "Modality: ", b[:,0]

输出错误

print "Modality: ", b[:,0]
IndexError: too many indices for array

我正在尝试根据在硬编码值时能够实现的模态范围更改标记符号。尝试将您的方法扩展到许多句子的模态。

编辑 2

图表看起来不错,但其中一项重要功能不存在。我需要单击标记点并希望返回单击所针对的特定句子,以便分析选择的特定句子。缩小到 onclick(事件)以返回句子。

fig = plt.figure()
ax = fig.add_subplot(111)
def onclick(event):
    print('button=%d,' %(event.button))
cid = fig.canvas.mpl_connect('button_press_event', onclick)

不确定如何连接特定标记的调用语句?这完成了我尝试极性分析的最后一个缺失部分。

编辑 3

我非常喜欢用红色表示消极情绪,用绿色表示积极情绪,用标记表示四种形式。我使用以下功能修改了您的代码以满足需要

print "polarities: ", a[:,0]
print "subjectivities: ", a[:,1]
print "modalities: ", a[:,2]
s = np.array(a[:,2])
r = np.array(a[:,1])
############ Plotting ############
def markers(s):
    if s > "0.5" and s< "1":
        return 'o'
    elif s > "0" or s < ".5":
        return 'x'
    elif s > "-.5" or s < "0":
        return 'v'
    else:
        return '^'

def colors(r):
    if r > "0" and r < "1":
        return "g"
    elif r < "0" or r > "-1":
        return "r"
    else:
        return "r"

fig=plt.figure()
ax=fig.add_subplot(111)
ax.scatter(a[:,0], a[:,1], marker = markers(s), color= colors(r), s=100, picker=5)

但是对于所有变体,图表都会返回红色的 x 标记。不知道为什么?

编辑 4:

ax=fig.add_subplot(111)
ax.scatter (p[(p>0.0)&(p<=1)&(m>0.5)&(m<=1)], s[(p>0.0)&(p<=1)&(m>0.5)&(m<=1)], marker = "o", color= 'g', s=100, picker=5)
ax.scatter (p[(p>0.0)&(p<=1)&(m>0.0)&(m<=0.5)], s[(p>0.0)&(p<=1)&(m>0.0)&(m<=0.5)], marker = "v", color= 'g', s=100, picker=5)


ax.scatter (p[(p>0.0)&(p<=1)&(m>-0.5)&(m<=0.0)], s[(p>0.0)&(p<=1)&(m>-0.5)&(m<=0.0)], marker = "s", color= 'g', s=100, picker=5)
ax.scatter (p[(p>0.0)&(p<=1)&(m>=-1.0)&(m<=-0.5)], s[(p>0.0)&(p<=1)&(m>=-1.0)&(m<=-0.5)], marker = "x", color= 'g', s=100, picker=5)
ax.scatter (p[(p>=-1.0)&(p<=0)&(m>0.5)&(m<=1)], s[(p>=-1.0)&(p<=0)&(m>0.5)&(m<=1)], marker = "o", color= 'r', s=100, picker=5)
ax.scatter (p[(p>=-1.0)&(p<=0)&(m>0.0)&(m<=0.5)], s[(p>=-1.0)&(p<=0)&(m>0.0)&(m<=0.5)], marker = "v", color= 'r', s=100, picker=5)
ax.scatter (p[(p>=-1.0)&(p<=0)&(m>-0.5)&(m<=0.0)], s[(p>=-1.0)&(p<=0)&(m>-0.5)&(m<=0.0)], marker = "s", color= 'r', s=100, picker=5)
ax.scatter (p[(p>=-1.0)&(p<=0)&(m>=-1.0)&(m<=-0.5)], s[(p>=-1.0)&(p<=0)&(m>=-1.0)&(m<=-0.5)], marker = "x", color= 'r', s=100, picker=5)

ax.set_xlabel("polarity")
ax.set_ylabel("subjectivity")
def onpick(event):
    index = event.ind
    for i in index:
        print i, sentences[i]
cid = fig.canvas.mpl_connect('pick_event', onpick)
plt.show()

不确定为什么 def on pick 没有给出相关的句子,但总是在第一句话。

【问题讨论】:

  • 您不应该通过删除问题的主要部分来编辑问题。其他人现在应该如何理解这个问题?
  • 试图合并问题以使其简洁。采取的建议。幸运地找到了丢失的部分。
  • 整合会有所帮助,是的。但是你不应该破坏编辑的结构,否则没有人可以跟随。现在,请参阅我的回答中的编辑 4。

标签: matplotlib nlp nltk


【解决方案1】:

这是对初始问题及其 Edit2 的回答。 Edit4 的答案见底部。

我正在添加另一个答案来解决 Edit2 中提出的问题。 您没有说“返回句子”是什么意思,所以我不得不猜测您希望将其打印到控制台。这是可以做到这一点的代码

from pattern.en import sentiment, modality

sentences0 = ["In fact, I'm not convinced that blue is a color.", 
             "The car is blue.",
             "Precisely speaking, no random opinion is allowed.",
             "Democracy is dead. Long live the king."]

complete_text = " ".join(sentences0)
sentences = complete_text.split(".")[:-1]

import numpy as np
import matplotlib.pyplot as plt

a = np.array([ sentiment(sentence) for sentence in sentences ])
b = np.array([ modality(sentence) for sentence in sentences  ])

a = np.append(a, np.array([b]).T, axis=1)


print "polarities: ", a[:,0]
print "subjectivities: ", a[:,1]
print "modalities: ", a[:,2]


############ Plotting ############
def colors(x):
    return [(1-xi,0., xi) for xi in x]

fig=plt.figure()
ax=fig.add_subplot(111)
ax.scatter(a[:,0], a[:,1], marker="s", color=colors(a[:,2]), s=100, picker=5)
ax.set_xlabel("polarity")
ax.set_ylabel("subjectivity")
def onpick(event):
    index = event.ind
    for i in index:
        print i, sentences[i]

cid = fig.canvas.mpl_connect('pick_event', onpick)
plt.show()

编辑4

问题是返回的索引是条件数组的索引,而sentences 没有条件。 这是一个希望能做你想做的事情的程序。

import numpy as np
import matplotlib.pyplot as plt

sentences = ["Sentence0", "Sentence1", "Sentence2", "Sentence3", "Sentence4", "Sentence5"]
p = np.array( [ 0. ,  0.2 ,  -0.3 ,  0.2, 0., 0.2] )
s = np.array( [ 0.1,  0.,   0.,   0.3 , 0.1, 0.] )
m = np.array( [ 1.,   -0.25,  1. ,  -0.6, 0.2,-0.25   ] )


colors = np.array([(0.8*(1-x), 0.7*x, 0) for x in np.ceil(p)])

cond = [(m>0.5)&(m<=1), (m>0.0)&(m<=0.5), (m>-0.5)&(m<=0.0), (m>=-1.0)&(m<=-0.5) ]
markers = ["o", "v", "s", "x"]


fig=plt.figure()
ax=fig.add_subplot(111)

sc=[]
for i in range(len(cond)):
    sc0 = ax.scatter(p[cond[i]], s[cond[i]], marker = markers[i], color= colors[cond[i]], s=100, picker=5)
    sc.append(sc0)

ax.set_xlabel("polarity")
ax.set_ylabel("subjectivity")

def onpick(event):
    index = event.ind
    artist = event.artist
    print len(index)
    for i in index:
        try:
            which = sc.index(artist)
            print i, sentences[int(np.arange(len(p))[cond[which]][i])]
        except:
            #raise
            print "no sentence found"

cid = fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

【讨论】:

  • 感谢确实做了它必须做的,但是我在edit3中提到的一个小改动。
  • 我认为我们现在已经超出了问题的范围。您在 Edit3 中遇到的问题是您将一个苹果与一堆橙子进行比较,反之亦然。你可能想看看这个:link
  • 我现在明白这个问题了。
  • 回复编辑 4 解决了整个问题。感谢您的努力。
【解决方案2】:

这是对最初问题的回答。

在这种情况下是什么定义了您的生产线? 如果我们可以假设点分隔句子,那么我们可以使用它将文本text.split(".") 拆分为一个列表。然后可以通过

计算每个项目的情感值
for sentence in complete_text.split("."):
    print sentiment(sentence)

查看此代码以了解工作示例以及绘图的工作原理。

from pattern.en import parse,sentiment

sentences = ["In fact, I'm not convinced that blue is a color.", 
             "The car is blue.",
             "Precisely speaking, no random opinion is allowed.",
             "Democracy is dead. Long live the king."]

complete_text = " ".join(sentences)


for sentence in sentences:
    print sentiment(sentence)

#mind the difference for the last sentence, which contains two dots.         
for sentence in complete_text.split("."):
    print sentiment(sentence)


import numpy as np
import matplotlib.pyplot as plt

a = np.array([ sentiment(sentence) for sentence in complete_text.split(".") ])

print "polarities: ", a[:,0]
print "subjectivities: ", a[:,1]

############ Plotting ############
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(a[:,0], a[:,1], marker="s", linestyle="")
ax.set_xlabel("polarity")
ax.set_ylabel("subjectivity")
plt.show()

【讨论】:

  • 正好解决了问题。尽管我使用 nltk 句子标记器来解析段落并将其调用为代码中的句子。效果很好。
  • sentiment() 返回一个元组 (polarity, subjectivity) 使得生成的 numpy 数组是二维的(第一列中的极性,第二列中的主观性)。与此相反,modality() 返回单个值。因此生成的数组是一维的,您只需要使用它:print "Modality: ", b
  • 是的,确实解决了这个问题。作为最后一个补充问题。请参阅edit2。
【解决方案3】:
    import numpy as np
    import matplotlib.pyplot as plt
    from pattern.en import sentiment,modality
    from matplotlib.pyplot import figure, show
    sentences = ["In fact, I'm not convinced that blue is a color.", 
                 "The car is blue.",
                 "Precisely speaking, no random opinion is allowed.",
                 "Democracy is dead. Long live the king."]
    complete_text = " ".join(sentences)
    for sentence in sentences:
        sentiment(sentence)    
    a = np.array([ sentiment(sentence) for sentence in complete_text.split(".") ])
    for sentence in sentences:
        modality(sentence)
    b = np.array([ modality(sentence) for sentence in complete_text.split(".") ])
    print "polarities: ", a[:,0]
    print "subjectivities: ", a[:,1]
    print "Modality: ", b
    print
    (x1) = a[:,0]
    (y1) = a[:,1]
    (z1) = b
    x = np.array([x1])
    y = np.array([y1])
    z = np.array([z1])
    i_opt1 = np.where((x >= 0) & (0.5 < z) & (z <= 1))
    i_opt2 = np.where((x >= 0) & (0 < z) & (z <= 0.5))  
    i_opt3 = np.where((x >= 0) & (-0.5 < z) & (z <= 0)) 
    i_opt4 = np.where((x >= 0) & (-1 < z) & (z <= -0.5))  
    i_opt5 = np.where((x < 0) &(0.5 < z) & (z <= 1))  
    i_opt6 = np.where((x < 0) &(0 < z) & (z <= 0.5))  
    i_opt7 = np.where((x < 0) &(-0.5 < z) & (z <= 0))  
    i_opt8 = np.where((x < 0) &(-1 < z) & (z <= -0.5))
    plt.scatter(x[i_opt1], y[i_opt1], label='Indicative', color='g', s=25, marker="o")
    plt.scatter(x[i_opt2], y[i_opt2], label='Imperative', color='g', s=25, marker="x")
    plt.scatter(x[i_opt3], y[i_opt3], label='Conditional', color='g', s=25, marker="^")
    plt.scatter(x[i_opt4], y[i_opt4], label='Subjunctive', color='g', s=25, marker="v")
    plt.scatter(x[i_opt5], y[i_opt5], color='r', s=25, marker="o")
    plt.scatter(x[i_opt6], y[i_opt6], color='r', s=25, marker="x")
    plt.scatter(x[i_opt7], y[i_opt7], color='r', s=25, marker="^")
    plt.scatter(x[i_opt8], y[i_opt8], color='r', s=25, marker="v")
    plt.xlabel('Sentiment polarity: negative -> positive')
    plt.ylabel('Subjectivity: objective -> subjective')
    plt.show()

【讨论】:

  • 虽然是一种迂回的方式来达到结果;认为这会对某人有所帮助。但是,如果有人可以对此进行“点击定义”,那就太好了。
猜你喜欢
  • 1970-01-01
  • 2014-04-30
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 2014-12-11
  • 1970-01-01
相关资源
最近更新 更多