第十二周

import keyword

def rewrite_py_file(input_file_path, output_file_path):
with open(input_file_path, 'r', encoding='utf - 8') as f:
content = f.read()

new_content = ""
words = content.split()
for word in words:
    if not keyword.iskeyword(word) and word.isalpha() and word.islower():
        new_content += word.upper() + " "
    else:
        new_content += word + " "

new_content = new_content.rstrip() 

with open(output_file_path, 'w', encoding='utf - 8') as f:
    f.write(new_content)

if name == "main":
input_path = "D:\python作业\hamlet.txt"
output_path = "D:\python作业\output.txt"
rewrite_py_file(input_path, output_path)
import jieba
from wordcloud import WordCloud
import requests
from bs4 import BeautifulSoup

def get_gov_report():
url = "https://www.news.cn/politics/20250312/a71e63d66967404e8e644f9753c65fc9/c.html"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
content = "".join([p.get_text() for p in soup.find_all('p')])
return content

def generate_wordcloud():
report = get_gov_report()
words = jieba.lcut(report)
meaningful_words = [word for word in words if len(word) > 1]
word_freq = {}
for word in meaningful_words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
sorted_word_freq = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:100]
wordcloud = WordCloud(font_path="simhei.ttf", width=800, height=400, background_color="white").generate_from_frequencies(dict(sorted_word_freq))
wordcloud.to_file("gov_report_wordcloud.png")

if name == "main":
generate_wordcloud()
import turtle
class ArcDrawingEngine:
def init(self):
self.t = turtle.Turtle()

def draw_circle_trajectory(self, radius, num_arcs):
    angle_per_arc = 360 / num_arcs
    for _ in range(num_arcs):
        self.t.circle(radius, angle_per_arc)

if name == "main":
engine = ArcDrawingEngine()
radius = 100
num_arcs = 36
engine.draw_circle_trajectory(radius, num_arcs)
turtle.done()
from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
from PIL import Image
import jieba

def generate_logo_wordcloud():
poem = "床前明月光,疑是地上霜。举头望明月,低头思故乡。"
words = jieba.lcut(poem)
word_freq = {}
for word in words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1

logo_mask = np.array(Image.open("C:\\Users\\Xiaoguo Lu\\Desktop\\1321.jpg"))
wordcloud = WordCloud(font_path="simhei.ttf", mask=logo_mask, background_color="white").generate_from_frequencies(word_freq)
image_colors = ImageColorGenerator(logo_mask)
wordcloud.recolor(color_func=image_colors)
wordcloud.to_file("logo_wordcloud.png")

if name == "main":
generate_logo_wordcloud()
def add_word(dictionary, word, meaning):
if word in dictionary:
print("该单词已添加到字典库")
else:
dictionary[word] = meaning
print(f"{word} 已成功添加到字典库")

def query_word(dictionary, word):
if word in dictionary:
print(f"{word}: {dictionary[word]}")
else:
print("字典库中未找到这个单词")

def main():
dictionary = {}
try:
with open("dictionary.txt", 'r', encoding='utf - 8') as f:
for line in f:
parts = line.strip().split()
dictionary[parts[0]] = parts[1]
except FileNotFoundError:
pass

while True:
    print("请选择操作:1. 添加 2. 查询 3. 退出")
    choice = input()
    if choice == "1":
        word = input("请输入要添加的英文单词:")
        meaning = input("请输入对应的中文释义:")
        add_word(dictionary, word, meaning)
    elif choice == "2":
        word = input("请输入要查询的英文单词:")
        query_word(dictionary, word)
    elif choice == "3":
        with open("dictionary.txt", 'w', encoding='utf - 8') as f:
            for word, meaning in dictionary.items():
                f.write(f"{word} {meaning}\n")
        break
    else:
        print("输入有误")

if name == "main":
main()
def add_word(dictionary, word, meaning):
if word in dictionary:
dictionary[word].append(meaning)
print(f"{word} 的新释义已添加到字典库")
else:
dictionary[word] = [meaning]
print(f"{word} 已成功添加到字典库")

def query_word(dictionary, word):
if word in dictionary:
print(f"{word}: {', '.join(dictionary[word])}")
else:
print("字典库中未找到这个单词")

def main():
dictionary = {}
try:
with open("dictionary.txt", 'r', encoding='utf - 8') as f:
for line in f:
parts = line.strip().split(maxsplit=1)
word = parts[0]
meanings = parts[1].split(',')
dictionary[word] = [meaning.strip() for meaning in meanings]
except FileNotFoundError:
pass

while True:
    print("请选择操作:1. 添加 2. 查询 3. 退出")
    choice = input()
    if choice == "1":
        word = input("请输入要添加的英文单词:")
        meaning = input("请输入对应的中文释义:")
        add_word(dictionary, word, meaning)
    elif choice == "2":
        word = input("请输入要查询的英文单词:")
        query_word(dictionary, word)
    elif choice == "3":
        with open("dictionary.txt", 'w', encoding='utf - 8') as f:
            for word, meanings in dictionary.items():
                f.write(f"{word} {', '.join(meanings)}\n")
        break
    else:
        print("输入有误")

if name == "main":
main()

posted @ 2025-05-17 19:39  XiaoguoLu  阅读(64)  评论(0)    收藏  举报