Matplotlib 情绪分析-模式NLP

Matplotlib 情绪分析-模式NLP,matplotlib,nlp,nltk,Matplotlib,Nlp,Nltk,尝试使用模式创建主客体极性图 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.60000000000000

尝试使用模式创建主客体极性图

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)
打印“极性:”,a[:,0]
打印“主观性:”,a[:,1]
打印“模式:”,a[:,2]
s=np.array(a[:,2])
r=np.array(a[:,1])
############策划############
def标记:
如果s>“0.5”和s<“1”:
返回“o”
elif s>“0”或s<“5”:
返回“x”
elif s>“-5”或s<“0”:
返回“v”
其他:
返回“^”
def颜色(r):
如果r>0且r<“1”:
返回“g”
如果r<“0”或r>“-1”:
返回“r”
其他:
返回“r”
图=plt.图()
ax=图添加_子批次(111)
最大散布(a[:,0],a[:,1],标记=标记,颜色=颜色(r),s=100,选取器=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()
ax=fig.add_子批次(111)
0.0)和(0.0)以及(p 0.0 0 0.0)和(p 0.0)以及(p 0 0.0)以及(p 0.0 0)和(0.0 0 0 0 0 0 0)和(0.0 0 0)以及(0.0 0)和(p-0.0)以及(p-0.0)和(p-0.5)和(p-0.5)以及(p-0.5)和(p-0)以及(p-0.0)和(p)以及(p)以及(p-0.5)以及(p-0.5)以及(p-0)和(p-0)以及(p-0.5)以及(p)以及(p-0.0)以及(p)以及(p)和(p)以及(p-0)以及(p-0.0)以及(p)和(p)和(p 0.0)以及(p)以及(p)以及(p)以及(p)以及(p这是对最初问题的回答

在这种情况下,什么定义了您的线路? 如果我们可以假设点分隔句子,那么我们可以使用它将文本
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()
这是对最初问题的回答

在这种情况下,什么定义了您的线路? 如果我们可以假设点分隔句子,那么我们可以使用它将文本
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()
这是对初始问题及其编辑2的回答。有关编辑4的回答,请参见底部

我添加了另一个答案来解决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()
Edit4

问题是返回的索引是条件化数组的索引,而
语句
不是条件化的。 这里有一个程序,希望能做你想做的

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()
将numpy导入为np
将matplotlib.pyplot作为plt导入
句子=[“第0句”、“第1句”、“第2句”、“第3句”、“第4句”、“第5句”]
p=np.数组([0,0.2,-0.3,0.2,0,0.2])
s=np.数组([0.1,0,0,0.3,0.1,0.]))
m=np.数组([1.,-0.25,1.,-0.6,0.2,-0.25])
颜色=np.数组([(0.8*(1-x),0.7*x,0)表示np.ceil(p)])
cond=[(m>0.5)&(m0.0)&(m-0.5)&(m=-1.0)&(m这是对初始问题及其编辑2的回答。对于编辑4的回答,请参见底部

我添加了另一个答案来解决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()
Edit4

问题是返回的索引是条件化数组的索引,而
语句
不是条件化的。 这里有一个程序,希望能做你想做的

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()
将numpy导入为np
将matplotlib.pyplot作为plt导入
句子=[“第0句”、“第1句”、“第2句”、“第3句”、“第4句”、“第5句”]
p=np.数组([0,0.2,-0.3,0.2,0,0.2])
s=np.数组([0.1,0,0,0.3,0.1,0.]))
m=np.数组([1.,-0.25,1.,-0.6,0.2,-0.25])
颜色=np.数组([(0.8*(1-x),0.7*x,0)表示np.ceil(p)])
cond=[(m>0.5)&(m0.0)&(m-0.5)&(m=-1.0)&(m
将numpy作为np导入
将matplotlib.pyplot作为plt导入
从模式中导入情感、情态
从matplotlib.pyplot导入图中,显示
事实上,我不相信蓝色是一种颜色,
“这辆车是蓝色的。”,
“准确地说,不允许随意发表意见。”,
“民主死了,国王万岁。”]
完成_text=”“.连接(句子)
对于句子中的句子:
情绪(句子)
a=np.array([完整文本中句子的情感(句子)拆分(“.”))
对于句子中的句子:
情态(句子)
b=np.array([完整文本中句子的情态(句子)拆分(“.”))
打印“极性:”,a[:,0]
打印“主语”