本文整理汇总了Python中snownlp.SnowNLP方法的典型用法代码示例。如果您正苦于以下问题:Python snownlp.SnowNLP方法的具体用法?Python snownlp.SnowNLP怎么用?Python snownlp.SnowNLP使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类snownlp
的用法示例。
在下文中一共展示了snownlp.SnowNLP方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def post(self):
args = parser.parse_args()
if args['type'] == 'hotel_tfidf':
result = hotel_tfidf_clf.predict([args['sentence']]).astype(np.str)
return result[0], 200
elif args['type'] == 'douban_wb':
result = douban_wb_clf.predict([args['sentence']]).astype(np.str)
return result[0], 200
# elif args['type'] == 'restaurant':
# result = restaurant_clf.predict([args['sentence']]).astype(np.str)
# return result[0], 200
elif args['type'] == 'douban_snowNLP':
s = SnowNLPNew(args['sentence'])
return s.sentiments*5, 200
else:
s = SnowNLP(args['sentence'])
return s.sentiments*5, 200
示例2: output
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def output(poem):
'''美化输出格式'''
poem = poem.strip('/')
lenth = len(poem.split('/')[0])*2 + 8
print()
print('-'*lenth)
print()
for i in poem.split('/'):
print(' '+i)
print()
print('-'*lenth)
print()
snow = SnowNLP(poem)
print("情感倾向:{}".format(snow.sentiments))
print()
print()
time.sleep(random.random()*2)
示例3: emotionParser
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def emotionParser(*names):
conn = conn = sqlite3.connect("deal_data.db")
conn.text_factory = str
cursor = conn.cursor()
likeStr = ""
for i in range(0, len(names)):
likeStr = likeStr + "like \"%" + names[i] + "%\" "
if i + 1 < len(names):
likeStr = likeStr + " or "
print likeStr
cursor.execute("select content from realData where content " + likeStr)
values = cursor.fetchall()
sentimentslist = []
for item in values:
# print SnowNLP(item[0].decode("utf-8")).words
sentimentslist.append(SnowNLP(item[0].decode("utf-8")).sentiments)
plt.hist(sentimentslist, bins=np.arange(0, 1, 0.01), facecolor="#4F8CD6")
plt.xlabel("Sentiments Probability")
plt.ylabel("Quantity")
plt.title("Analysis of Sentiments for Lidan")
plt.show()
cursor.close()
conn.close()
示例4: wallstr_news
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def wallstr_news():
user_agent='Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers={'User-Agent':user_agent}
url='https://wallstreetcn.com/live/blockchain'
raw_page=requests.get(url,headers=headers)
page=bs(raw_page.text)
blockchain_news=page.find_all('div',class_="wscn-tab-pane")[1]
big_news=blockchain_news.find_all('div',class_='live-item score-2')
normal_news=blockchain_news.find_all('div',class_='live-item score-1')
s=0
count=0
for i in big_news:
text=i.find('div',class_='content-html').get_text()
sen=SnowNLP(text)
sentiment=sen.sentiments
s=s+2*sentiment
count=count+2
for i in normal_news:
text=i.find('div',class_='content-html').get_text()
sen=SnowNLP(text)
sentiment=sen.sentiments
s=s+sentiment
count=count+1
total_sentiment=s/count
return(total_sentiment)
开发者ID:benjaminshi02003220,项目名称:Smart-Bitcoin-Auto-Trading-Bot-based-on-Nerual-Networks,代码行数:27,代码来源:wallstreet_news.py
示例5: cal_sentiment
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def cal_sentiment(text):
"""
calculate the sentiment value of a particular sentence powered by SnowNLP
:param text:
:return:
"""
s = SnowNLP(text)
return s.sentiments
示例6: zh_simplify_v2
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def zh_simplify_v2(line, server_model):
return SnowNLP(line).han
示例7: zh_simplify_v2
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def zh_simplify_v2(line):
return SnowNLP(line).han
示例8: sentiments_analysis
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def sentiments_analysis():
from snownlp import SnowNLP
# 读取评论
df = pd.read_csv('comment.csv', sep=';', header=None)
# 获取情感评分
sentiment = lambda x:SnowNLP(x).sentiments
df[2] = df[1].apply(sentiment)
# 写入csv
# df.to_csv('comment_sentiments.csv', sep=';', index=False, header=False)
# 整理数据
df.columns = ['date', 'comment', 'sentiment']
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
# 筛选日期
cacu_df = df[:'2019-02-04']['sentiment']
# 按日统计数量
cacu = cacu_df.resample('D').mean()
# 画图
# 使用plot画pandas建议先注册
register_matplotlib_converters()
# 使用pltz解决中文展示问题
# 新建pltz对象,用于显示中文
# from pyplotz.pyplotz import PyplotZ
# pltz = PyplotZ()
# pltz.enable_chinese()
# pltz.title("流浪地球评论分析")
# pltz.xlabel("日期")
# pltz.ylabel("评论数")
# 通过设置中文字体方式解决中文展示问题
font = FontProperties(fname='../font/PingFang.ttc')
plt.title("流浪地球评论分析", fontproperties=font)
plt.xlabel("日期", fontproperties=font)
plt.ylabel("好感度", fontproperties=font)
plt.plot(cacu)
plt.axis("tight")
# 显示网格
plt.grid(True)
# 自动旋转横轴日期
plt.gcf().autofmt_xdate()
# 显示数值
for a, b in zip(cacu.index, cacu.values):
plt.text(a, b, str(round(b, 4)))
# 保存图片
plt.savefig('comment_sentiment_analysis.png')
# 查看图片
plt.show()
示例9: analyseSignature
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def analyseSignature(friends):
signatures = ''
emotions = []
pattern = re.compile("1f\d.+")
for friend in friends:
signature = friend['Signature']
if(signature != None):
signature = signature.strip().replace('span', '').replace('class', '').replace('emoji', '')
signature = re.sub(r'1f(\d.+)','',signature)
if(len(signature)>0):
nlp = SnowNLP(signature)
emotions.append(nlp.sentiments)
signatures += ' '.join(jieba.analyse.extract_tags(signature,5))
with open('signatures.txt','wt',encoding='utf-8') as file:
file.write(signatures)
# Sinature WordCloud
back_coloring = np.array(Image.open('flower.jpg'))
wordcloud = WordCloud(
font_path='simfang.ttf',
background_color="white",
max_words=1200,
mask=back_coloring,
max_font_size=75,
random_state=45,
width=960,
height=720,
margin=15
)
wordcloud.generate(signatures)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file('signatures.jpg')
# Signature Emotional Judgment
count_good = len(list(filter(lambda x:x>0.66,emotions)))
count_normal = len(list(filter(lambda x:x>=0.33 and x<=0.66,emotions)))
count_bad = len(list(filter(lambda x:x<0.33,emotions)))
print(count_good * 100/len(emotions))
print(count_normal * 100/len(emotions))
print(count_bad * 100/len(emotions))
labels = [u'负面消极',u'中性',u'正面积极']
values = (count_bad,count_normal,count_good)
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel(u'情感判断')
plt.ylabel(u'频数')
plt.xticks(range(3),labels)
plt.legend(loc='upper right',)
plt.bar(range(3), values, color = 'rgb')
plt.title(u'%s的微信好友签名信息情感分析' % friends[0]['NickName'])
plt.show()
# login wechat and extract friends
示例10: __call__
# 需要导入模块: import snownlp [as 别名]
# 或者: from snownlp import SnowNLP [as 别名]
def __call__(self, value, positions=False, chars=False, keeporiginal=False,
removestops=True, start_pos=0, start_char=0, tokenize=True,
mode='', **kwargs):
"""
:param value: The unicode string to tokenize.
:param positions: Whether to record token positions in the token.
:param chars: Whether to record character offsets in the token.
:param start_pos: The position number of the first token. For example,
if you set start_pos=2, the tokens will be numbered 2,3,4,...
instead of 0,1,2,...
:param start_char: The offset of the first character of the first
token. For example, if you set start_char=2, the text "aaa bbb"
will have chars (2,5),(6,9) instead (0,3),(4,7).
:param tokenize: if True, the text should be tokenized.
"""
assert isinstance(value, text_type), "%r is not unicode" % value
# test
#fpath = '/Users/astorer/Dev/txtorg/examples/chinese/1.txt'
#text = open(fpath).read()
#value = unicode(text,encoding='utf-8')
# Thanks, isnowfy!
s = SnowNLP(value)
tokenlist = s.words
t = Token(positions, chars, removestops=removestops, mode=mode,
**kwargs)
if not tokenize:
t.original = t.text = value
t.boost = 1.0
if positions:
t.pos = start_pos
if chars:
t.startchar = start_char
t.endchar = start_char + len(value)
yield t
else:
for (pos,text) in enumerate(tokenlist):
# we may have some off by one errors
# what is the starting character of the token?
start_char_t = value[start_char:].find(text)+start_char
t.text = text
#print pos, start_char_t, text
if positions:
t.pos = start_pos+pos
if chars:
t.startchar = start_char_t
t.endchar = start_char_t + len(text)
yield t
# make the tokens
# copying from https://bitbucket.org/mchaput/whoosh/src/c9ad870378a0f5167182349b64fc3e09c6ca12df/src/whoosh/analysis/tokenizers.py?at=default