这个作业属于哪个课程 https://edu.cnblogs.com/campus/zjlg/rjjc
这个作业的目标 实现一个命令行文本计数统计程序,能正确统计导入的txt文本中的字符数、单词数、句子数和导入的程序文件中的代码行、空行、注释行等
姓名-学号 金乐雨 2022329301145
作业码云地址: https://gitee.com/jinleyu/second-assignment1
  • 作业要求
    实现一个命令行文本计数统计程序。能正确统计导入的纯英文txt文本中的字符数,单词数,句子数。
    具体命令行界面要求举例:

    命令模式: wc.exe [参数] [文件名]
    wc.exe -c file.txt 统计字符数
    wc.exe -w file.txt 统计单词数
    扩展功能(加分项):统计代码行、空行、注释行等,并提供相应命令接口。
    作业要求:

项目代码上传到自己的码云(需自行注册)。

程序和参数名可以按个人喜好命名。项目文档应包含项目说明,写入README.MD。该说明应该包含:

a. 项目的简介及其相关的用法;

b. 文件列表及其相关说明;

c. 例程运行及其相关结果。

码云上传的项目要求必须通过多次提交(commit)体现各个版本更迭和运行结果截图。

版本号分别为

v0.1 空项目;

v0.2 项目完成基础功能

v0.3 项目完成扩展功能(加分项)

编写对应的单元测试。

a. 实现基本功能的测试;

b. 单元测试包含更多的测试用例。(加分项)

使用选用的IDE附带的工具进行performance test。(加分项)

交作业时需直接附上自己的码云仓库地址。

  • README
    项目简介
    基础功能:统计文本中的字符数、单词数、句子数
    拓展功能:统计代码行、空行、注释行
    功能使用表
    python v0.3.py -c note.txt #统计字母数量
    python v0.3.py -c note.txt #统计字母数量
    python v0.3.py -w note.txt #统计单词数量
    python v0.3.py -s note.txt #统计句子数量
    python v0.3.py -l note.txt #统计代码行数量
    python v0.3.py -e note.txt #统计空白行数量

    • 完整代码
点击查看代码
import sys
import re



def count_chars(filename):
    with open(filename, 'r') as file:
        text = file.read()
        return len(text)

def count_words(filename):
    with open(filename, 'r') as file:
        text = file.read()
        words = re.findall(r'\w+', text)
        return len(words)

def count_sentences(filename):
    with open(filename, 'r') as file:
        text = file.read()
        sentences = re.split(r'[.!?]+', text)
        return len([s for s in sentences if s.strip()])

def count_code_lines(filename):
    with open(filename, 'r') as file:
        lines = file.readlines()
        return sum(1 for line in lines if line.strip().startswith('#'))

def count_empty_lines(filename):
    with open(filename, 'r') as file:
        lines = file.readlines()
        return sum(1 for line in lines if not line.strip())

def main():
    if len(sys.argv) != 3:
        print("Usage: wc.py [option] [filename]")
        return

    option = sys.argv[1]
    filename = sys.argv[2]

    if option == '-c':
        print("Character count:", count_chars(filename))
    elif option == '-w':
        print("Word count:", count_words(filename))
    elif option == '-s':
        print("Sentence count:", count_sentences(filename))
    elif option == '-l':
        print("Code line count:", count_code_lines(filename))
    elif option == '-e':
        print("Empty line count:", count_empty_lines(filename))
    else:
        print("Invalid option")

if __name__ == '__main__':
    main()

  • 测试文本(农夫与蛇)
点击查看代码
his story is called "The Farmer and the Snake." Every day, 
a farmer went to the city to sell his flowers and farm produce
 and then went home after selling all his things. One day, he
  left home very early, so early that when he arrived at the 
  city, the gate was still closed. So he lay down to take a 
  nap, when he awoke he found that the storage bin containing
   his farm produce had become empty except that there was a 
   gold coin inside. Although all the things in the bin had 
   vanished, the gold was much more valuable so he was still
    very happy. He thought most probably someone had taken 
    his things and left the payment there, and went home 
    happily with the money.

The next day, the farmer again went out to sell his things
 but again arrived too early so he slept outside the city 
 gate just like he had done the day before. And the incident
  repeated itself. All the produce that the farmer had brought 
  disappeared, and there was a gold coin in the bin! At that 
  time, gold was very valuable. One gold coin had a value many
   times higher than the farmer's produce.

Then one day some time later, the farmer's father asked him,
 "Where have you gotten so many gold coins lately? Where did
  the money come from?" So the farmer revealed to his father
   what had happened. After listening to his story, the father
    thought, "One day I'll follow my son as he goes out, and
     see who's been consuming his things and leaving the money
      in the bin."

So one day, when the farmer went out to do business, his father
 quietly followed him. While the farmer was sleeping near the
  city gate, his father saw a snake crawl up to his bin and 
  eat his produce. Having finished eating, the snake once again
   spat a gold coin into the bin as payment to the farmer before
    leaving. Seeing this, the farmer's father thought, "If I kill 
    the snake, I'll be able to seize all its gold coins!" He then 
    picked up a stone and cut the snake into two parts.

