import jieba
from wordcloud import WordCloud
import imageio
mask = imageio.imread('china.jpg')
# 读取小说
def plot_wc(text, name):
wc = WordCloud(
background_color='white',
height=600,
width=600,
font_path='MSYH.TTC',
mask=mask,
# 解决两个重复关联词匹配
collocations = False
).generate(text)
# wc.to_file("%s.png"%(name))
wc.to_file("{}.png".format(name))
def analysis():
with open('./novel/threekingdom.txt', mode='r', encoding='utf-8') as f:
data = f.read()
print(len(data))
word_list = jieba.lcut(data)
new_word_list = []
for word in word_list:
if len(word)<=1:
continue
else:
new_word_list.append(word)
text = " ".join(new_word_list)
plot_wc(text, "三国词云")
# print(word_list)
# 词频统计
counts = {}
for word in new_word_list:
counts[word] = counts.get(word, 0) + 1
# print(counts)
stop_words = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",
"如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",
"东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",
"孔明曰","玄德曰","刘备","云长"}
# 相同指代进行合并
counts["孔明"] = counts["孔明"] + counts["孔明曰"]
counts["玄德"] = counts["玄德"] + counts["玄德曰"] + counts["刘备"]
counts["关公"] = counts["关公"] + counts["云长"]
# 删除不是人名的
for word in stop_words:
del counts[word]
# 转化成列表排序
sort_list = list(counts.items())
sort_list.sort(key=lambda item:item[1], reverse=True)
top10_list = []
for i in range(10):
novel_name, f = sort_list[i]
for j in range(f):
top10_list.append(novel_name)
# 列表变成字符串
top10_text = " ".join(top10_list)
plot_wc(top10_text, "TOP10")
analysis()
Python的面向对象
# class 类名:
# pass
# this 代表当前类对象
class Student:
#可以理解为构造方法
def __init__(self, name, age):
print("__init__被调用")
# 属性
# self 那个对象调用, self就是谁
# python中封装是 变量前 _ __ 下划线
# self._name = name
self.name = name
self.age = age
def say_hello(self):
print("hello")
def show_id(self):
print("id = ", id(self))
def show(self, name, age):
print(name, age)
# print下会被优先调用
def __str__(self):
return "haha"
# 交互式下会被优先调用
def __repr__(self):
return "hehe"
# 方法
# 实例化类(类被new的过程)
student1 = Student("lisi", 18)
student2 = Student("王五", 18)
print(student1)
print(student1.name)
print(student1.age)
student1.say_hello()
# 查看对象在内存中的地址
print(id(student1))
student1.show_id()
print(id(student2))
student2.show_id()
Java 面向对象
package com.myc.day04;
import java.util.ArrayList;
import java.util.Scanner;
// Java Bean 规范类
// 属性建议封装成私有, 必须有空参构造, 建议全参构造
// 要提供公有的get、set方法
public class Student {
private String name; //null
private Integer age; // 0
Student(){}
Student(String name, Integer age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
// 重写 toString
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student("zhansgan", 19);
System.out.println(student1); // com.myc.day04.Student@1b6d3586
System.out.println(student2); // com.myc.day04.Student@1b6d3586
// int[] arr = new int[10];
// System.out.println(arr);
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list); // 重写 object toString()
}
}
继承
class A():
def __init__(self):
self.weight = 100
self.height = 200
class C():
def __init__(self):
self.weight = 200
self.height = 200
# 继承
class B(C, A):
pass
b = B()
print(b.weight)
网友评论