{"id":407,"date":"2009-05-07T17:04:21","date_gmt":"2009-05-07T22:04:21","guid":{"rendered":"http:\/\/mikeconley.ca\/blog\/?p=407"},"modified":"2023-12-20T16:25:20","modified_gmt":"2023-12-20T21:25:20","slug":"wordcountxpi-part-1","status":"publish","type":"post","link":"https:\/\/mikeconley.ca\/blog\/2009\/05\/07\/wordcountxpi-part-1\/","title":{"rendered":"wordCount.xpi &#8211; Part 1"},"content":{"rendered":"<p>So, if you recall, I was asked to write a Firefox extension that would do word counting on websites.<\/p>\n<p>Originally, when I started this project, I set a goal for myself:\u00a0 I copied the text from <a href=\"http:\/\/www.gutenberg.org\/dirs\/etext00\/0ws2610.txt\">Project Gutenberg&#8217;s First Folio version of Shakespeare&#8217;s Hamlet <\/a>into OpenOffice Writer, recorded the word\/line\/character count statistics, and set that as my projected goal for my first iteration of my extension.<\/p>\n<p>But there&#8217;s a problem with this approach:\u00a0 I&#8217;m supposed to be copying the behaviour of Unix&#8217;s wc, not OpenOffice Writer&#8217;s word count.\u00a0 Normally, this wouldn&#8217;t be a problem &#8211; a word count is a word count, a line count is a line count, and Writer should pump out the same numbers as wc.<\/p>\n<p>Not so.<\/p>\n<p>In my last post, I wrote:<\/p>\n<blockquote><p>According to OpenOffice Writer, this text has 32230 words, 173543 characters, and 4257 lines.<\/p><\/blockquote>\n<p>However, upon passing the same text (saved in the textfile &#8220;count.txt&#8221;) through wc, I got the following output:<\/p>\n<blockquote><p>5302  32230 178845 count.txt<\/p><\/blockquote>\n<p>Writer and wc agree on the number of words, but disagree on the number of lines &#8211; 5302 (wc) vs 4257 (Writer).\u00a0 It&#8217;s a disagreement of about a thousand lines.<\/p>\n<p>Brutal.<\/p>\n<p>Anyhow, I&#8217;m going to focus on wc&#8217;s approach to line counting &#8211; simply returning the number of newline characters in the file.<\/p>\n<p>And guess what&#8230;it works.\u00a0 For Hamlet, my extension pumps out:<\/p>\n<blockquote><p>Document statistics:<\/p>\n<p>Word Count:\u00a0 32230<br \/>\nLine Count:\u00a0 5302<br \/>\nCharacter Count:\u00a0 178845<br \/>\nCharacter Count (no spaces):\u00a0 142368<\/p><\/blockquote>\n<p>Nice.<\/p>\n<p>Hamlet&#8217;s just the simple case though.\u00a0 There are plenty of other cases to consider, but this is a start.<\/p>\n<p>Anyhow, <a href=\"http:\/\/www.mikeconley.ca\/Firefox_Extensions\/wordCount\/wordCount.xpi\">download here<\/a>.<\/p>\n<p>In this version, I&#8217;m using <a href=\"https:\/\/developer.mozilla.org\/En\/DOM\/TreeWalker\">Mozilla&#8217;s TreeWalker implementation<\/a> to stitch together the page text.\u00a0 So far it seems to be working alright, but if it somehow ends up falling through, I might end up using something like <a href=\"http:\/\/www.friendpaste.com\/29sHyVu1J3iljnGks30yY3\">Andrew Trusty&#8217;s code<\/a> with the jQuery library to do the text stitching.<\/p>\n<p>So there it is.\u00a0 Maybe I&#8217;ll keep working on this, pretty it up a bit, etc.\u00a0 However, work starts on Monday, and that&#8217;ll probably take up most of my technical attention.<\/p>\n<p>We&#8217;ll see though.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, if you recall, I was asked to write a Firefox extension that would do word counting on websites. Originally, when I started this project, I set a goal for myself:\u00a0 I copied the text from Project Gutenberg&#8217;s First Folio version of Shakespeare&#8217;s Hamlet into OpenOffice Writer, recorded the word\/line\/character count statistics, and set that [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,220,44,68,79],"tags":[125,35,225,224,222,227,228,223,226],"class_list":["post-407","post","type-post","status-publish","format-standard","hentry","category-computer-science","category-firefox-extensions","category-internet","category-javascript","category-technology","tag-firefox","tag-mozilla","tag-openoffice","tag-treewalker","tag-wc","tag-word-count","tag-word-counting","tag-wordcount","tag-writer"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/prmTy-6z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/comments?post=407"}],"version-history":[{"count":5,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/407\/revisions"}],"predecessor-version":[{"id":3247,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/407\/revisions\/3247"}],"wp:attachment":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/media?parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/categories?post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/tags?post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}