{"id":483,"date":"2024-12-20T13:27:45","date_gmt":"2024-12-20T13:27:45","guid":{"rendered":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/?page_id=483"},"modified":"2024-12-20T17:57:57","modified_gmt":"2024-12-20T17:57:57","slug":"code-insight","status":"publish","type":"page","link":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/code-insight\/","title":{"rendered":"Code Insight"},"content":{"rendered":"<div class=\"page\" title=\"Page 1\">\n<div class=\"section\">\n<div class=\"layoutArea\">\n<div class=\"column\">\n<h2>1. Word Length Analysis<\/h2>\n<h4>Code Example:<\/h4>\n<pre>def word_length(filename):\r\n    file = open(filename)\r\n    line = file.readline()\r\n    count = 0\r\n<\/pre>\n<pre>    while line != '':\r\n        word_list = line.split()\r\n        for word in word_list:\r\n<\/pre>\n<pre>            count += len(word)\r\n        line = file.readline()\r\n<\/pre>\n<p>return count<\/p>\n<h4>Explanation:<\/h4>\n<p>This function calculates the total length of all words in a speech. We get every single line in the text, split every single word in the line into the list, and using len() function to get the length of words in lines in text<\/p>\n<h2>2. Word Count<\/h2>\n<h4>Code Example:<\/h4>\n<pre>def word_count(filename):\r\n    file = open(filename)\r\n    line = file.readline()\r\n    count = 0\r\n<\/pre>\n<pre>    while line != '':\r\n        word_list = line.split()\r\n        for word in word_list:\r\n<\/pre>\n<pre>            count += 1\r\n        line = file.readline()\r\n<\/pre>\n<p>return count<\/p>\n<h4>Explanation:<\/h4>\n<p>This function calculates the total number of words in a speech. We read every line in the text, split it into a list of words, and count the words in the list by iterating through them. The sum of all the words in all lines gives us the word count.<\/p>\n<h2>3. Sentence Count<\/h2>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"page\" title=\"Page 2\">\n<div class=\"section\">\n<div class=\"layoutArea\">\n<div class=\"column\">\n<h4>Code Example:<\/h4>\n<\/div>\n<\/div>\n<div class=\"layoutArea\">\n<div class=\"column\">\n<pre>def sentence_count(filename):\r\n<\/pre>\n<pre>    file = open(filename)\r\n    line = file.readline()\r\n    count = 0\r\n    while line != '':\r\n<\/pre>\n<pre>        for word in line:\r\n            if word == \".\":\r\n<\/pre>\n<pre>                count += 1\r\n        line = file.readline()\r\n<\/pre>\n<p>return count<\/p>\n<h4>Explanation:<\/h4>\n<p>This function calculates the total number of sentences in a speech. We go through every line in the text and check each character in the line for a period (.). Each occurrence of a period increases the sentence count by one.<\/p>\n<h2>4. Average Sentence Length<\/h2>\n<h4>Code Example:<\/h4>\n<pre>def average_word_in_sen(filename):\r\n    word_number = word_count(filename)\r\n    sentence_number = sentence_count(filename)\r\n    average_word_in_sen = word_number \/ sentence_number\r\n    return average_word_in_sen\r\n<\/pre>\n<h4>Explanation:<\/h4>\n<p>This function calculates the average number of words per sentence. We first get the total word count and the total sentence count, then divide the word count by the sentence count to find the average.<\/p>\n<h2>5. Line Count<\/h2>\n<h4>Code Example:<\/h4>\n<pre>def line_count(filename):\r\n    file = open(filename)\r\n    line = file.readline()\r\n    count = 1\r\n<\/pre>\n<pre>    while line != '':\r\n        line = file.readline()\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"page\" title=\"Page 3\">\n<div class=\"section\">\n<div class=\"layoutArea\">\n<div class=\"column\">\n<pre>        count += 1\r\n    return count\r\n<\/pre>\n<h4>Explanation:<\/h4>\n<p>This function calculates the total number of lines in the speech. We go through each line in the text, and for every line read, we increase the count by one.<\/p>\n<h2>6. Average Line Length<\/h2>\n<h4>Code Example:<\/h4>\n<pre>def average_line_length(filename):\r\n    line_number = line_count(filename)\r\n    word_number = word_count(filename)\r\n    return word_number \/ line_number\r\n<\/pre>\n<h4>Explanation:<\/h4>\n<p>This function calculates the average number of words per line. We divide the total word count by the total line count to compute this value.<\/p>\n<h2>7. Punctuation Marks<\/h2>\n<h4>Code Example:<\/h4>\n<pre>import string\r\ndef punctuation_marks(filename):\r\n<\/pre>\n<pre>    file = open(filename)\r\n    line = file.readline()\r\n    punc = string.punctuation\r\n    count = 0\r\n<\/pre>\n<pre>    while line != '':\r\n        for i in line:\r\n<\/pre>\n<pre>            if i in punc:\r\n                count += 1\r\n<\/pre>\n<pre>        line = file.readline()\r\n    return count\r\n<\/pre>\n<h4>Explanation:<\/h4>\n<p>This function calculates the total number of punctuation marks in a speech. We read every line and check each character to see if it matches any symbol in string.punctuation. Each match increases the punctuation count by one.<\/p>\n<h2>8. Average Sentence Complexity<\/h2>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"page\" title=\"Page 4\">\n<div class=\"section\">\n<div class=\"layoutArea\">\n<div class=\"column\">\n<h4>Code Example:<\/h4>\n<\/div>\n<\/div>\n<div class=\"layoutArea\">\n<div class=\"column\">\n<pre>def average_sentence_complexity(filename):\r\n    sentence_number = sentence_count(filename)\r\n    amount_punc = punctuation_marks(filename)\r\n    return amount_punc \/ sentence_number\r\n<\/pre>\n<h4>Explanation:<\/h4>\n<p>This function calculates the average sentence complexity by finding the average number of punctuation marks per sentence. We divide the total punctuation count by the total sentence count to compute this.<\/p>\n<h2>9. Average Word Length<\/h2>\n<h4>Code Example:<\/h4>\n<pre>def average_word_length(filename):\r\n    return word_length(filename) \/ word_count(filename)\r\n<\/pre>\n<h4>Explanation:<\/h4>\n<p>This function calculates the average length of words in a speech. We divide the total length of all words by the total number of words to compute this value.<\/p>\n<h2>1. aspects_listing() Function<\/h2>\n<h4>Code Example:<\/h4>\n<pre>def aspects_listing(filename):\r\n    temp = []\r\n<\/pre>\n<pre>    temp.append(round(word_length(filename),2))\r\n    temp.append(round(word_count(filename),2))\r\n    temp.append(round(sentence_count(filename),2))\r\n    temp.append(round(average_word_in_sen(filename),2))\r\n    temp.append(round(line_count(filename),2))\r\n    temp.append(round(average_line_length(filename),2))\r\n    temp.append(round(punctuation_marks(filename),2))\r\n    temp.append(round(average_sentence_complexity(filename),2))\r\n    temp.append(round(average_word_length(filename),2))\r\n<\/pre>\n<p>return temp<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"page\" title=\"Page 5\">\n<div class=\"section\">\n<div class=\"layoutArea\">\n<div class=\"column\">\n<h4>Explanation:<\/h4>\n<p>This function collects multiple aspects of the text into a list. It uses previously defined functions to calculate various metrics (e.g., word length, word count, sentence complexity) for the given file. Each value is rounded to two decimal places and added to the list in a specific order. The resulting list serves as a compact representation of a text&#8217;s stylistic attributes, and we use that as data for drawing graphs.<\/p>\n<h4>2. Scatter Plot: plot_scatter_graph()<\/h4>\n<p>Code:<\/p>\n<pre>import matplotlib.pyplot as plt\r\ndef plot_scatter_graph():\r\n<\/pre>\n<pre>    files = [\r\n        \"MIT Mid-Century Convocation\",\r\n        \"Sinews of Peace, 1946\",\r\n        \"The Council of Europe, 1949\",\r\n        \"Victory in Europe, 1945\",\r\n        \"Winston Churchill announces the Sur\"\r\n<\/pre>\n<p>]<\/p>\n<p>x_data = []<\/p>\n<pre>    x_data.append(aspects_listing('MIT Mid-Century\r\nConvocation.txt')[8])\r\n<\/pre>\n<pre>    x_data.append(aspects_listing('Sinews of Peace, 1946.txt')[8])\r\n<\/pre>\n<pre>    x_data.append(aspects_listing('The Council of Europe, 1949\r\n(1).txt')[8])\r\n<\/pre>\n<pre>    x_data.append(aspects_listing('Victory in Europe, 1945.txt')[8])\r\n<\/pre>\n<pre>    x_data.append(aspects_listing('Winston Churchill announces the\r\nSur.txt')[8])\r\n<\/pre>\n<p>y_data = []<\/p>\n<pre>    y_data.append(aspects_listing('MIT Mid-Century\r\nConvocation.txt')[7])\r\n<\/pre>\n<pre>    y_data.append(aspects_listing('Sinews of Peace, 1946.txt')[7])\r\n<\/pre>\n<pre>    y_data.append(aspects_listing('The Council of Europe, 1949\r\n(1).txt')[7])\r\n<\/pre>\n<pre>    y_data.append(aspects_listing('Victory in Europe, 1945.txt')[7])\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"page\" title=\"Page 6\">\n<div class=\"section\">\n<div class=\"layoutArea\">\n<div class=\"column\">\n<pre>    y_data.append(aspects_listing('Winston Churchill announces the\r\nSur.txt')[7])\r\n<\/pre>\n<pre>    plt.scatter(x_data, y_data)\r\n    for i in range(5):\r\n<\/pre>\n<pre>        plt.text(x_data[i],y_data[i],files[i])\r\n<\/pre>\n<pre>    plt.title(\"Word Length vs Sentence Complexity in some texts\")\r\n    plt.ylabel(\"Word Length\")\r\n    plt.xlabel(\"Sentence Complexity\")\r\n    plt.show()\r\n<\/pre>\n<pre>plot_scatter_graph()\r\n<\/pre>\n<p>Explanation:<br \/>\nThis function generates a scatter plot comparing average word length (x-axis) to sentence complexity (y-axis) across five speeches.<\/p>\n<p>\u25cf Data Preparation:<br \/>\nIt uses the aspects_listing function to extract the required metrics (average word length and sentence complexity) for each speech.<\/p>\n<p>\u25cf Visualization:<br \/>\nEach data point represents a speech, and text labels indicate the speech titles. The relationship between these two metrics is visualized, helping to identify stylistic differences. For instance, speeches with higher sentence complexity may reflect a formal style.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-592\" src=\"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-92-300x169.png\" alt=\"\" width=\"657\" height=\"370\" srcset=\"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-92-300x169.png 300w, https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-92-1024x576.png 1024w, https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-92-768x432.png 768w, https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-92-1536x864.png 1536w, https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-92.png 1920w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/p>\n<h4>3. 3D Scatter Plot: three_dimension_graph() Code:<br \/>\npython Copy code<\/h4>\n<pre>def three_dimension_graph():\r\n    ax = plt.axes(projection=\"3d\")\r\n    files = [\r\n<\/pre>\n<pre>        \"MIT Mid-Century Convocation\",\r\n        \"Sinews of Peace, 1946\",\r\n        \"The Council of Europe, 1949\",\r\n        \"Victory in Europe, 1945\",\r\n        \"Winston Churchill announces the Sur\"\r\n<\/pre>\n<pre>    ]\r\n       x_data = [aspects_listing(filename)[8] for filename in\r\n<\/pre>\n<p>file_list]<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"page\" title=\"Page 7\">\n<div class=\"section\">\n<div class=\"layoutArea\">\n<div class=\"column\">\n<pre>y_data = [aspects_listing(filename)[7] for filename in\r\nfile_list]\r\n<\/pre>\n<pre>    z_data = [aspects_listing(filename)[5] for filename in\r\nfile_list]\r\n<\/pre>\n<pre>    ax.scatter(x_data, y_data, z_data)\r\n    for i in range(len(files)):\r\n<\/pre>\n<pre>        ax.text(x_data[i], y_data[i], z_data[i], files[i])\r\n<\/pre>\n<pre>    ax.set_title(\"Word Length vs Sentence Complexity vs Line Length\r\nin some texts\")\r\n<\/pre>\n<pre>    ax.set_xlabel(\"Sentence Complexity\")\r\n    ax.set_ylabel(\"Word Length\")\r\n    ax.set_zlabel(\"Line Length\")\r\n    plt.show()\r\n<\/pre>\n<h4>Explanation:<\/h4>\n<p>This function creates a 3D scatter plot to compare three stylistic metrics:<\/p>\n<ol>\n<li>x-axis (Sentence Complexity): Average punctuation marks per sentence.<\/li>\n<li>y-axis (Word Length): Average word length.<\/li>\n<li>z-axis (Line Length): Average line length.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-594\" src=\"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-91-300x169.png\" alt=\"\" width=\"618\" height=\"348\" srcset=\"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-91-300x169.png 300w, https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-91-1024x576.png 1024w, https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-91-768x432.png 768w, https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-91-1536x864.png 1536w, https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/files\/2024\/12\/Screenshot-91.png 1920w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/p>\n<h4>\u25cf Data Preparation:<\/h4>\n<p>The aspects_listing function extracts the required metrics for each speech. \u25cf Visualization:<\/p>\n<p>Each point in the 3D space represents a speech. The axes provide a three-dimensional view of how sentence complexity, word length, and line length interact. Speeches with higher values in these metrics may suggest a more formal or complex structure.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Word Length Analysis Code Example: def word_length(filename): file = open(filename) line = file.readline() count = 0 while line != &#8221;: word_list = line.split() for word in word_list: count += len(word) line = file.readline() return count Explanation: This function calculates the total length of all words in a speech. We get every single line in &hellip; <a href=\"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/code-insight\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code Insight<\/span><\/a><\/p>\n","protected":false},"author":5512,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-483","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/wp-json\/wp\/v2\/pages\/483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/wp-json\/wp\/v2\/users\/5512"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/wp-json\/wp\/v2\/comments?post=483"}],"version-history":[{"count":0,"href":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/wp-json\/wp\/v2\/pages\/483\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.dickinson.edu\/digitalmethodsforthehumanities2024\/wp-json\/wp\/v2\/media?parent=483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}