At that time, the head and trunk of the snake were already in 
its den, and only the tail was outside. The father thought that 
there must be a big treasure in the den so he told his son to 
reach inside for it. But unexpectedly, when the farmer reached 
his hand into the den, the snake bit and killed him! Even though
 it had been cut into two, the snake could still bite! I understand
  how this could have happened because that was how I got bitten 
  when I was small. At that time, I saw a centipede that had been 
  beaten and squashed by someone, leaving only the head intact. I 
  thought it was dead, and to make sure, I poked it with my foot. 
  It bit me hard and I cried for three days. (Laughter) I was really
   dumb! Remember! Don't play with a centipede even if it looks dead.
    Sometimes it just fakes death; it's not really dead.
  • 输入指令
点击查看代码
python v0.3.py -c note.txt
python v0.3.py -w note.txt
python v0.3.py -s note.txt
python v0.3.py -l note.txt
python v0.3.py -e note.txt
  • 运行结果

Performance Test
对代码运行的时间进行测试,采用python中time包来进行对时间的读取与计时,并且在结果中显示出来。

点击查看代码
import sys
import re
import time

def read_file(filename):
    with open(filename, 'r', encoding='utf-8') as file:
        return file.read()

def count_characters(filename):
    content = read_file(filename)
    return len(content)

def count_words(filename):
    content = read_file(filename)
    words = re.findall(r'\b\w+\b', content)
    return len(words)

def count_sentences(filename):
    content = read_file(filename)
    sentences = re.split(r'[.!?]+', content)
    return len([s for s in sentences if s.strip()])

def main():
    if len(sys.argv) != 3:
        print("Usage: wc.py [option] [filename]")
        sys.exit(1)

    option = sys.argv[1]
    filename = sys.argv[2]

    start_time = time.time()  # 开始时间

    if option == '-c':
        result = count_characters(filename)
    elif option == '-w':
        result = count_words(filename)
    elif option == '-s':
        result = count_sentences(filename)
    else:
        print("Invalid option. Use -c for character count, -w for word count, -s for sentence count.")
        return

    end_time = time.time()  # 结束时间
    print(f"Result: {result}")
    print(f"Execution time: {end_time - start_time:.6f} seconds")

if __name__ == "__main__":
    main()

  • 测试文本(农夫与蛇)
点击查看代码
his story is called "The Farmer and the Snake." Every day, 
a farmer went to the city to sell his flowers and farm produce
 and then went home after selling all his things. One day, he
  left home very early, so early that when he arrived at the 
  city, the gate was still closed. So he lay down to take a 
  nap, when he awoke he found that the storage bin containing
   his farm produce had become empty except that there was a 
   gold coin inside. Although all the things in the bin had 
   vanished, the gold was much more valuable so he was still
    very happy. He thought most probably someone had taken 
    his things and left the payment there, and went home 
    happily with the money.

The next day, the farmer again went out to sell his things
 but again arrived too early so he slept outside the city 
 gate just like he had done the day before. And the incident
  repeated itself. All the produce that the farmer had brought 
  disappeared, and there was a gold coin in the bin! At that 
  time, gold was very valuable. One gold coin had a value many
   times higher than the farmer's produce.

Then one day some time later, the farmer's father asked him,
 "Where have you gotten so many gold coins lately? Where did
  the money come from?" So the farmer revealed to his father
   what had happened. After listening to his story, the father
    thought, "One day I'll follow my son as he goes out, and
     see who's been consuming his things and leaving the money
      in the bin."

So one day, when the farmer went out to do business, his father
 quietly followed him. While the farmer was sleeping near the
  city gate, his father saw a snake crawl up to his bin and 
  eat his produce. Having finished eating, the snake once again
   spat a gold coin into the bin as payment to the farmer before
    leaving. Seeing this, the farmer's father thought, "If I kill 
    the snake, I'll be able to seize all its gold coins!" He then 
    picked up a stone and cut the snake into two parts.

At that time, the head and trunk of the snake were already in 
its den, and only the tail was outside. The father thought that 
there must be a big treasure in the den so he told his son to 
reach inside for it. But unexpectedly, when the farmer reached 
his hand into the den, the snake bit and killed him! Even though
 it had been cut into two, the snake could still bite! I understand
  how this could have happened because that was how I got bitten 
  when I was small. At that time, I saw a centipede that had been 
  beaten and squashed by someone, leaving only the head intact. I 
  thought it was dead, and to make sure, I poked it with my foot. 
  It bit me hard and I cried for three days. (Laughter) I was really
   dumb! Remember! Don't play with a centipede even if it looks dead.
    Sometimes it just fakes death; it's not really dead.
  • 输入指令
    python v0.2text.py -c note.txt
    python v0.2text.py -w note.txt
    python v0.2text.py -s note.txt

  • 运行结果

可以看出该程序运行速度还是相当快的

posted on 2024-10-28 19:45  指甲盖好吃吗  阅读(23)  评论(0编辑  收藏  举报