improve translation and csci-1100-hw-7
This commit is contained in:
@@ -60,7 +60,7 @@ You will need the data files we provide in `hw6_files.zip`, so be sure to downlo
|
|||||||
|
|
||||||
There are many software systems for analyzing the style and sophistication of written text and even deciding if two documents were authored by the same individual. The systems analyze documents based on the sophistication of word usage, frequently used words, and words that appear closely together. In this assignment you will write a Python program that reads two files containing the text of two different documents, analyzes each document, and compares the documents. The methods we use are simple versions of much more sophisticated methods that are used in practice in the field known as natural language processing (NLP).
|
There are many software systems for analyzing the style and sophistication of written text and even deciding if two documents were authored by the same individual. The systems analyze documents based on the sophistication of word usage, frequently used words, and words that appear closely together. In this assignment you will write a Python program that reads two files containing the text of two different documents, analyzes each document, and compares the documents. The methods we use are simple versions of much more sophisticated methods that are used in practice in the field known as natural language processing (NLP).
|
||||||
|
|
||||||
## Files and Parameters
|
### Files and Parameters
|
||||||
|
|
||||||
Your program must work with three files and an integer parameter.
|
Your program must work with three files and an integer parameter.
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ Enter the maximum separation between words in a pair ==> 2
|
|||||||
2
|
2
|
||||||
```
|
```
|
||||||
|
|
||||||
## Parsing
|
### Parsing
|
||||||
|
|
||||||
The job of parsing for this homework is to break a file of text into a single list of consecutive words. To do this, the contents from a file should first be split up into a list of strings, where each string contains consecutive non-white-space characters. Then each string should have all non-letters removed and all letters converted to lower case. For example, if the contents of a file (e.g., `doc1.txt`) are read to form the string (note the end-of-line and tab characters)
|
The job of parsing for this homework is to break a file of text into a single list of consecutive words. To do this, the contents from a file should first be split up into a list of strings, where each string contains consecutive non-white-space characters. Then each string should have all non-letters removed and all letters converted to lower case. For example, if the contents of a file (e.g., `doc1.txt`) are read to form the string (note the end-of-line and tab characters)
|
||||||
|
|
||||||
@@ -168,7 +168,7 @@ Once you have produced the word list with stop words removed, you are ready to a
|
|||||||
|
|
||||||
5. Finally, as a measure of how distinct the word pairs are, calculate and output, accurate to three decimal places, the ratio of the number of distinct word pairs to the total number of word pairs.
|
5. Finally, as a measure of how distinct the word pairs are, calculate and output, accurate to three decimal places, the ratio of the number of distinct word pairs to the total number of word pairs.
|
||||||
|
|
||||||
#### Compare Documents
|
### Compare Documents
|
||||||
The last step is to compare the documents for complexity and similarity. There are many possible measures, so we will implement just a few.
|
The last step is to compare the documents for complexity and similarity. There are many possible measures, so we will implement just a few.
|
||||||
|
|
||||||
Before we do this we need to define a measure of similarity between two sets. A very common one, and the one we use here, is called Jaccard Similarity. This is a sophisticated-sounding name for a very simple concept (something that happens a lot in computer science and other STEM disciplines). If A and B are two sets, then the Jaccard similarity is just
|
Before we do this we need to define a measure of similarity between two sets. A very common one, and the one we use here, is called Jaccard Similarity. This is a sophisticated-sounding name for a very simple concept (something that happens a lot in computer science and other STEM disciplines). If A and B are two sets, then the Jaccard similarity is just
|
||||||
|
|||||||
BIN
content/en/posts/csci-1100/hw-7/HW7.zip
Normal file
BIN
content/en/posts/csci-1100/hw-7/HW7.zip
Normal file
Binary file not shown.
230
content/en/posts/csci-1100/hw-7/index.md
Normal file
230
content/en/posts/csci-1100/hw-7/index.md
Normal file
@@ -0,0 +1,230 @@
|
|||||||
|
---
|
||||||
|
title: CSCI 1100 - Homework 7 - Dictionaries
|
||||||
|
subtitle:
|
||||||
|
date: 2024-09-12T15:36:47-04:00
|
||||||
|
slug: csci-1100-hw-7
|
||||||
|
draft: false
|
||||||
|
author:
|
||||||
|
name: James
|
||||||
|
link: https://www.jamesflare.com
|
||||||
|
email:
|
||||||
|
avatar: /site-logo.avif
|
||||||
|
description: "This blog post outlines a homework assignment worth 100 points, due on March 28, 2024, focusing on Python dictionary manipulation. The assignment includes two parts: an autocorrect program and a movie rating analysis, both requiring careful handling of data files and dictionary operations."
|
||||||
|
keywords: ["Python", "Dictionaries"]
|
||||||
|
license:
|
||||||
|
comment: true
|
||||||
|
weight: 0
|
||||||
|
tags:
|
||||||
|
- CSCI 1100
|
||||||
|
- Homework
|
||||||
|
- RPI
|
||||||
|
- Python
|
||||||
|
- Programming
|
||||||
|
categories:
|
||||||
|
- Programming
|
||||||
|
collections:
|
||||||
|
- CSCI 1100
|
||||||
|
hiddenFromHomePage: false
|
||||||
|
hiddenFromSearch: false
|
||||||
|
hiddenFromRss: false
|
||||||
|
hiddenFromRelated: false
|
||||||
|
summary: "This blog post outlines a homework assignment worth 100 points, due on March 28, 2024, focusing on Python dictionary manipulation. The assignment includes two parts: an autocorrect program and a movie rating analysis, both requiring careful handling of data files and dictionary operations."
|
||||||
|
resources:
|
||||||
|
- name: featured-image
|
||||||
|
src: featured-image.jpg
|
||||||
|
- name: featured-image-preview
|
||||||
|
src: featured-image-preview.jpg
|
||||||
|
toc: true
|
||||||
|
math: true
|
||||||
|
lightgallery: false
|
||||||
|
password:
|
||||||
|
message:
|
||||||
|
repost:
|
||||||
|
enable: false
|
||||||
|
url:
|
||||||
|
|
||||||
|
# See details front matter: https://fixit.lruihao.cn/documentation/content-management/introduction/#front-matter
|
||||||
|
---
|
||||||
|
|
||||||
|
<!--more-->
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This homework is worth 100 points and it will be due Thursday, March 28, 2024 at 11:59:59 pm.
|
||||||
|
|
||||||
|
It has two parts, each worth 50 points. Please download `hw7_files.zip` and unzip it into the directory for your HW7. You will find multiple data files to be used in both parts.
|
||||||
|
|
||||||
|
The goal of this assignment is to work with dictionaries. In part 1, you will do some simple file processing. Read the guidelines very carefully there. In part 2, we have done all the file work for you so you should be able to get the data loaded in just a few lines. For both parts, you will spend most of your time manipulating dictionaries given to you in the various files.
|
||||||
|
|
||||||
|
Please remember to name your files `hw7_part1.py` and `hw7_part2.py`.
|
||||||
|
|
||||||
|
As always, make sure you follow the program structure guidelines. You will be graded on program correctness as well as good program structure.
|
||||||
|
|
||||||
|
Remember as well that we will be continuing to test homeworks for similarity. So, follow our guidelines for the acceptable levels of collaboration. You can download the guidelines from the Course Resources section of Submitty if you need a refresher. Note that this includes using someone else’s code from a previous semester. Make sure the code you submit is truly your own.
|
||||||
|
|
||||||
|
## Honor Statement
|
||||||
|
|
||||||
|
There have been a number of incidents of academic dishonesty on homework assignments and this must change. Cases are easily flagged using automated tools, and verified by the instructors. This results in substantial grade penalties, poor learning, frustration, and a waste of precious time for everyone concerned. In order to mitigate this, the following is a restatement of the course integrity policy in the form of a pledge. By submitting your homework solution files for grading on Submitty, you acknowledge that you understand and have abided by this pledge:
|
||||||
|
|
||||||
|
- I have not shown my code to anyone in this class, especially not for the purposes of guiding their own work.
|
||||||
|
- I have not copied, with or without modification, the code of another student in this class or who took the class in a previous semester.
|
||||||
|
- I have not used a solution found or purchased on the internet for this assignment.
|
||||||
|
- The work I am submitting is my own and I have written it myself.
|
||||||
|
- I understand that if I am found to have broken this pledge that I will receive a 0 on the assignment and an additional 10 point overall grade penalty.
|
||||||
|
|
||||||
|
You will be asked to agree to each of these individual statements before you can submit your solutions to this homework.
|
||||||
|
|
||||||
|
Please understand that if you are one of the vast majority of the students who follow the rules and only work with other students to understand problem descriptions, Python constructs, and solution approaches you will not have any trouble whatsoever.
|
||||||
|
|
||||||
|
## Part 1: Autocorrect
|
||||||
|
|
||||||
|
We have all used auto-correct to fix our various typos and mistakes as we write, but have you ever wondered how it works? Here is a small version of autocorrect that looks for a few common typographical errors.
|
||||||
|
|
||||||
|
To solve this problem, your program will read the names of three files:
|
||||||
|
|
||||||
|
- The first contains a list of valid words and their frequencies,
|
||||||
|
- The second contains a list of words to autocorrect, and
|
||||||
|
- The third contains potential letter substitutions (described below).
|
||||||
|
|
||||||
|
The input word file has two entries per line; the first entry on the line is a single valid word in the English language and the second entry is a float representing the frequency of the word in the lexicon. The two values are separated by a comma.
|
||||||
|
|
||||||
|
Read this English dictionary into a Python dictionary, using words as keys and frequency as values. You will use the frequency for deciding the most likely correction when there are multiple possibilities
|
||||||
|
|
||||||
|
The keyboard file has a line for each letter. The first entry on the line is the letter to be replaced and the remaining letters are possible substitutions for that letter. All the letters on the line are separated by spaces. These substitutions are calculated based on adjacency on the keyboard, so if you look down at your keyboard, you will see that the “a” key is surrounded by “q”, “w”, “s”, and “z”. Other substitutions were calculated similarly, so:
|
||||||
|
|
||||||
|
```text
|
||||||
|
b v f g h n
|
||||||
|
```
|
||||||
|
|
||||||
|
means that a possible replacement for `b` is any one of `v f g h n`. Read this keyboard file into a dictionary: the first letter is the key (e.g., b) and the remaining letters are the value, stored as a list.
|
||||||
|
|
||||||
|
Your program will then go through every single word in the input file, autocorrect each word and print the correction. To correct a single word, you will consider the following:
|
||||||
|
|
||||||
|
- **FOUND**: If the word is in the dictionary, it is correct. There is no need for a change. Print it as found, and go on to the next word.
|
||||||
|
- Otherwise consider all of the remaining possibilities.
|
||||||
|
|
||||||
|
- **DROP**: If the word is not found, consider all possible ways to drop a single letter from the word. Store any valid words (words that are in your English dictionary) in some container (list/set/dictionary). These will be candidate corrections.
|
||||||
|
- **INSERT**: If the word is not found, consider all possible ways to insert a single letter in the word. Store any valid words in some container (list/set/dictionary). These will be candidate corrections.
|
||||||
|
- **SWAP**: Consider all possible ways to swap two consecutive letters from the word. Store any valid words in some container (list/set/dictionary). These will be candidate corrections.
|
||||||
|
- **REPLACE**: Next consider all possible ways to change a single letter in the word with any other letter from the possible replacements in the keyboard file. Store any valid words in some container (list/set/dictionary). These will be candidate corrections.
|
||||||
|
|
||||||
|
For example, for the keyboard file we have given you, possible replacements for `b` are `v f g h n`. Hence, if you are replacing `b` in `abar`, you should consider: `avar`, `afar`, `agar`, `ahar`, `anar`.
|
||||||
|
|
||||||
|
After going through all of the above, if there are multiple potential matches, sort them by their potential frequency from the English dictionary and return the top 3 values that are in most frequent usage as the most likely corrections in order. If there are three or fewer potential matches, print all of them in order. In the unlikely event that two words are equally likely based on frequency, you should pick the one that comes last in lexicographical order. See the note below.
|
||||||
|
|
||||||
|
If there are no potential matches using any of the above corrections, print `NOT FOUND`. Otherwise, print the word (15 spaces), the number of matches, and at most three matches, all on one line.
|
||||||
|
|
||||||
|
An example output of your program for the English dictionary we have given you is contained in `part1_output_01.txt`. Note that, we will use a more extensive dictionary on Submitty, so your results may be different on Submitty than they are on your laptop.
|
||||||
|
|
||||||
|
When you are sure your homework works properly, submit it to Submitty. Your program must be named `hw7_part1.py` to work correctly.
|
||||||
|
|
||||||
|
### Notes:
|
||||||
|
|
||||||
|
1. Do NOT write a for loop to search to see if a string (word or letter) is in a dictionary! This will be very slow and may cause Submitty to terminate your program (and you to lose substantial points). Instead, you must use the `in` operator.
|
||||||
|
2. It is possible, but unlikely, that a candidate replacement word is generated more than once. We recommend that you gather all possible candidate replacements into a set before looking them up in the dictionary.
|
||||||
|
3. Ordering the potential matches by frequency can be handled easily. For each potential match, create a tuple with the frequencey first, followed by the word. Add this to a list and then sort the list in reverse order. For example, if the list is `v`, then you just need the line of code `v.sort(reverse=True)`
|
||||||
|
|
||||||
|
## Part 2: Well rated and not so well rated movies ...
|
||||||
|
|
||||||
|
In this section, we are providing you with two data files `movies.json` and `ratings.json` in JSON data format. The first data file is movie information directly from IMDB, including ratings for some movies but not all. The second file contains ratings from Twitter. Be careful: Not all movies in `movies.json` have a rating in `ratings.json`, and not all movies in `ratings.json` have relevant info in `movies.json`.
|
||||||
|
|
||||||
|
The data can be read in its entirety with the following five lines of code:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
movies = json.loads(open("movies.json").read())
|
||||||
|
ratings = json.loads(open("ratings.json").read())
|
||||||
|
```
|
||||||
|
|
||||||
|
Both files store data in a dictionary. The first dictionary has movie ids as keys and a second dictionary containing an attribute list for the movie as a value. For example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
print(movies['3520029'])
|
||||||
|
(movie with id '3520029') produces the output:
|
||||||
|
{'genre': ['Sci-Fi', 'Action', 'Adventure'], 'movie_year': 2010,
|
||||||
|
'name': 'TRON: Legacy', 'rating': 6.8, 'numvotes': 254865}
|
||||||
|
```
|
||||||
|
|
||||||
|
This is same as saying:
|
||||||
|
|
||||||
|
```python
|
||||||
|
movies = dict()
|
||||||
|
movies['3520029'] = {'genre': ['Sci-Fi', 'Action', 'Adventure'],
|
||||||
|
'movie_year': 2010, 'name': 'TRON: Legacy',
|
||||||
|
'rating': 6.8, 'numvotes': 254865}
|
||||||
|
```
|
||||||
|
|
||||||
|
If we wanted to get the individual information for each movie, we can use the following commands:
|
||||||
|
|
||||||
|
```python
|
||||||
|
print(movies['3520029']['genre'])
|
||||||
|
print(movies['3520029']['movie_year'])
|
||||||
|
print(movies['3520029']['rating'])
|
||||||
|
print(movies['3520029']['numvotes'])
|
||||||
|
```
|
||||||
|
|
||||||
|
which would provide the output:
|
||||||
|
|
||||||
|
```python
|
||||||
|
['Sci-Fi', 'Action', 'Adventure']
|
||||||
|
2010
|
||||||
|
6.8
|
||||||
|
254865
|
||||||
|
```
|
||||||
|
|
||||||
|
The second dictionary again has movie ids as keys, and a list of ratings as values. For example,
|
||||||
|
|
||||||
|
```python
|
||||||
|
print(ratings['3520029'])
|
||||||
|
(movie with id '3520029') produces the output:
|
||||||
|
[6, 7, 7, 7, 8]
|
||||||
|
```
|
||||||
|
|
||||||
|
So, this movie had 5 ratings with the above values.
|
||||||
|
|
||||||
|
Now, on to the homework.
|
||||||
|
|
||||||
|
### Problem specification
|
||||||
|
|
||||||
|
In this homework, assume you are given these two files called `movies.json` and `ratings.json`. Read the data in from these files. Ask the user for a year range: min year and max year, and two weights: `w1` and `w2`. Find all movies in movies made between min and max years (inclusive of both min and max years). For each movie, compute the combined rating for the movie as follows:
|
||||||
|
|
||||||
|
```python
|
||||||
|
(w1 * imdb_rating + w2 * average_twitter_rating) / (w1 + w2)
|
||||||
|
```
|
||||||
|
|
||||||
|
where the `imdb_rating` comes from movies and `average_twitter_rating` is the average rating from ratings.
|
||||||
|
|
||||||
|
If a movie is not rated in Twitter, or if the Twitter rating has fewer than 3 entries, skip the movie. Now, repeatedly ask the user for a genre of movie and return the best and worst movies in that genre based on the years given and the rating you calculated. Repeat until the user enters stop.
|
||||||
|
|
||||||
|
An example of the program run (how it will look when you run it using Spyder) is provided in file `hw7_part2_output_01.txt` (the second line for each movie has 8 spaces at the start of the line, and the rating is given in `{:.2f}` format).
|
||||||
|
|
||||||
|
The movies we are giving you for testing are a subset of the movies we will use during testing on Submitty, so do not be surprised if there are differences when you submit.
|
||||||
|
|
||||||
|
When you are sure your homework works properly, submit it to Submitty. Your program must be named `hw7_part2.py` to work correctly.
|
||||||
|
|
||||||
|
### General hint on sorting
|
||||||
|
|
||||||
|
It is possible that two movies have the same rating. Consider the following code:
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> example = [(1, "b"), (1, "a"), (2, "b"), (2, "a")]
|
||||||
|
>>> sorted(example)
|
||||||
|
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
|
||||||
|
>>> sorted(example, reverse=True)
|
||||||
|
[(2, 'b'), (2, 'a'), (1, 'b'), (1, 'a')]
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that the sort puts tuples in order based on the index 0 value first, but in the case of ties, the tie is broken by the index 1 tuple. (If there were a tie in both the index 0 and the index 1 tuple, the sort would continue with the index 2 tuple if available and so on.) The same relationship holds when sorting lists of lists.
|
||||||
|
|
||||||
|
To determine the worst and best movies, the example code used a sort with the rating in the index 0 spot and with the name of the movie in the index 1 position. Keep this in mind when you are determining the worst and best movies.
|
||||||
|
|
||||||
|
## Supporting Files
|
||||||
|
|
||||||
|
{{< link href="HW7.zip" content="HW7.zip" title="Download HW7.zip" download="HW7.zip" card=true >}}
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> I didn't get a full mark in this assignment (Only 96%), so I didn't post the solution. I may redo it to get a full mark solution. After that, I will add it here.
|
||||||
@@ -50,97 +50,76 @@ repost:
|
|||||||
|
|
||||||
## 概述
|
## 概述
|
||||||
|
|
||||||
这个作业在你的总作业成绩中占 100 分。截止日期为 2024 年 3 月 21 日星期四晚上 11:59:59。像往常一样,会有自动评分分数、教师测试用例分数和助教评分分数的混合。这个作业只有一个"部分"。
|
这份作业总共占你总作业成绩的100分。它将于2024年3月21日晚上11:59:59截止。像往常一样,会有自动评分的部分、助教测试案例部分和TA评分的部分。这个作业只有一个“part”。
|
||||||
|
|
||||||
请参阅提交指南和协作政策手册,了解关于评分和过度协作的讨论。这些规则将在本学期剩余时间内生效。
|
请参阅手头文件中的提交指南和合作政策以了解关于评分的讨论以及什么被认为是过度的合作行为。这些规则在整个学期中都有效。
|
||||||
|
|
||||||
你将需要我们在 `hw6_files.zip` 中提供的数据文件,所以请务必从 Submitty 的课程材料部分下载此文件,并将其解压缩到你的 HW 6 目录中。该 zip 文件包含数据文件以及程序的示例输入/输出。
|
你需要我们提供的数据文件 `hw6_files.zip`,所以务必从Submitty课程材料部分下载该文件并将其解压缩到你为作业6创建的目录下。zip 文件包含了数据文件和程序示例输入/输出。
|
||||||
|
|
||||||
## 问题介绍
|
## 问题介绍
|
||||||
|
|
||||||
有许多软件系统可以分析书面文本的风格和复杂程度,甚至可以判断两个文档是否由同一个人撰写。这些系统根据词汇使用的复杂程度、常用词以及紧密出现在一起的词来分析文档。在这个作业中,你将编写一个 Python 程序,读取包含两个不同文档文本的两个文件,分析每个文档,并比较这些文档。我们使用的方法是在自然语言处理 (NLP) 领域实际使用的更复杂方法的简化版本。
|
有许多软件系统用于分析书面文本的风格和复杂性,并且可以判断两份文档是否由同一人撰写。这些系统根据词汇使用的复杂程度、常用词以及紧密相邻出现的词语来分析文档。在这个作业中,你将编写一个Python程序,该程序读取两个包含不同文档内容的文件,对每个文档进行分析,并比较这两个文档。我们使用的方法是自然语言处理(NLP)领域实际应用中的更高级方法的简化版本。
|
||||||
|
|
||||||
## 文件和参数
|
### 文件和参数
|
||||||
|
|
||||||
你的程序必须使用三个文件和一个整数参数。
|
你的程序必须与三个文件和一个整数参数一起工作。
|
||||||
|
|
||||||
第一个文件的名称对于你程序的每次运行都将是 `stop.txt`,所以你不需要询问用户。该文件包含我们将称为"停用词"的内容——应该忽略的词。你必须确保 `stop.txt` 文件与你的 `hw6_sol.py` Python 文件在同一文件夹中。我们将提供一个示例,但可能在测试你的代码时使用其他示例。
|
第一个文件的名字将是 `stop.txt`,每次运行程序时都会用到这个文件名,因此你不需要向用户请求它。该文件包含了我们称为“停用词”的单词——应该忽略的词汇。你需要确保 `stop.txt` 文件位于你的 `hw6_sol.py` Python 文件所在的同一目录下。我们会提供一个示例,但测试代码可能会使用其他不同的文件。
|
||||||
|
|
||||||
你必须请求要分析和比较的两个文档的名称以及一个整数"最大分隔"参数,这里将称为 `max_sep`。请求应如下所示:
|
你还必须请求两个文档的名字用于分析和比较以及一个整数参数“最大间隔”,这个参数将被称作 `max_sep`。这些请求应该像下面这样:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
Enter the first file to analyze and compare ==> doc1.txt
|
输入要分析并比较的第一个文件名 ==> doc1.txt
|
||||||
doc1.txt
|
doc1.txt
|
||||||
Enter the second file to analyze and compare ==> doc2.txt
|
输入要分析并比较的第二个文件名 ==> doc2.txt
|
||||||
doc2.txt
|
doc2.txt
|
||||||
Enter the maximum separation between words in a pair ==> 2
|
输入单词对之间允许的最大间隔数 ==> 2
|
||||||
2
|
2
|
||||||
```
|
```
|
||||||
|
|
||||||
## 解析
|
### 解析
|
||||||
|
|
||||||
这个作业的解析工作是将文本文件分解为一个连续单词的列表。为此,应首先将文件的内容拆分为字符串列表,其中每个字符串包含连续的非空白字符。然后,每个字符串应删除所有非字母并将所有字母转换为小写。例如,如果文件的内容(例如 `doc1.txt`)被读取以形成字符串(注意行尾和制表符)
|
这个作业的任务是将一个文本文件解析成单个连续词组成的列表。为此,应该首先将文件内容分割成字符串列表,其中每个字符串包含连续的非空格字符。然后,需要从每个字符串中移除所有非字母字符,并将所有字母转换为小写形式。例如,如果读取了一个名为 `doc1.txt` 的文件(注意行尾和制表符):
|
||||||
|
|
||||||
```python
|
```python
|
||||||
s = " 01-34 can't 42weather67 puPPy, \r \t and123\n Ch73%allenge 10ho32use,.\n"
|
s = " 01-34 can't 42weather67 puPPy, \r \t and123\n Ch73%allenge 10ho32use,.\n"
|
||||||
```
|
```
|
||||||
|
|
||||||
然后拆分应产生字符串列表
|
那么分割结果应该得到字符串列表:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
['01-34', "can't", '42weather67', 'puPPy,', 'and123', 'Ch73%allenge', '10ho32use,.']
|
['01-34', "can't", '42weather67', 'puPPy,', 'and123', 'Ch73%allenge', '10ho32use,.']
|
||||||
```
|
```
|
||||||
|
|
||||||
并且这应该被拆分为(非空)字符串列表
|
然后进一步解析为非空字符串的列表
|
||||||
|
|
||||||
```python
|
```python
|
||||||
['cant', 'weather', 'puppy', 'and', 'challenge', 'house']
|
['cant', 'weather', 'puppy', 'and', 'challenge', 'house']
|
||||||
```
|
```
|
||||||
|
|
||||||
请注意,第一个字符串 `'01-34'` 被完全删除,因为它没有字母。所有三个文件——`stop.txt` 和上面称为 `doc1.txt` 和 `doc2.txt` 的两个文档文件——都应以这种方式解析。
|
注意,第一个字符串 `'01-34'` 因为没有字母而完全被移除。所有三个文件——`stop.txt` 和两个文档文件 `doc1.txt` 和 `doc2.txt` ——都应按照这种方式进行解析。
|
||||||
|
|
||||||
完成此解析后,解析 `stop.txt` 文件产生的列表应转换为集合。此集合包含在 NLP 中被称为"停用词"的内容——出现频率如此之高以至于应该忽略的词。
|
一旦完成这些解析步骤,从解析 `stop.txt` 文件得到的列表应该转换成集合。此集合包含自然语言处理(NLP)中所谓的“停用词”——在文本中出现频繁到可以忽略不计的词汇。
|
||||||
|
|
||||||
`doc1.txt` 和 `doc2.txt` 文件包含要比较的两个文档的文本。对于每个文件,从解析返回的列表应通过删除任何停用词来进一步修改。继续我们的示例,如果 `'cant'` 和 `'and'` 是停用词,那么单词列表应减少为
|
文件 `doc1.txt` 和 `doc2.txt` 包含要比较的两个文档的内容。对于每个文件,解析返回的列表应进一步通过移除所有停用词来修改。继续我们的示例,如果 `'cant'` 和 `'and'` 是停用词,则单词列表应该被减少为:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
['weather', 'puppy', 'challenge', 'house']
|
['weather', 'puppy', 'challenge', 'house']
|
||||||
```
|
```
|
||||||
|
|
||||||
像"and"这样的词几乎总是在停用词列表中,而"cant"(实际上是缩写"can't")在某些列表中。请注意,从 `doc1.txt` 和 `doc2.txt` 构建的单词列表应保留为列表,因为单词顺序很重要。
|
像 "and" 这样的词汇几乎总是出现在停用词表中,而 "cant"(实际上是缩写形式的 "can't")则在某些情况下会出现。注意,从 `doc1.txt` 和 `doc2.txt` 构建出来的单词列表应该保持为列表形式,因为单词顺序很重要。
|
||||||
|
|
||||||
### 分析每个文档的单词列表
|
### 分析每个文档的单词列表
|
||||||
一旦你生成了删除停用词的单词列表,你就可以分析单词列表了。有很多方法可以做到这一点,但以下是此作业所需的方法:
|
|
||||||
|
|
||||||
1. 计算并输出平均单词长度,精确到小数点后两位。这里的想法是单词长度是复杂程度的粗略指标。
|
一旦你生成了移除了停用词后的单词列表,就可以开始分析这个单词列表了。有许多方法可以完成这一点,但这里只列出本作业所要求的方法:
|
||||||
|
|
||||||
2. 计算并输出不同单词数与总单词数之比,精确到小数点后三位。这是衡量所使用语言多样性的一种方法(尽管必须记住,一些作者重复使用单词和短语以加强他们的信息。)
|
1. 计算并输出平均单词长度(保留两位小数)。这里的思路是,单词长度是一个粗略的语言复杂度指标。
|
||||||
|
|
||||||
3. 对于从 1 开始的每个单词长度,找到具有该长度的单词集。打印长度、具有该长度的不同单词数以及最多六个这些单词。如果对于某个长度,有六个或更少的单词,则打印所有六个,但如果有超过六个,则按字母顺序打印前三个和后三个。例如,假设我们上面的简单文本示例扩展为列表
|
2. 计算并输出不同词的数量与总词数量的比例(保留三位小数),这衡量了使用的语言的多样性(不过必须记住一些作者会反复使用某些词汇和短语来加强他们的信息)。
|
||||||
|
|
||||||
```python
|
3. 对于每个从1开始的单词长度,找到具有该长度的所有单词集合。打印出这个长度、不同单词的数量,并输出最多六个这些单词。如果对于某个特定长度有六或更少个单词,则全部列出,如果有超过六个则列出前三个和后三个按字母顺序排列。
|
||||||
['weather', 'puppy', 'challenge', 'house', 'whistle', 'nation', 'vest',
|
|
||||||
'safety', 'house', 'puppy', 'card', 'weather', 'card', 'bike',
|
|
||||||
'equality', 'justice', 'pride', 'orange', 'track', 'truck',
|
|
||||||
'basket', 'bakery', 'apples', 'bike', 'truck', 'horse', 'house',
|
|
||||||
'scratch', 'matter', 'trash']
|
|
||||||
```
|
|
||||||
|
|
||||||
那么输出应该是
|
4. 找到文档中的所有唯一单词对。一个单词对是一个两个元素的元组,在文档列表中距离不超过 `max_sep` 的位置出现的两个词构成。例如,如果用户输入导致 `max_sep == 2`,那么生成的第一个六个单词对是:
|
||||||
|
|
||||||
```text
|
|
||||||
1: 0:
|
|
||||||
2: 0:
|
|
||||||
3: 0:
|
|
||||||
4: 3: bike card vest
|
|
||||||
5: 7: horse house pride ... track trash truck
|
|
||||||
6: 7: apples bakery basket ... nation orange safety
|
|
||||||
7: 4: justice scratch weather whistle
|
|
||||||
8: 1: equality
|
|
||||||
9: 1: challenge
|
|
||||||
```
|
|
||||||
|
|
||||||
4. 找到此文档的不同单词对。单词对是文档列表中相隔 `max_sep` 个或更少位置出现的两个单词的二元组。例如,如果用户输入导致 `max_sep == 2`,那么生成的前六个单词对将是:
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
('puppy', 'weather'), ('challenge', 'weather'),
|
('puppy', 'weather'), ('challenge', 'weather'),
|
||||||
@@ -148,78 +127,60 @@ s = " 01-34 can't 42weather67 puPPy, \r \t and123\n Ch73%allenge 10ho32use,.\n"
|
|||||||
('challenge', 'house'), ('challenge', 'whistle')
|
('challenge', 'house'), ('challenge', 'whistle')
|
||||||
```
|
```
|
||||||
|
|
||||||
你的程序应输出不同单词对的总数。(请注意,`('puppy', 'weather')` 和 `('weather', 'puppy')` 应视为相同的单词对。)它还应按字母顺序输出前 5 个单词对(而不是它们形成的顺序,上面写的就是这样)和最后 5 个单词对。你可以假设,无需检查,有足够的单词来生成这些对。以下是上面较长示例的输出(假设读取它们的文件名为 `ex2.txt`):
|
你的程序应该输出唯一单词对的总数,并且也应按字母顺序输出前五个和后五个单词对。你可以假设有足够的单词来生成这些对。
|
||||||
|
|
||||||
```text
|
5. 最终,作为衡量单词对独特性的指标,计算并输出(保留三位小数)唯一单词对的数量与总单词对数量的比例。
|
||||||
Word pairs for document ex2.txt
|
|
||||||
54 distinct pairs
|
|
||||||
apples bakery
|
|
||||||
apples basket
|
|
||||||
apples bike
|
|
||||||
apples truck
|
|
||||||
bakery basket
|
|
||||||
...
|
|
||||||
puppy weather
|
|
||||||
safety vest
|
|
||||||
scratch trash
|
|
||||||
track truck
|
|
||||||
vest whistle
|
|
||||||
```
|
|
||||||
|
|
||||||
5. 最后,作为单词对的独特性的度量,计算并输出不同单词对的数量与单词对总数之比,精确到小数点后三位。
|
### 比较文档
|
||||||
|
|
||||||
#### 比较文档
|
最后一步是根据复杂性和相似性比较这两个文档。有许多可能的度量标准,我们将实现其中的一些。
|
||||||
最后一步是比较文档的复杂性和相似性。有许多可能的度量方法,所以我们将只实现其中的一些。
|
|
||||||
|
|
||||||
在我们这样做之前,我们需要定义两个集合之间的相似性度量。一个非常常见的,也是我们在这里使用的,称为 Jaccard 相似度。这是一个听起来很复杂的名称,但概念非常简单(在计算机科学和其他 STEM 学科中经常发生这种情况)。如果 A 和 B 是两个集合,那么 Jaccard 相似度就是
|
在进行这个步骤之前我们需要定义两个集合之间的相似度测量方法。一个非常常见且在这里使用的叫做雅卡尔相似度(Jaccard Similarity)。这是一个听起来很高深但其实很简单的方法(这种情况在计算机科学和其他STEM学科中经常出现)。如果A和B是两个集合,那么雅卡尔相似度就是
|
||||||
|
|
||||||
$$
|
$$
|
||||||
J(A, B) = \frac{|A \cap B)|}{|A \cup B)|}
|
J(A, B) = \frac{|A \cap B|}{|A \cup B|}
|
||||||
$$
|
$$
|
||||||
|
|
||||||
用通俗的英语来说,它就是两个集合的交集大小除以它们的并集大小。举例来说,如果 $A$ 和 $B$ 相等,$J(A, B)$ = 1,如果 A 和 B 不相交,$J(A, B)$ = 0。作为特殊情况,如果一个或两个集合为空,则度量为 0。使用 Python 集合操作可以非常容易地计算 Jaccard 度量。
|
用简单的英语来说,这就是两个集合的交集大小除以它们并集的大小。例如,如果 $A$ 和 $B$ 相等,则 $J(A, B)$ = 1;如果 $A$ 和 $B$ 没有共同元素(即不相交),则 $J(A, B)$ = 0。作为特殊情况,如果一个或两个集合为空,度量值为0。雅卡尔度量使用Python集合操作计算非常简单。
|
||||||
|
|
||||||
以下是文档之间的比较度量:
|
这里有一些文档之间比较的度量标准:
|
||||||
|
|
||||||
1. 决定哪个文档的平均单词长度更大。这是衡量哪个文档使用更复杂语言的粗略度量。
|
1. 决定哪个文档具有更大的平均单词长度。这是一个粗略的语言复杂性的指标。
|
||||||
|
2. 计算两份文档中整体词汇使用的雅卡尔相似度(保留三位小数)。
|
||||||
|
3. 对于每个单词长度,计算单词使用情况的雅卡尔相似度。输出也应精确到三位小数。
|
||||||
|
4. 计算单词对集合之间的雅卡尔相似度。输出应该准确到四位小数。我们研究的这些文档不会具有大量的成对相似性,但在其他情况下这是一个有用的比较度量。
|
||||||
|
|
||||||
2. 计算两个文档中总体单词使用的 Jaccard 相似度。这应精确到小数点后三位。
|
请参考示例输出以获取详细信息。
|
||||||
|
|
||||||
3. 计算每个单词长度的单词使用的 Jaccard 相似度。每个输出也应精确到小数点后三位。
|
## 备注
|
||||||
|
|
||||||
4. 计算单词对集之间的 Jaccard 相似度。输出应精确到小数点后四位。我们在这里研究的文档不会有实质性的对相似性,但在其他情况下,这是一个有用的比较度量。
|
- 这个作业的重要部分是练习使用集合。最复杂的实例出现在处理每个单词长度的词集计算中。这需要你构建一个列表中的集合。列表第k项对应的集合应该包含长度为k的单词。
|
||||||
|
|
||||||
有关详细信息,请参阅示例输出。
|
- 排序两个字符串元组构成的列表或集合非常简单。(注意当你对集合进行排序时,结果是一个列表)产生的顺序是按元组的第一个元素字母顺序排列,对于并列的情况则按照第二个元素字母顺序。例如:
|
||||||
|
|
||||||
## 注意事项
|
|
||||||
|
|
||||||
- 本作业的一个重要部分是练习使用集合。最复杂的情况发生在处理每个单词长度的单词集的计算时。这需要你形成一个集合列表。与列表中的条目 k 相关联的集合应该是长度为 k 的单词。
|
|
||||||
|
|
||||||
- 对字符串的二元组列表或集合进行排序很简单。(请注意,当你对一个集合进行排序时,结果是一个列表。)产生的顺序是按元组的第一个元素按字母顺序排列,然后对于相同的元素,按第二个元素按字母顺序排列。例如,
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
>>> v = [('elephant', 'kenya'), ('lion', 'kenya'), ('elephant', 'tanzania'), \
|
>>> v = [('elephant', 'kenya'), ('lion', 'kenya'), ('elephant', 'tanzania'),
|
||||||
('bear', 'russia'), ('bear', 'canada')]
|
('bear', 'russia'), ('bear', 'canada')]
|
||||||
>>> sorted(v)
|
>>> sorted(v)
|
||||||
[('bear', 'canada'), ('bear', 'russia'), ('elephant', 'kenya'), \
|
[('bear', 'canada'), ('bear', 'russia'), ('elephant', 'kenya'),
|
||||||
('elephant', 'tanzania'), ('lion', 'kenya')]
|
('elephant', 'tanzania'), ('lion', 'kenya')]
|
||||||
```
|
```
|
||||||
|
|
||||||
- 只提交一个 Python 文件 `hw6_sol.py`。
|
- 提交一个单一的Python文件 `hw6_sol.py`。
|
||||||
|
|
||||||
- 我们分析中缺少的一个组成部分是每个单词出现的频率。使用字典可以很容易地跟踪这一点,但我们不会在这个作业中这样做。当你学习字典时,思考一下它们如何用于增强我们在这里所做的分析。
|
- 我们的分析中缺失的一个重要部分是每个单词出现的频率。这很容易使用字典来跟踪,但我们不会在本次作业中这样做。当你学习到字典时,请思考如何利用它们来增强我们在此处进行的分析。
|
||||||
|
|
||||||
## 文档文件
|
## 文档文件
|
||||||
|
|
||||||
我们提供了上面描述的示例,我们将使用其他几个文档测试你的代码(其中一些是):
|
我们已经提供了上述示例,并且我们将对你的代码进行测试,同时还将提供其他一些文档(其中几个包括):
|
||||||
|
|
||||||
- Elizabeth Alexander 的诗《Praise Song for the Day》。
|
- Elizabeth Alexander 的诗歌《赞歌》。
|
||||||
- Maya Angelou 的诗《On the Pulse of the Morning》。
|
- Maya Angelou 的诗歌《脉搏之晨》。
|
||||||
- William Shakespeare 的《Hamlet》中的一个场景。
|
- William Shakespeare 的戏剧《哈姆雷特》中的一个场景。
|
||||||
- Dr. Seuss 的《The Cat in the Hat》
|
- Dr. Seuss 的《帽子猫》
|
||||||
- Walt Whitman 的《When Lilacs Last in the Dooryard Bloom'd》(不是全部!)
|
- Walt Whitman 的《当紫丁香在门廊绽放时》(不包括全部内容!)
|
||||||
|
|
||||||
所有这些都可以在网上全文阅读。请访问poetryfoundation.org,了解这些诗人、剧作家和作者的一些历史。
|
所有这些都可以在线全文获取。请访问 poetryfoundation.org 了解一些诗人、剧作家和作者的历史。
|
||||||
|
|
||||||
## 支持文件
|
## 支持文件
|
||||||
|
|
||||||
|
|||||||
BIN
content/zh-cn/posts/csci-1100/hw-7/HW7.zip
Normal file
BIN
content/zh-cn/posts/csci-1100/hw-7/HW7.zip
Normal file
Binary file not shown.
232
content/zh-cn/posts/csci-1100/hw-7/index.md
Normal file
232
content/zh-cn/posts/csci-1100/hw-7/index.md
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
---
|
||||||
|
title: CSCI 1100 - 作业 7 - 字典
|
||||||
|
subtitle:
|
||||||
|
date: 2024-09-12T15:36:47-04:00
|
||||||
|
slug: csci-1100-hw-7
|
||||||
|
draft: false
|
||||||
|
author:
|
||||||
|
name: James
|
||||||
|
link: https://www.jamesflare.com
|
||||||
|
email:
|
||||||
|
avatar: /site-logo.avif
|
||||||
|
description: 这篇博客文章概述了一个满分100分的家庭作业,截止日期为2024年3月28日,重点是Python字典操作。该作业包括两部分内容:自动更正程序和电影评分分析,都需要仔细处理数据文件和字典操作。
|
||||||
|
keywords: ["Python", "字典"]
|
||||||
|
license:
|
||||||
|
comment: true
|
||||||
|
weight: 0
|
||||||
|
tags:
|
||||||
|
- CSCI 1100
|
||||||
|
- 作业
|
||||||
|
- RPI
|
||||||
|
- Python
|
||||||
|
- 编程
|
||||||
|
categories:
|
||||||
|
- 编程语言
|
||||||
|
collections:
|
||||||
|
- CSCI 1100
|
||||||
|
hiddenFromHomePage: false
|
||||||
|
hiddenFromSearch: false
|
||||||
|
hiddenFromRss: false
|
||||||
|
hiddenFromRelated: false
|
||||||
|
summary: 这篇博客文章概述了一个满分100分的家庭作业,截止日期为2024年3月28日,重点是Python字典操作。该作业包括两部分内容:自动更正程序和电影评分分析,都需要仔细处理数据文件和字典操作。
|
||||||
|
resources:
|
||||||
|
- name: featured-image
|
||||||
|
src: featured-image.jpg
|
||||||
|
- name: featured-image-preview
|
||||||
|
src: featured-image-preview.jpg
|
||||||
|
toc: true
|
||||||
|
math: true
|
||||||
|
lightgallery: false
|
||||||
|
password:
|
||||||
|
message:
|
||||||
|
repost:
|
||||||
|
enable: false
|
||||||
|
url:
|
||||||
|
|
||||||
|
# See details front matter: https://fixit.lruihao.cn/documentation/content-management/introduction/#front-matter
|
||||||
|
---
|
||||||
|
|
||||||
|
<!--more-->
|
||||||
|
|
||||||
|
## 作业概述
|
||||||
|
|
||||||
|
该作业总分为100分,截止日期为2024年3月28日星期四晚上11:59:59。
|
||||||
|
|
||||||
|
本作业包含两部分,每部分各占50分。请下载`hw7_files.zip`并将其解压到你的HW7目录下。你将找到多个将在两个部分中使用的数据文件。
|
||||||
|
|
||||||
|
本次作业的目标是使用字典进行编程。在第一部分中,你需要做一些简单的文件处理工作,请仔细阅读指导说明。在第二部分中,我们已经为你完成了所有的文件操作,因此你应该只需几行代码就可以完成任务了。对于两部分中的每一部分,你将花费大部分时间来操作给定的各种文件中的字典。
|
||||||
|
|
||||||
|
请记得将你的文件命名为`hw7_part1.py`和`hw7_part2.py`。
|
||||||
|
|
||||||
|
一如既往,请确保遵循程序结构的指导方针。我们将根据程序正确性和良好的程序结构进行评分。
|
||||||
|
|
||||||
|
同样要提醒的是,我们会继续对作业相似度进行检测。因此,请务必遵守我们关于合作程度的规定。如果你需要复习这些规定,可以从Submitty课程资源部分下载指南。请注意,这包括使用以前学期某位同学的代码。请确保你提交的代码确实是你自己写的。
|
||||||
|
|
||||||
|
### 荣誉声明
|
||||||
|
|
||||||
|
在家庭作业中出现了许多学术不端行为,这种情况必须改变。利用自动化工具很容易发现这些案件,并由讲师验证。这种做法会导致严重的分数扣减、不良的学习效果、挫折感以及浪费所有相关人员宝贵的资源时间。为了缓解这一问题,以下是课程诚信政策的重申形式:
|
||||||
|
|
||||||
|
- 我没有向本班级中的任何人展示我的代码,特别是用于指导他们自己的工作。
|
||||||
|
- 我没有复制另一位同学或以前学期学生的代码(无论是修改过的还是未修改过的)。
|
||||||
|
- 我没有使用在网络上找到或购买的解决方案完成这项作业。
|
||||||
|
- 我提交的工作是我自己写的,并且我完全理解并遵守这一承诺
|
||||||
|
- 如果我发现违反了上述任何一条,将在这次作业中得到零分,并额外扣除10分总成绩。
|
||||||
|
|
||||||
|
在你提交你的家庭作业之前,你需要同意这些单独声明。
|
||||||
|
|
||||||
|
请明白如果你是绝大多数遵循规则的学生之一——只是与其他同学一起了解问题描述、Python结构和解决方案方法——那么你就不会遇到任何麻烦。
|
||||||
|
|
||||||
|
## 第一部分:自动更正
|
||||||
|
|
||||||
|
我们所有人都使用过自动更正来修正我们在写作时的各种拼写错误,但你是否想过它是如何工作的呢?这里有一个小型的自动更正程序,它会查找一些常见的打字错误。
|
||||||
|
|
||||||
|
要解决这个问题,你的程序将读取三个文件的名字:
|
||||||
|
|
||||||
|
- 第一个包含有效单词及其频率的列表,
|
||||||
|
- 第二个包含需要自动更正的单词列表,
|
||||||
|
- 第三个包含可能的字母替换(如下所述)。
|
||||||
|
|
||||||
|
输入单词文件每行有两个条目;第一项是单个有效的英文单词,第二项是一个表示该词在词汇表中的频次的浮点数。这两个值之间用逗号分隔。
|
||||||
|
|
||||||
|
读取这个英语字典并将其转换为Python字典:使用单词作为键和频率作为值。你将利用这些频率来决定多个可能更正中最有可能的一个。
|
||||||
|
|
||||||
|
键盘文件每一行对应一个字母,第一项是需要替换的字母,其余的是该字母可能的替换选项。这一行中的所有字母之间用空格分隔。这些替代方案基于键盘上的相邻关系计算得出,所以如果你查看一下你的键盘,会发现“a”键周围有“q”,“w”,“s”,和“z”。其他替代方案也是类似方式计算出来的,例如:
|
||||||
|
|
||||||
|
```text
|
||||||
|
b v f g h n
|
||||||
|
```
|
||||||
|
|
||||||
|
这意味着`b`的可能替换可以是`v`, `f`, `g`, `h`, 或者 `n`。读取这个键盘文件并将其转换为字典:第一个字母作为键(如 b),其余字母作为值,存储为列表。
|
||||||
|
|
||||||
|
你的程序将遍历输入文件中的每一个单词,对每个单词进行自动更正,并打印出更正结果。为了修正一个单独的单词,你将考虑以下情况:
|
||||||
|
|
||||||
|
- **找到**:如果这个单词在字典中,它就是正确的。不需要任何更改。将其打印为“找到”,然后继续下一个单词。
|
||||||
|
- 否则,请考虑所有剩余的可能性。
|
||||||
|
|
||||||
|
- **删除**:如果没有找到该词,则考虑从该词中删除一个字母的所有可能方式。将任何有效单词(即在你的英文字典中的单词)存储在一个容器(列表/集合/字典)中,这些将是候选更正。
|
||||||
|
- **插入**:如果该词不存在,则考虑向该词插入一个单个字母的所有可能方式。将所有有效单词存储在一个容器(列表/集合/字典)中,这些将是候选更正。
|
||||||
|
- **交换**:考虑从该词交换两个连续字母的所有方式。将任何有效单词存储在一些容器(列表/集合/字典)中,这些将是候选更正。
|
||||||
|
- **替换**:接下来,考虑使用键盘文件中存在的可能替换对单个字母进行更改的所有可能性。将所有有效单词存储在一个容器(列表/集合/字典)中,这些将是候选更正。
|
||||||
|
|
||||||
|
例如,对于给定的键盘文件来说,`b` 的可能替换为 `v f g h n`。因此,如果你在 `abar` 中替换 `b`,你应该考虑:`avar`, `afar`, `agar`, `ahar`, `anar`。
|
||||||
|
|
||||||
|
经过上述所有步骤后,如果有多于一个潜在匹配,则按其在英语字典中的频率进行排序,并返回使用最频繁的前三个值作为最可能的更正结果。如果有三或更少个潜在匹配,请按顺序打印它们。在这种不太可能发生的情况下,两个单词基于频率相等时,你应该选择字典序靠后的那个。
|
||||||
|
|
||||||
|
如果没有任何上述更正方法的有效匹配,则打印“未找到”。否则,在一行中打印该词(15个空格),匹配的数量,并且最多三个匹配项。
|
||||||
|
|
||||||
|
你程序的一个示例输出包含在文件 `part1_output_01.txt` 中。注意,我们在Submitty上将使用一个更广泛的字典,因此你的结果可能与你在笔记本电脑上的不同。
|
||||||
|
|
||||||
|
当你确定作业正确无误后,请将其提交到 Submitty。你的程序必须命名为 `hw7_part1.py` 才能正常工作。
|
||||||
|
|
||||||
|
### 注意事项:
|
||||||
|
|
||||||
|
1. 不要写循环来搜索字符串(单词或字母)是否在字典中!这将非常慢,并可能导致Submitty终止你的程序(并且你将失去大量分数)。相反,你应该使用 `in` 操作符。
|
||||||
|
2. 可能,但不太可能,候选替换词会被生成多次。我们建议你在查找之前先将所有可能的候选替换存储在一个集合中。
|
||||||
|
3. 通过频率排序潜在匹配项可以简单处理。对于每个潜在匹配项,创建一个元组,首先包含频率值,然后是单词。将其添加到列表中并按反向顺序对列表进行排序。例如,如果列表为 `v`,则只需使用代码:`v.sort(reverse=True)`
|
||||||
|
|
||||||
|
## 第二部分:评分高和低的电影...
|
||||||
|
|
||||||
|
在这个部分,我们将提供两个数据文件 `movies.json` 和 `ratings.json`,格式是 JSON 数据格式。第一个数据文件是从 IMDB 直接获取的电影信息,包括一些但不是所有电影的评分。第二个文件包含来自 Twitter 的评分。
|
||||||
|
|
||||||
|
请注意:并非所有的电影在 `movies.json` 中都有对应的评级,也并非所有的电影在 `ratings.json` 中都有相关的电影信息。
|
||||||
|
|
||||||
|
这些数据可以通过以下五行代码完整读取:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
movies = json.loads(open("movies.json").read())
|
||||||
|
ratings = json.loads(open("ratings.json").read())
|
||||||
|
```
|
||||||
|
|
||||||
|
两个文件都以字典的形式存储数据。第一个字典使用电影 ID 作为键,值是一个包含电影属性列表的第二个字典。例如:
|
||||||
|
|
||||||
|
```python
|
||||||
|
print(movies['3520029'])
|
||||||
|
(movie with id '3520029') produces the output:
|
||||||
|
{'genre': ['Sci-Fi', 'Action', 'Adventure'], 'movie_year': 2010,
|
||||||
|
'name': 'TRON: Legacy', 'rating': 6.8, 'numvotes': 254865}
|
||||||
|
```
|
||||||
|
|
||||||
|
这相当于:
|
||||||
|
|
||||||
|
```python
|
||||||
|
movies = dict()
|
||||||
|
movies['3520029'] = {'genre': ['Sci-Fi', 'Action', 'Adventure'],
|
||||||
|
'movie_year': 2010, 'name': 'TRON: Legacy',
|
||||||
|
'rating': 6.8, 'numvotes': 254865}
|
||||||
|
```
|
||||||
|
|
||||||
|
如果我们想获取每个电影的单独信息,可以使用以下命令:
|
||||||
|
|
||||||
|
```python
|
||||||
|
print(movies['3520029']['genre'])
|
||||||
|
print(movies['3520029']['movie_year'])
|
||||||
|
print(movies['3520029']['rating'])
|
||||||
|
print(movies['3520029']['numvotes'])
|
||||||
|
```
|
||||||
|
|
||||||
|
这将提供以下输出:
|
||||||
|
|
||||||
|
```python
|
||||||
|
['Sci-Fi', 'Action', 'Adventure']
|
||||||
|
2010
|
||||||
|
6.8
|
||||||
|
254865
|
||||||
|
```
|
||||||
|
|
||||||
|
第二个字典同样使用电影 ID 作为键,值是一个评分列表。例如,
|
||||||
|
|
||||||
|
```python
|
||||||
|
print(ratings['3520029'])
|
||||||
|
(movie with id '3520029') produces the output:
|
||||||
|
[6, 7, 7, 7, 8]
|
||||||
|
```
|
||||||
|
|
||||||
|
因此,这部电影有五个评分:6、7、7、7 和 8。
|
||||||
|
|
||||||
|
现在开始作业:
|
||||||
|
|
||||||
|
### 问题描述
|
||||||
|
|
||||||
|
在这个家庭作业中,假设你有两个名为 `movies.json` 和 `ratings.json` 的文件。请从这些文件中读取数据,并让用户输入一个年份范围(最小和最大年份)以及两个权重:`w1` 和 `w2`。找到在 min 到 max 年之间制作的所有电影(包含 min 和 max)。对于每部电影,计算其综合评分为:
|
||||||
|
|
||||||
|
$$
|
||||||
|
(w1 \times imdb_rating + w2 \times average_twitter_rating) / (w1 + w2)
|
||||||
|
$$
|
||||||
|
|
||||||
|
其中 `imdb_rating` 来自于 movies 文件,而 `average_twitter_rating` 是 ratings 文件中的平均评分。
|
||||||
|
|
||||||
|
如果某部电影没有在 Twitter 上被评分,或者其 Twitter 评分少于三个条目,则跳过该电影。然后反复询问用户输入一个电影类型,并返回该类型的最佳和最差电影(基于给定年份和计算出的评分)。重复此操作直到用户输入“stop”。
|
||||||
|
|
||||||
|
程序运行的一个示例(在你使用 Spyder 运行时的样子)包含在文件 `hw7_part2_output_01.txt` 中(每部电影第二行有8个空格,评分为 `{:.2f}` 格式)。
|
||||||
|
|
||||||
|
我们提供的用于测试的电影是一个子集,在 Submitty 上进行测试时会有所不同,所以当你提交作业时可能会看到不同之处。
|
||||||
|
|
||||||
|
当你确定你的程序正确无误后,请将其提交到 Submitty。你的程序必须命名为 `hw7_part2.py` 才能正常工作。
|
||||||
|
|
||||||
|
### 排序的一般提示
|
||||||
|
|
||||||
|
有可能两部电影的评分相同。考虑以下代码:
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> example = [(1, "b"), (1, "a"), (2, "b"), (2, "a")]
|
||||||
|
>>> sorted(example)
|
||||||
|
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
|
||||||
|
>>> sorted(example, reverse=True)
|
||||||
|
[(2, 'b'), (2, 'a'), (1, 'b'), (1, 'a')]
|
||||||
|
```
|
||||||
|
|
||||||
|
注意,排序是基于索引 0 的值进行的,但如果出现并列情况,则根据索引 1 的值来决定。如果在索引 0 和索引 1 都有并列时,排序将继续使用索引 2 的值(如果有)以此类推。对列表列表也是一样的关系。
|
||||||
|
|
||||||
|
为了确定最佳和最差电影,示例代码中用了一个将评分放在索引 0 处、名字放在索引 1 处的排序方法。在你决定最佳和最差电影时,请记住这一点。
|
||||||
|
|
||||||
|
## 支持文件
|
||||||
|
|
||||||
|
{{< link href="HW7.zip" content="HW7.zip" title="Download HW7.zip" download="HW7.zip" card=true >}}
|
||||||
|
|
||||||
|
## 解决方案
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> 我在这个作业中没有得到满分(只有 96%),所以我没有发布解决方案。我可能会重新做一遍以获得满分,之后会将它添加在这里。
|
||||||
@@ -50,61 +50,61 @@ repost:
|
|||||||
|
|
||||||
## 概述
|
## 概述
|
||||||
|
|
||||||
这份作业占你总作业成绩的100分,截止日期是2024年4月18日星期四晚上11:59:59。它分为三个部分。前两部分分值不高,甚至可能不计分。它们主要是为了给你提供信息,帮助你调试解决方案。请下载 `hw8_files.zip` 并将其解压到你的HW8目录中。你会在每个部分中找到数据文件和示例输出。
|
本作业满分 100 分,并将于2024年4月18日星期四晚上11:59:59截止。它包含三个部分,前两个部分的分值不高,最终可能为零分,主要是为了提供调试信息。请下载 `hw8_files.zip` 并将其解压到你的 HW8 目录中。你会找到每个部分的数据文件和示例输出。
|
||||||
|
|
||||||
本次作业的目标是使用类进行编程。你将被要求编写一个模拟引擎,并使用类来封装数据和功能。你将有许多设计选择要做。虽然我们之前做过模拟,但这次会更复杂。特别重要的是,你要慢慢开始,构建一个适用于简单情况的程序,测试它,然后增加复杂性。我们将提供难度递增的测试用例。确保你慢慢开发并彻底测试。
|
本作业的目标是使用类进行编程。你将被要求编写一个模拟引擎,并使用类封装数据和功能。你有许多设计选择要做出,虽然我们以前做过类似的模拟,但这次的难度更高。特别重要的是你要慢慢来,先建立适用于简单情况的程序并测试它,然后再增加复杂性。我们将提供逐渐困难程度不同的测试用例,请确保你逐步开发并彻底测试。
|
||||||
|
|
||||||
## 提交说明
|
## 提交说明
|
||||||
|
|
||||||
在这份作业中,你将首次向Submitty提交多个文件,这些文件共同组成一个程序。请仔细遵循以下说明。
|
在本作业中,你第一次需要提交多个文件到 Submitty 以构成一个完整的程序。请仔细遵循以下指示。
|
||||||
|
|
||||||
每个部分(第一部分、第二部分和第三部分)都需要你编写一个主程序:`hw8_part1.py`、`hw8_part2.py` 和 `hw8_part3.py`。除了主文件外,你还必须为每个部分提交三个模块,每个模块封装一个类。第一个是名为 `BerryField.py` 的文件,包含你的 `BerryField` 类;第二个是名为 `Bear.py` 的文件,包含你的 `Bear` 类;第三个是名为 `Tourist.py` 的文件,包含你的 `Tourist` 类。
|
每部分(第一部分、第二部分和第三部分)都需要编写主程序:`hw8_part1.py`、`hw8_part2.py` 和 `hw8_part3.py`。此外,你还需要为每个部分提交三个模块文件,其中每一个封装一个类。第一个是名为 `BerryField.py` 的文件,包含你的 BerryField 类;第二个是名为 `Bear.py` 的文件,包含你的 Bear 类;第三个是名为 `Tourist.py` 的文件,包含你的 Tourist 类。
|
||||||
|
|
||||||
一如既往,确保你遵循程序结构指南。你将根据良好的程序结构和程序正确性进行评分。
|
一如既往,请确保你遵循程序结构的指导原则。我们将根据良好的程序结构和程序正确性进行评分。
|
||||||
|
|
||||||
同样要记住,我们将继续测试作业的相似性。因此,请遵循我们关于可接受合作水平的指南。如果你需要复习,可以从课程材料中的资源部分下载指南。我们对此非常认真,必要时会毫不犹豫地施加处罚。
|
请记住我们还将继续检查作业相似度。因此,请务必遵守合作指南中的可接受的合作程度。如果你需要复习,请从课程材料资源部分下载这些指南。我们对此非常重视,并且在必要时会毫不犹豫地施加处罚。
|
||||||
|
|
||||||
## 开始
|
## 开始
|
||||||
|
|
||||||
你需要为本次作业编写至少三个类,分别对应于 `BerryField`、`Bear` 和 `Tourist`。我们将给你很大的自由来组织这三个类,但每个类必须至少有一个初始化方法和一个字符串方法。其他方法由你决定。每个类描述如下。
|
你将需要为此作业编写至少三个类,分别对应 BerryField、Bear 和 Tourist。我们将给你很大的自由度来组织这三个类,但每个类必须有一个初始化器和一个字符串方法。其他的方法由你自己决定。每个类的描述如下:
|
||||||
|
|
||||||
### BerryField
|
### BerryField
|
||||||
|
|
||||||
`BerryField` 类必须维护和管理浆果的位置,作为一个方形行 X 列的网格,(0,0) 是左上角,(N-1, N-1) 是右下角。每个空间包含 0-10 个浆果单位。
|
`BerryField` 类必须维护并管理浆果的位置作为包含 (0, 0) 的左上角和 (N-1, N-1) 的右下角的行 X 列网格。每个位置可以容纳 0-10 单位的浆果。
|
||||||
|
|
||||||
- 初始化类必须至少能够接收一个值网格(想想我们的数独实验),并使用它创建一个包含网格中值的浆果田。
|
- 初始化器类至少需要能够接收一个值网格(考虑我们之前的数独实验),并使用它来创建一个包含该网格中值的浆果田。
|
||||||
- 字符串函数必须至少能够生成当前浆果田状态的字符串。网格中的每个块必须使用 `"{:>4}"` 格式说明符进行格式化。如果该位置有熊,网格应显示 `"B"`;如果有游客,网格应显示 `"T"`;如果既有熊又有游客,网格应显示 `"X"`。如果既没有熊也没有游客,它应显示该位置的浆果数量。
|
- 字符串方法至少需要能够生成当前浆果田的状态字符串。每个网格块必须以 `"{:>4}"` 格式化,如果有熊在位置上,则网格应该有一个 "B";如果有游客在位置上,则网格应该有一个 "T";如果同时有熊和游客,则网格应该有一个 "X"。如果没有熊或游客,则应该是该位置的浆果数量。
|
||||||
- 浆果会生长。`BerryField` 类必须提供一种方法来生长浆果田。当浆果生长时,任何值为 `1 <= 浆果数量 < 10` 的位置将增加一个浆果。
|
- 浆果会生长。`BerryField` 类必须提供一种方式来使浆果田生长。当浆果生长时,任何值为 `1 <= number of berries < 10` 的位置将增加一个浆果单位。
|
||||||
- 浆果也会扩散。任何没有浆果且与有10个浆果的位置相邻的位置,在生长操作期间将获得1个浆果。
|
- 浆果也会扩散。任何没有浆果且相邻于有 10 单位浆果的位置在生长操作期间会获得一个浆果。
|
||||||
|
|
||||||
### Bear
|
### Bear
|
||||||
|
|
||||||
每只熊都有一个位置和一个它们正在行走的方向。熊也非常饥饿。在你的程序中,你必须管理两组熊。第一组是当前在田野中行走的熊。第二组是等待进入田野的熊队列。
|
每个熊都有一个位置和行走的方向。熊也总是很饿。你的程序必须管理两个熊列表:第一个包含当前正在田中行走的熊,第二个是等待进入田野的熊队列。
|
||||||
|
|
||||||
- 初始化类必须至少能够接收行和列位置以及行进方向。
|
- 初始化器类至少需要能够接收行、列位置以及旅行方向。
|
||||||
- 字符串函数必须至少能够打印出熊的位置、行进方向以及熊是否在睡觉。
|
- 字符串方法至少需要能够打印出熊的位置和行走方向,并且如果熊在睡觉,则显示这一点。
|
||||||
- 熊可以向 `北 (N)`、`南 (S)`、`东 (E)`、`西 (W)`、`东北 (NE)`、`西北 (NW)`、`东南 (SE)` 或 `西南 (SW)` 行走。一旦熊开始向某个方向行走,它永远不会转弯。
|
- 熊可以向北 (N)、南 (S)、东 (E)、西 (W)、东北 (NE)、西北 (NW)、东南 (SE) 或西南 (SW) 方向行走。一旦一只熊开始朝一个方向行走,它就不会改变方向。
|
||||||
- 熊总是饥饿的。每回合,除非同一位置有游客,否则熊会吃掉该空间中所有可用的浆果,然后沿着当前方向移动到下一个空间。这种情况会持续到当前回合结束,直到熊吃了30个浆果或遇到游客。
|
- 熊总是很饿。每个回合,除非同一位置上有游客,否则熊会吃掉该空间的所有浆果,并然后朝其当前方向移动到下一个空间。在本回合中,这将一直持续直到熊吃了 30 单位的浆果或遇到一个游客为止。
|
||||||
- 对于熊和游客在同一位置的特殊情况,熊不会吃任何浆果,但游客会神秘消失,熊会睡三回合。
|
- 对于熊和游客在同一地点的情况,在同一个回合内,熊不会吃任何浆果,但游客神秘地消失了并且熊会在三个回合后睡觉。
|
||||||
- 一旦熊到达田野的边界(其行或列变为-1或N),它就不再在田野中行走,不再需要考虑。
|
- 当一只熊达到田野边界(其行或列变为 -1 或 N)时,它将不再在田中行走,并且不需要再考虑。
|
||||||
|
|
||||||
### Tourist
|
### Tourist
|
||||||
|
|
||||||
每个游客都有一个位置。与熊一样,你必须维护当前在田野中的游客列表和等待进入田野的游客队列。
|
每个游客都有一个位置。就像熊一样,你必须维护一个当前在田野中的游客列表和等待进入田野的游客队列。
|
||||||
|
|
||||||
- 初始化类必须至少能够接收行和列位置。
|
- 初始化器类至少需要能够接收行、列位置。
|
||||||
- 如果熊在游客当前位置的4个单位内,游客会看到熊。
|
- 游客可以看到距离他们当前位置 4 范围内的熊。
|
||||||
- 字符串函数必须至少能够打印出游客的位置以及自上次看到熊以来经过的回合数。
|
- 字符串方法至少需要能够打印出游客的位置以及自他上次看到熊以来经过的回合数。
|
||||||
- 游客会站着观看。他们不会移动,但如果以下情况发生,他们会离开田野:
|
- 游客站着观看。他们不会移动,但他们会离开田野的情况如下:
|
||||||
1. 三回合内没有看到熊;他们会感到无聊并回家。
|
- 如果三个回合没有看到熊,则他们会感到无聊并回家。
|
||||||
2. 他们同时看到三只熊;他们会感到害怕并回家。
|
- 如果他们同时可以看到三只熊,则会被吓跑并回家。
|
||||||
3. 一只熊撞到他们;他们会神秘消失,不再出现在田野中。
|
- 如果一只熊撞到他们,游客会神秘消失并且再也无法在田野中找到。
|
||||||
|
|
||||||
## 执行
|
## 执行
|
||||||
|
|
||||||
记得从Submitty的课程材料部分获取 `hw8_files_F19.zip`。它包含两个示例输入文件和你的程序的预期输出。
|
请记住从 Submitty 的课程材料部分获取 `hw8_files_F19.zip`。它包含两个示例输入文件和你程序的预期输出。
|
||||||
|
|
||||||
对于这份作业,初始化你的类和程序所需的所有数据都可以在JSON文件中找到。你的每个部分都应该首先询问JSON文件的名称,读取文件,然后根据读取的数据创建所需的对象。以下代码将帮助你完成这一过程。
|
对于本作业,所需的所有初始化类和程序的数据都可以在 JSON 文件中找到。每个部分应该首先询问 JSON 文件名、读取该文件并根据所读数据创建所需的对象。下面的代码将帮助你完成此操作。
|
||||||
|
|
||||||
```python
|
```python
|
||||||
f = open("bears_and_berries_1.json")
|
f = open("bears_and_berries_1.json")
|
||||||
@@ -116,39 +116,39 @@ print(data["active_tourists"])
|
|||||||
print(data["reserve_tourists"])
|
print(data["reserve_tourists"])
|
||||||
```
|
```
|
||||||
|
|
||||||
你会看到,田野是一个列表的列表,其中每个 `[row][column]` 值是该位置的浆果数量;`"active_bears"` 和 `"reserve_bears"` 条目是定义熊的三元组 `(row, column, direction)` 的列表;`"active_tourists"` 和 `"reserve_tourists"` 条目是定义游客的二元组 `(row, column)` 的列表。
|
你会看到田野是一个列表中的列表,其中每个 `[row][column]` 值是该位置的浆果数量;"active_bears" 和 "reserve_bears" 条目是定义熊的三元组 `(row, column, direction)` 的列表;以及 "active_tourists" 和 "reserve_tourists" 条目是定义游客的二元组 `(row, column)` 的列表。
|
||||||
|
|
||||||
## 第一部分
|
## 第一部分
|
||||||
|
|
||||||
在第一部分中,读取JSON文件,创建你的对象,然后简单地报告模拟的初始状态,打印出浆果田、活跃的熊和活跃的游客。将你的程序命名为 `hw8_part1.py`,并连同你开发的三个类一起提交。
|
在第一部分中,读取 JSON 文件、创建你的对象,并简单报告初始模拟状态,打印浆果田、活跃熊和活跃游客。将程序命名为 `hw8_part1.py` 并与你开发的三个类一起提交。
|
||||||
|
|
||||||
## 第二部分
|
## 第二部分
|
||||||
|
|
||||||
在第二部分中,首先读取JSON文件,创建你的对象,再次打印出模拟的初始状态。然后运行五回合的模拟,步骤如下:
|
在第二部分中,先同上一样读取 JSON 文件、创建你的对象并再次打印初始模拟状态。然后运行五回合的模拟通过:
|
||||||
|
|
||||||
- 生长浆果
|
- 使浆果生长
|
||||||
- 移动熊
|
- 移动熊
|
||||||
- 检查游客
|
- 检查游客情况
|
||||||
- 打印出模拟状态
|
- 打印模拟的状态
|
||||||
|
|
||||||
不要担心备用熊或备用游客进入田野,但要报告任何离开的游客或熊。将你的程序命名为 `hw8_part2.py`,并连同你开发的三个类一起提交。
|
不用担心储备熊和储备游客进入田野,但报告离开的任何游客或熊。将程序命名为 `hw8_part2.py` 并与你开发的三个类一起提交。
|
||||||
|
|
||||||
## 第三部分
|
## 第三部分
|
||||||
|
|
||||||
在第三部分中,做你在第二部分中所做的一切,但做以下更改:
|
在第三部分中,做第二部分的所有事情,并进行以下更改:
|
||||||
|
|
||||||
- 在检查游客后,如果备用队列中仍有熊且至少有500个浆果,将下一只备用熊添加到活跃熊中。
|
- 在检查完游客之后,如果有剩余的储备队列中的熊且至少有 500 单位浆果,则将下一个储备熊添加到活跃熊列表。
|
||||||
- 然后,如果备用队列中仍有游客且至少有一只活跃的熊,将下一只备用游客添加到田野中。
|
- 然后,如果仍有等待进入田野的游客且至少有一个活跃熊,则将下一个储备游客添加到田野中。
|
||||||
- 不要在5回合后停止,而是运行直到田野中没有更多的熊且备用列表中没有更多的熊,或者田野中没有更多的熊且没有更多的浆果。
|
- 不再在五个回合后停止,而是运行直到没有更多的熊在田里并且没有剩余的储备队列中的熊,或者如果没有更多的熊在田野上并且没有浆果了。
|
||||||
- 最后,不要每回合报告状态,而是每5回合报告一次,并在模拟结束时再次报告。
|
- 最终,在每五个回合报告一次状态,并且当模拟结束时再次报告。
|
||||||
|
|
||||||
在过程中,报告任何离开或进入田野的游客或熊。将你的程序命名为 `hw8_part3.py`,并连同你开发的三个类一起提交。
|
在整个过程中,报告离开或进入田野的任何游客或熊。将程序命名为 `hw8_part3.py` 并与你开发的三个类一起提交。
|
||||||
|
|
||||||
## 支持文件
|
## 支持文件
|
||||||
|
|
||||||
{{< link href="HW8.zip" content="HW8.zip" title="下载 HW8.zip" download="HW8.zip" card=true >}}
|
{{< link href="HW8.zip" content="HW8.zip" title="Download HW8.zip" download="HW8.zip" card=true >}}
|
||||||
|
|
||||||
## 解决方案
|
## 解决方案
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> 我没有在这份作业中获得满分,所以我没发布解决方案。我可能会重做以获得满分解决方案。之后,我会在这里添加它。
|
> 我在这个作业中没有得到满分,所以我没有发布解决方案。我会重新做以获得满分的解决方案,并在此之后添加。
|
||||||
Reference in New Issue
Block a user