├── .gitignore ├── LICENSE ├── README ├── readability_score ├── __init__.py ├── calculators │ ├── __init__.py │ ├── ari.py │ ├── colemanliau.py │ ├── dalechall.py │ ├── flesch.py │ ├── fleschkincaid.py │ ├── linsearwrite.py │ ├── nl │ │ ├── __init__.py │ │ ├── fleschdouma.py │ │ └── kpc.py │ └── smog.py ├── calculatortest.py ├── common.py └── textanalyzer.py ├── setup.py └── test ├── test_ari.py ├── test_colemanliau.py ├── test_common.py ├── test_dalechall.py ├── test_fleschkincaid.py ├── test_lib.py ├── test_linsearwrite.py ├── test_nl_fleschdouma.py ├── test_nl_kpc.py ├── test_smog.py └── test_unicode.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Eclipse 2 | .settings 3 | .pydevproject 4 | 5 | 6 | # Python 7 | *.pyc 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is a readability calculator library for Python. 2 | 3 | You're able to calculate the readability of a text using several algorithms: 4 | - Flesch-Kincaid 5 | - Coleman-Liau 6 | - Dale-Chall 7 | - SMOG 8 | - Automated Readability Index 9 | - Flesch Reading Ease (does not have min_age) 10 | 11 | Dutch: 12 | - Flesch-Douma 13 | - KPC (AVI) 14 | 15 | 16 | All available calculators have a min_age property which describes the typical minimum age for a potential reader. 17 | 18 | Although the algorithms themselves might not be language independent, they can be called as such using a custom locale. This locale has to be available in the pyphen dependency. By default, the locale is set to 'en_GB'. 19 | 20 | 21 | Wim Muskee, 2012-2017 22 | wimmuskee@gmail.com 23 | 24 | License: GPL-2 25 | 26 | 27 | # Dependencies 28 | - NLTK 29 | - Pyphen 30 | - Python, tested on 2.7 and 3.4 31 | 32 | 33 | # Example: 34 | from readability_score.calculators.fleschkincaid import * 35 | from readability_score.calculators.dalechall import * 36 | 37 | fk = FleschKincaid(open('/tmp/text.txt').read(), locale='nl_NL') 38 | dc = DaleChall(open( '/tmp/text.txt' ).read(), simplewordlist=awordlist, locale='de_DE') 39 | 40 | print(fk.min_age) 41 | print(dc.min_age) 42 | 43 | 44 | # Tests 45 | Some tests are included with the library. I'am not sure how reactive the scores will be using different dependencies on other systems. 46 | This is not exact science, don't use these tests for anything important. 47 | 48 | cd readability_score 49 | python -m unittest discover -s test -v 50 | -------------------------------------------------------------------------------- /readability_score/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wimmuskee/readability-score/5f738f1128c998eabde1e906aa72ca640210eec7/readability_score/__init__.py -------------------------------------------------------------------------------- /readability_score/calculators/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wimmuskee/readability-score/5f738f1128c998eabde1e906aa72ca640210eec7/readability_score/calculators/__init__.py -------------------------------------------------------------------------------- /readability_score/calculators/ari.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the Automated Readability Index readability calculator 4 | 5 | This tool can calculate the readability score of a text 6 | using the Automated Readability Index. 7 | http://en.wikipedia.org/wiki/Automated_Readability_Index 8 | 9 | Wim Muskee, 2012-2017 10 | wimmuskee@gmail.com 11 | 12 | License: GPL-2 13 | """ 14 | from __future__ import division 15 | from readability_score.common import getMinimumAgeFromUsGrade 16 | from readability_score.textanalyzer import TextAnalyzer 17 | 18 | 19 | class ARI(TextAnalyzer): 20 | def __init__(self, text, locale='en_GB'): 21 | TextAnalyzer.__init__(self,text,locale) 22 | self.setTextScores() 23 | self.us_grade = 0 24 | self.setGrade() 25 | self.min_age = getMinimumAgeFromUsGrade(self.us_grade) 26 | 27 | def setGrade(self): 28 | """ 29 | Calculates US grade as a float from the available 30 | text scores. 31 | """ 32 | if self.scores['word_count'] != 0 and self.scores['sent_count'] != 0: 33 | self.us_grade = (4.71 * (self.scores['letter_count']/self.scores['word_count'])) + (0.5 * (self.scores['word_count']/self.scores['sent_count'])) - 21.43 34 | 35 | -------------------------------------------------------------------------------- /readability_score/calculators/colemanliau.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the Coleman-Liau readability calculator 4 | 5 | This tool can calculate the readability score of a text 6 | using the Coleman–Liau index. 7 | http://en.wikipedia.org/wiki/Coleman-Liau_Index 8 | 9 | Wim Muskee, 2012-2017 10 | wimmuskee@gmail.com 11 | 12 | License: GPL-2 13 | """ 14 | from readability_score.common import getMinimumAgeFromUsGrade 15 | from readability_score.textanalyzer import TextAnalyzer 16 | 17 | 18 | class ColemanLiau(TextAnalyzer): 19 | def __init__(self, text, locale='en_GB'): 20 | TextAnalyzer.__init__(self,text,locale) 21 | self.setTextScores() 22 | self.us_grade = 0 23 | self.setGrade() 24 | self.min_age = getMinimumAgeFromUsGrade(self.us_grade) 25 | 26 | def setGrade(self): 27 | """ 28 | Calculates US grade as a float from the available 29 | text scores. 30 | """ 31 | self.us_grade = (0.0588 * self.scores['wordletter_average'] * 100) - (0.296 * self.scores['wordsent_average'] * 100) - 15.8 32 | -------------------------------------------------------------------------------- /readability_score/calculators/dalechall.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the Dale-Chall readability calculator 4 | 5 | This tool can calculate the readability score of a text 6 | using the Dale-Chall Readability Formula. 7 | http://en.wikipedia.org/wiki/Dale-Chall_Readability_Formula 8 | 9 | This algorithm requires a lower case simple word list. 10 | According to the documentation, some 3000 words which are 11 | considered easily readable. 12 | 13 | Wim Muskee, 2012-2017 14 | wimmuskee@gmail.com 15 | 16 | License: GPL-2 17 | """ 18 | from __future__ import division 19 | from readability_score.common import getMinimumAgeFromUsGrade 20 | from readability_score.textanalyzer import TextAnalyzer 21 | 22 | 23 | class DaleChall(TextAnalyzer): 24 | def __init__(self, text, locale='en_GB', simplewordlist=[]): 25 | TextAnalyzer.__init__(self,text,locale) 26 | self.setSimpleWordsList(simplewordlist) 27 | self.setTextScores() 28 | self.readingindex = 0 29 | self.us_grade = 0 30 | self.setReadingIndex() 31 | self.setGrade() 32 | self.min_age = getMinimumAgeFromUsGrade(self.us_grade) 33 | 34 | def setReadingIndex(self): 35 | if self.scores['word_count'] != 0: 36 | difficultwords = self.scores['word_count'] - self.scores['simpleword_count'] 37 | difficultwords_perc = difficultwords / self.scores['word_count'] * 100 38 | self.readingindex = (0.1579 * difficultwords_perc) + (0.0496 * self.scores['sentlen_average']) 39 | 40 | if difficultwords_perc > 5: 41 | self.readingindex = self.readingindex + 3.6365 42 | 43 | def setGrade(self): 44 | """ 45 | The grade calculation is based on the one in the 46 | documentation, also allowing for ranges within the 47 | grades. The grade calculation counts on after the 48 | college graduate grade (16), result might be less 49 | reliable after that. 50 | """ 51 | if self.readingindex <= 5.9: 52 | self.us_grade = self.readingindex 53 | elif self.readingindex > 5.9 and self.readingindex <= 6.9: 54 | self.us_grade = self.readingindex + 1 55 | elif self.readingindex > 6.9 and self.readingindex <= 7.9: 56 | self.us_grade = self.readingindex + 2 57 | elif self.readingindex > 7.9 and self.readingindex <= 8.9: 58 | self.us_grade = self.readingindex + 3 59 | elif self.readingindex > 8.9 and self.readingindex <= 9.9: 60 | self.us_grade = self.readingindex + 4 + (self.readingindex - int(self.readingindex)) 61 | else: 62 | self.us_grade = self.readingindex + 6 63 | -------------------------------------------------------------------------------- /readability_score/calculators/flesch.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the Flesch Reading Ease readability calculator 4 | 5 | This tool can calculate the readability score of a text 6 | using the Flesch Reading Ease algorithm. 7 | http://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_test 8 | 9 | This calculator won't export a grade level or minimum age, but only 10 | the score for reading ease. 11 | 12 | Wim Muskee, 2013-2017 13 | wimmuskee@gmail.com 14 | 15 | License: GPL-2 16 | """ 17 | from readability_score.textanalyzer import TextAnalyzer 18 | 19 | class Flesch(TextAnalyzer): 20 | def __init__(self, text, locale='en_GB'): 21 | TextAnalyzer.__init__(self,text,locale) 22 | self.setTextScores() 23 | self.reading_ease = 206.835 - ( 1.015 * self.scores['sentlen_average'] ) - ( 84.6 * self.scores['wordlen_average'] ) 24 | self.min_age = None 25 | -------------------------------------------------------------------------------- /readability_score/calculators/fleschkincaid.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the Flesch-Kincaid readability calculator 4 | 5 | This tool can calculate the readability score of a text 6 | using the Flesch-Kincaid Grade Level. 7 | http://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_test 8 | 9 | Wim Muskee, 2012-2017 10 | wimmuskee@gmail.com 11 | 12 | License: GPL-2 13 | """ 14 | from readability_score.common import getMinimumAgeFromUsGrade 15 | from readability_score.textanalyzer import TextAnalyzer 16 | 17 | 18 | class FleschKincaid(TextAnalyzer): 19 | def __init__(self, text, locale='en_GB'): 20 | TextAnalyzer.__init__(self,text,locale) 21 | self.setTextScores() 22 | self.us_grade = 0 23 | self.setGrade() 24 | self.min_age = getMinimumAgeFromUsGrade(self.us_grade) 25 | 26 | def setGrade(self): 27 | """ 28 | Calculates US grade as a float from the available 29 | text scores. 30 | """ 31 | self.us_grade = (0.39 * self.scores['sentlen_average']) + (11.8 * self.scores['wordlen_average']) - 15.59 32 | -------------------------------------------------------------------------------- /readability_score/calculators/linsearwrite.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the LinsearWrite readability calculator 4 | 5 | This tool can calculate the readability score of a text 6 | using the Linsear Write readability metric. 7 | 8 | https://en.wikipedia.org/wiki/Linsear_Write 9 | 10 | Wim Muskee, 2017 11 | wimmuskee@gmail.com 12 | 13 | License: GPL-2 14 | """ 15 | from __future__ import division 16 | from readability_score.common import getMinimumAgeFromUsGrade 17 | from readability_score.textanalyzer import TextAnalyzer 18 | 19 | 20 | class LinsearWrite(TextAnalyzer): 21 | def __init__(self, text, locale='en_GB'): 22 | TextAnalyzer.__init__(self,text,locale) 23 | self.setTextScores() 24 | self.us_grade = 0 25 | self.setGrade() 26 | self.min_age = getMinimumAgeFromUsGrade(self.us_grade) 27 | 28 | def setGrade(self): 29 | if self.scores["sent_count"] != 0: 30 | score = ((self.scores["polysyllword_count"] * 3) + self.scores['word_count'] - self.scores["polysyllword_count"]) / self.scores["sent_count"] 31 | 32 | if score > 20: 33 | self.us_grade = score / 2 34 | else: 35 | self.us_grade = (score - 2) / 2 36 | -------------------------------------------------------------------------------- /readability_score/calculators/nl/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wimmuskee/readability-score/5f738f1128c998eabde1e906aa72ca640210eec7/readability_score/calculators/nl/__init__.py -------------------------------------------------------------------------------- /readability_score/calculators/nl/fleschdouma.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the Flesch-Douma readability calculator 4 | 5 | This tool can calculate the readability score of a text 6 | using the Flesch-Douma index. 7 | http://www.kennislink.nl/publicaties/hoe-begrijpelijk-is-mijn-tekst 8 | http://hchiemstra.wordpress.com/2011/02/24/is-de-leesbaarheid-van-een-tekst-objectief-te-meten/ 9 | 10 | Wim Muskee, 2012-2017 11 | wimmuskee@gmail.com 12 | 13 | License: GPL-2 14 | """ 15 | from readability_score.textanalyzer import TextAnalyzer 16 | 17 | 18 | class FleschDouma(TextAnalyzer): 19 | def __init__(self, text, locale='en_GB'): 20 | TextAnalyzer.__init__(self,text,locale) 21 | self.setTextScores() 22 | self.readingindex = 0 23 | self.setReadingIndex() 24 | self.setMinimumAge() 25 | 26 | def setReadingIndex(self): 27 | self.readingindex = 206.84 - (0.77 * self.scores['sentlen_average']) - (93 * self.scores['wordlen_average']) 28 | 29 | def setMinimumAge(self): 30 | """ 31 | Mapped the textual descriptions of the target groups 32 | on to ages. Extrapolated this beyond the index of 100. 33 | """ 34 | if self.readingindex < 30: 35 | self.min_age = 24 36 | elif self.readingindex >= 30 and self.readingindex < 50: 37 | self.min_age = 18 38 | elif self.readingindex >= 50 and self.readingindex < 60: 39 | self.min_age = 16 40 | elif self.readingindex >= 60 and self.readingindex < 70: 41 | self.min_age = 12 42 | elif self.readingindex >= 70 and self.readingindex < 80: 43 | self.min_age = 11 44 | elif self.readingindex >= 80 and self.readingindex < 90: 45 | self.min_age = 10 46 | elif self.readingindex >= 90 and self.readingindex < 100: 47 | self.min_age = 9 48 | # extrapolated from this point 49 | elif self.readingindex >= 100 and self.readingindex < 110: 50 | self.min_age = 8 51 | elif self.readingindex >= 110 and self.readingindex < 120: 52 | self.min_age = 7 53 | else: 54 | self.min_age = 6 55 | -------------------------------------------------------------------------------- /readability_score/calculators/nl/kpc.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the KPC AVI readability calculator 4 | 5 | This tool can calculate the AVI readability score 6 | of a Dutch text using the old KPC method. 7 | http://nl.wikipedia.org/wiki/AVI_%28onderwijs%29 8 | 9 | Wim Muskee, 2012-2017 10 | wimmuskee@gmail.com 11 | 12 | License: GPL-2 13 | """ 14 | from __future__ import division 15 | from readability_score.textanalyzer import TextAnalyzer 16 | 17 | 18 | class KPC(TextAnalyzer): 19 | def __init__(self, text, locale='en_GB'): 20 | TextAnalyzer.__init__(self,text,locale) 21 | self.setTextScores() 22 | self.avi = 0 23 | self.readingindex = 0 24 | self.setReadingIndex() 25 | self.setAvi() 26 | self.setMinimumAge() 27 | 28 | 29 | def setReadingIndex(self): 30 | """ 31 | Calculates reading index required for AVI calculation. 32 | Sets readingindex as float. When using with the avi 33 | calculating table, it should be rounded to a whole integer. 34 | """ 35 | self.readingindex = 192 - ( 2 * self.scores['sentlen_average'] ) - ( 200/3 * self.scores['wordlen_average'] ) 36 | 37 | 38 | def setAvi(self): 39 | """ 40 | Calculates AVI level using the old KPC method. The calculation 41 | follows the documentation until AVI 9. However more AVI scores 42 | will be calculated. 43 | """ 44 | readingindex = round(self.readingindex) 45 | 46 | if readingindex > 127: 47 | self.avi = 0 48 | elif readingindex <= 127 and readingindex >= 123: 49 | self.avi = 1 50 | elif readingindex <= 123 and readingindex >= 112: 51 | self.avi = 2 52 | elif readingindex <= 120 and readingindex >= 108 and self.scores['wordlen_average'] >= 1.10: 53 | self.avi = 3 54 | elif readingindex <= 110 and readingindex >= 100 and self.scores['wordlen_average'] >= 1.15: 55 | self.avi = 4 56 | elif readingindex <= 99 and readingindex >= 94: 57 | self.avi = 5 58 | else: 59 | avi = 5 60 | max_index = 98 61 | i = 1 62 | 63 | while (not self.avi): 64 | max_index = max_index - 5 65 | min_index = max_index - 4 66 | 67 | if readingindex <= max_index and readingindex >= min_index: 68 | self.avi = avi + i 69 | 70 | i = i + 1 71 | 72 | 73 | def setMinimumAge(self): 74 | """ 75 | Sets minimum age required for reading a text based 76 | on set AVI score. The calculation is based roughly on 77 | the documentation. 78 | """ 79 | if self.avi == 0: 80 | self.min_age = 0 81 | elif self.avi < 8: 82 | self.min_age = int(round( (self.avi/3) + 6 )) 83 | else: 84 | self.min_age = int(round( (self.avi/2) + 5 )) 85 | 86 | -------------------------------------------------------------------------------- /readability_score/calculators/smog.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This is the SMOG readability calculator 4 | 5 | This tool can calculate the readability score of a text 6 | using the Simple Measure Of Gobbledygook. 7 | For texts with 30 or more sentences, the calculator gets a sample 8 | of 10 sentences from the beginning, middle and the end of the text. 9 | 10 | http://en.wikipedia.org/wiki/SMOG_%28Simple_Measure_Of_Gobbledygook%29 11 | http://webpages.charter.net/ghal/SMOG_Readability_Formula_G._Harry_McLaughlin_%281969%29.pdf 12 | 13 | Wim Muskee, 2012-2017 14 | wimmuskee@gmail.com 15 | 16 | https://github.com/i-trofimtschuk, 2013 17 | 18 | License: GPL-2 19 | """ 20 | from __future__ import division 21 | from readability_score.common import getMinimumAgeFromUsGrade 22 | from readability_score.textanalyzer import TextAnalyzer 23 | 24 | 25 | class SMOG(TextAnalyzer): 26 | def __init__(self, text, locale='en_GB'): 27 | TextAnalyzer.__init__(self,text,locale) 28 | self.setTextScores() 29 | self.us_grade = 0 30 | self.setGrade() 31 | self.min_age = getMinimumAgeFromUsGrade(self.us_grade) 32 | 33 | def setTextScores(self): 34 | """ 35 | SMOG custom wrapper for setting all the scores. 36 | """ 37 | self.setSentences() 38 | if self.scores["sent_count"] >= 30: 39 | sentence_middle = int(self.scores["sent_count"]/2) 40 | self.sentences = self.sentences[:10] + self.sentences[sentence_middle -5:5+ sentence_middle] + self.sentences[-10:] 41 | self.scores["sent_count"] = len(self.sentences) 42 | self.parseSentences() 43 | self.setAverages() 44 | 45 | def setGrade(self): 46 | """ 47 | Calculates US grade as a float from the available 48 | text scores. 49 | """ 50 | if self.scores['sent_count'] != 0: 51 | self.us_grade = (1.043 * ((self.scores['polysyllword_count'] * (30 / self.scores['sent_count']))**0.5)) + 3.1291 52 | -------------------------------------------------------------------------------- /readability_score/calculatortest.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # class used by each calculator test 4 | class CalculatorTest(): 5 | def __init__(self,min_test_age): 6 | self.text = "Readability is the ease with which a reader can understand a written text. In natural language, the readability of text depends on its content (the complexity of its vocabulary and syntax) and its presentation (such as typographic aspects like font size, line height, and line length)." 7 | self.text_ro = "Lizibilitatea (etimologie franceză lisibilité) este calitatea unui text de a fi ușor de citit și înțeles. De-a lungul timpului, au fost dezvoltate mai multe criterii pentru evaluarea lizibilității. Unul dintre acestea este viteza de citire. Lizibilitatea a fost cercetată în amănunțime în secolul al XX-lea, fiind elaborate mai multe sisteme de ameliorare a lizibilității textelor." 8 | self.min_age_test = min_test_age 9 | self.test_range = [ self.min_age_test, self.min_age_test + 1, self.min_age_test - 1 ] 10 | self.test_range_fail_text = "min_age is not within 1 year range of " + str(self.min_age_test) 11 | -------------------------------------------------------------------------------- /readability_score/common.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This module contains common functions used 4 | in the various readability calculations. 5 | 6 | Wim Muskee, 2012-2017 7 | wimmuskee@gmail.com 8 | 9 | License: GPL-2 10 | """ 11 | def getMinimumAgeFromUsGrade(us_grade): 12 | """ 13 | The age has a linear relation with the grade. 14 | http://en.wikipedia.org/wiki/Education_in_the_United_States#School_grades 15 | """ 16 | if us_grade == 0: 17 | return 0 18 | 19 | from decimal import Decimal, ROUND_HALF_UP 20 | min_age = int(Decimal(us_grade + 5).quantize(Decimal('1'), rounding=ROUND_HALF_UP)) 21 | if min_age < 0: 22 | return 0 23 | else: 24 | return min_age 25 | -------------------------------------------------------------------------------- /readability_score/textanalyzer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This class contains the main text analyzer used in all 4 | the calculators. 5 | 6 | Wim Muskee, 2012-2017 7 | wimmuskee@gmail.com 8 | 9 | License: GPL-2 10 | """ 11 | from __future__ import division 12 | from sys import version_info 13 | import warnings 14 | import re 15 | import os 16 | 17 | with warnings.catch_warnings(): 18 | # catch NLTK warning, fixed in 4.2.2 19 | warnings.filterwarnings("ignore",category=PendingDeprecationWarning,message='the imp module is deprecated in favour of importlib.*') 20 | # catch ndg-httpsclient warning, fixed in 0.4.2 21 | warnings.filterwarnings("ignore",category=ImportWarning,message='Not importing directory.*ndg.*') 22 | # catch matplotlib warning, don't know what the issue is, no problem for this package 23 | warnings.filterwarnings("ignore",category=ImportWarning,message='Not importing directory.*mpl_toolkits.*') 24 | 25 | from nltk.tokenize import sent_tokenize, word_tokenize 26 | import pyphen 27 | 28 | 29 | class TextAnalyzer: 30 | def __init__(self,text,locale='en_GB'): 31 | self.setText(text) 32 | self.setLocale(locale) 33 | self.sentences = [] 34 | self.simple_words = [] 35 | self.min_age = 0 36 | self.scores = { 37 | 'sent_count': 0, # nr of sentences 38 | 'word_count': 0, # nr of words 39 | 'letter_count':0, # nr of characters in words (no spaces) 40 | 'syll_count': 0, # nr of syllables 41 | 'polysyllword_count': 0, # nr of polysyllables (words with more than 2 syllables) 42 | 'simpleword_count': 0, # nr of simplewords (depends on provided list) 43 | 'sentlen_average': 0, # words per sentence 44 | 'wordlen_average': 0, # syllables per word 45 | 'wordletter_average': 0, # letters per word 46 | 'wordsent_average': 0 # sentences per word 47 | } 48 | self.re_words = re.compile(r'\w+', flags = re.UNICODE) 49 | 50 | 51 | def setText(self,text): 52 | """ 53 | Sets the text, and makes sure Python2 is working with unicode. 54 | """ 55 | if version_info.major == 1: 56 | raise RuntimeError("Python version too low") 57 | elif version_info.major == 2 and not isinstance(text,unicode): 58 | self.text = unicode(text,'utf-8') 59 | else: 60 | self.text = text 61 | 62 | 63 | def setLocale(self,locale): 64 | """ 65 | Sets locale-related data. 66 | """ 67 | if os.path.exists(locale): 68 | self.hyphenator = pyphen.Pyphen(filename=locale) 69 | elif len(locale) > 1 and locale in pyphen.LANGUAGES: 70 | self.hyphenator = pyphen.Pyphen(lang=locale) 71 | self.setTokenizeLanguage(locale) 72 | else: 73 | raise LookupError("provided locale not supported by pyphen") 74 | 75 | 76 | def setSimpleWordsList(self,simplewords): 77 | """ 78 | Simple word list for DaleChall calculator. 79 | """ 80 | if isinstance(simplewords,list): 81 | self.simple_words = simplewords 82 | else: 83 | raise ValueError("A simple word list should be provided as list") 84 | 85 | 86 | def setTokenizeLanguage(self,locale): 87 | """ 88 | Set the language NLTK's sent_tokenize uses. 89 | Based on local available punkt tokenizers. 90 | This is done in the init, but can also be changed by calling this. 91 | """ 92 | self.tokenize_language = self.__getTokenizelanguage(locale[:2]) 93 | 94 | 95 | def setTextScores(self): 96 | """ 97 | Wrapper for setting all the scores. 98 | """ 99 | self.setSentences() 100 | self.parseSentences() 101 | self.setAverages() 102 | 103 | 104 | def setSentences(self): 105 | """ 106 | Tokenize the sentences from the text. Depending on the locale, 107 | a custom tokenize language may be used if available. 108 | """ 109 | try: 110 | self.sentences = sent_tokenize(self.text, language=self.tokenize_language) 111 | except LookupError: 112 | # maybe custom tokenize language not available on fs, do default 113 | self.sentences = sent_tokenize(self.text, language="english") 114 | 115 | self.scores['sent_count'] = len(self.sentences) 116 | 117 | 118 | def parseSentences(self): 119 | """ 120 | Parse each sentence and each word, and count 121 | the individual countable scores. 122 | """ 123 | for s in self.sentences: 124 | words = self.re_words.findall(s) 125 | self.scores['word_count'] += len(words) 126 | 127 | for w in words: 128 | syllables_count = self.hyphenator.inserted(w).count('-') + 1 129 | self.scores['syll_count'] += syllables_count 130 | self.scores['letter_count'] += len(w) 131 | 132 | if syllables_count > 2: 133 | self.scores['polysyllword_count'] += 1 134 | 135 | if self.simple_words: 136 | if w.lower() in self.simple_words: 137 | self.scores['simpleword_count'] += 1 138 | 139 | 140 | def setAverages(self): 141 | """ 142 | Sets all relevant averages based on the 143 | individual counts. 144 | """ 145 | if self.scores['sent_count'] > 0: 146 | self.scores['sentlen_average'] = self.scores['word_count'] / self.scores['sent_count'] 147 | 148 | if self.scores['word_count'] > 0: 149 | self.scores['wordlen_average'] = self.scores['syll_count'] / self.scores['word_count'] 150 | self.scores['wordletter_average'] = self.scores['letter_count'] / self.scores['word_count'] 151 | self.scores['wordsent_average'] = self.scores['sent_count'] / self.scores['word_count'] 152 | 153 | 154 | def __getTokenizelanguage(self,locale_lookup): 155 | """ 156 | Try to find a value for provided locale key. 157 | Return "english" by default. 158 | """ 159 | lookup_value = "english" 160 | lookup = { 161 | "cs": "czech", 162 | "da": "danish", 163 | "de": "german", 164 | "el": "greek", 165 | "es": "spanish", 166 | "et": "estonian", 167 | "en": "english", 168 | "fr": "french", 169 | "it": "italian", 170 | "nb": "norwegian", 171 | "nl": "dutch", 172 | "po": "polish", 173 | "pt": "portuguese", 174 | "sl": "slovene", 175 | "sv": "swedish" 176 | } 177 | 178 | if locale_lookup in lookup: 179 | lookup_value = lookup[locale_lookup] 180 | 181 | return lookup_value 182 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import setup, find_packages 3 | 4 | def read(fname): 5 | return open(os.path.join(os.path.dirname(__file__), fname)).read() 6 | 7 | setup( 8 | name = "readability-score", 9 | version = "2.1", 10 | author = "Wim Muskee", 11 | author_email = "wimmuskee@gmail.com", 12 | description = ("This tool can calculate the readability score of a text."), 13 | license = "GPL-2", 14 | keywords = "text difficulty readability score", 15 | packages=find_packages(), 16 | long_description=read('README'), 17 | install_requires = [ 18 | 'pyphen', 19 | 'nltk' 20 | ] 21 | ) 22 | -------------------------------------------------------------------------------- /test/test_ari.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, expectedFailure 2 | from readability_score.calculatortest import CalculatorTest 3 | from readability_score.calculators.ari import * 4 | 5 | class ARITestCase(TestCase,CalculatorTest): 6 | def setUp(self): 7 | CalculatorTest.__init__(self, 19) 8 | 9 | def test_range_min_age(self): 10 | self.calc = ARI(self.text) 11 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 12 | 13 | @expectedFailure 14 | def test_exact_min_age(self): 15 | self.calc = ARI(self.text) 16 | self.assertEqual(self.calc.min_age, self.min_age_test, "might break because of deps") 17 | 18 | def test_not_negative_min_age(self): 19 | self.calc = ARI("") 20 | self.assertGreaterEqual(self.calc.min_age, 0, "even with no text, min_age should not be negative") 21 | -------------------------------------------------------------------------------- /test/test_colemanliau.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, expectedFailure 2 | from readability_score.calculatortest import CalculatorTest 3 | from readability_score.calculators.colemanliau import * 4 | 5 | class ColemanLiauTestCase(TestCase,CalculatorTest): 6 | def setUp(self): 7 | CalculatorTest.__init__(self, 17) 8 | 9 | def test_range_min_age(self): 10 | self.calc = ColemanLiau(self.text) 11 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 12 | 13 | @expectedFailure 14 | def test_exact_min_age(self): 15 | self.calc = ColemanLiau(self.text) 16 | self.assertEqual(self.calc.min_age, self.min_age_test, "might break because of deps") 17 | 18 | def test_not_negative_min_age(self): 19 | self.calc = ColemanLiau("") 20 | self.assertGreaterEqual(self.calc.min_age, 0, "even with no text, min_age should not be negative") 21 | -------------------------------------------------------------------------------- /test/test_common.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase 2 | from readability_score.common import * 3 | 4 | class CommonTestCase(TestCase): 5 | def test_minimum_age_rounding(self): 6 | # default python3 round lets 6.5 be rounded to 6 7 | us_grade = 1.5 8 | min_age = getMinimumAgeFromUsGrade(us_grade) 9 | self.assertEqual(min_age,7) 10 | -------------------------------------------------------------------------------- /test/test_dalechall.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, expectedFailure 2 | from readability_score.calculatortest import CalculatorTest 3 | from readability_score.calculators.dalechall import * 4 | 5 | class DaleChallTestCase(TestCase,CalculatorTest): 6 | def setUp(self): 7 | CalculatorTest.__init__(self, 21) 8 | self.simplewords = ["is","the","a","can","text","in","of","on","its","and","such","as","line","size","ease","with","content","font","length","height"] 9 | 10 | def test_range_min_age(self): 11 | self.calc = DaleChall(self.text,simplewordlist=self.simplewords) 12 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 13 | 14 | @expectedFailure 15 | def test_exact_min_age(self): 16 | self.calc = DaleChall(self.text,simplewordlist=self.simplewords) 17 | self.assertEqual(self.calc.min_age, self.min_age_test, "might break because of deps") 18 | 19 | def test_not_negative_min_age(self): 20 | self.calc = DaleChall("") 21 | self.assertGreaterEqual(self.calc.min_age, 0, "even with no text, min_age should not be negative") 22 | -------------------------------------------------------------------------------- /test/test_fleschkincaid.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, expectedFailure 2 | from readability_score.calculatortest import CalculatorTest 3 | from readability_score.calculators.fleschkincaid import * 4 | 5 | class FleschKincaidTestCase(TestCase,CalculatorTest): 6 | def setUp(self): 7 | CalculatorTest.__init__(self, 17) 8 | 9 | def test_range_min_age(self): 10 | self.calc = FleschKincaid(self.text) 11 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 12 | 13 | @expectedFailure 14 | def test_exact_min_age(self): 15 | self.calc = FleschKincaid(self.text) 16 | self.assertEqual(self.calc.min_age, self.min_age_test, "might break because of deps") 17 | 18 | def test_not_negative_min_age(self): 19 | self.calc = FleschKincaid("") 20 | self.assertGreaterEqual(self.calc.min_age, 0, "even with no text, min_age should not be negative") 21 | -------------------------------------------------------------------------------- /test/test_lib.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase 2 | from readability_score.textanalyzer import TextAnalyzer 3 | 4 | class LibTestCase(TestCase): 5 | def test_incorrect_locale(self): 6 | with self.assertRaises(LookupError): 7 | t = TextAnalyzer("some text","bad_locale") 8 | 9 | def test_short_locale(self): 10 | with self.assertRaises(LookupError): 11 | t = TextAnalyzer("some text","e") 12 | 13 | def test_correct_locale(self): 14 | try: 15 | t = TextAnalyzer("some text","nl_NL") 16 | except: 17 | fk = None 18 | self.assertTrue(hasattr(t,"scores"),"text analyzer not made with non default but correct locale") 19 | 20 | def test_bad_simpleword_list(self): 21 | t = TextAnalyzer("some text") 22 | with self.assertRaises(ValueError): 23 | t.setSimpleWordsList("this string") 24 | 25 | def test_tokenize_language_default(self): 26 | t = TextAnalyzer("some text") 27 | self.assertEqual(t.tokenize_language,"english","default tokenize_language should be english") 28 | 29 | def test_tokenize_language_custom(self): 30 | t = TextAnalyzer("some text","nl_NL") 31 | self.assertEqual(t.tokenize_language,"dutch","set tokenize_language should be dutch") 32 | 33 | def test_tokenize_language_exotic(self): 34 | t = TextAnalyzer("some text","zu_ZA") 35 | self.assertEqual(t.tokenize_language,"english","default tokenize_language should be english") 36 | -------------------------------------------------------------------------------- /test/test_linsearwrite.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, expectedFailure 2 | from readability_score.calculatortest import CalculatorTest 3 | from readability_score.calculators.linsearwrite import * 4 | 5 | class LinsearWriteTestCase(TestCase,CalculatorTest): 6 | def setUp(self): 7 | CalculatorTest.__init__(self, 21) 8 | 9 | def test_range_min_age(self): 10 | self.calc = LinsearWrite(self.text) 11 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 12 | 13 | @expectedFailure 14 | def test_exact_min_age(self): 15 | self.calc = LinsearWrite(self.text) 16 | self.assertEqual(self.calc.min_age, self.min_age_test, "might break because of deps") 17 | 18 | def test_not_negative_min_age(self): 19 | self.calc = LinsearWrite("") 20 | self.assertGreaterEqual(self.calc.min_age, 0, "even with no text, min_age should not be negative") 21 | -------------------------------------------------------------------------------- /test/test_nl_fleschdouma.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, expectedFailure 2 | from readability_score.calculatortest import CalculatorTest 3 | from readability_score.calculators.nl.fleschdouma import * 4 | 5 | class FleschDoumaTestCase(TestCase,CalculatorTest): 6 | def setUp(self): 7 | CalculatorTest.__init__(self, 18) 8 | 9 | def test_range_min_age(self): 10 | self.calc = FleschDouma(self.text) 11 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 12 | 13 | @expectedFailure 14 | def test_exact_min_age(self): 15 | self.calc = FleschDouma(self.text) 16 | self.assertEqual(self.calc.min_age, self.min_age_test, "might break because of deps") 17 | 18 | def test_not_negative_min_age(self): 19 | self.calc = FleschDouma("") 20 | self.assertGreaterEqual(self.calc.min_age, 0, "even with no text, min_age should not be negative") 21 | -------------------------------------------------------------------------------- /test/test_nl_kpc.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, expectedFailure 2 | from readability_score.calculatortest import CalculatorTest 3 | from readability_score.calculators.nl.kpc import * 4 | 5 | class KPCTestCase(TestCase,CalculatorTest): 6 | def setUp(self): 7 | CalculatorTest.__init__(self, 13) 8 | 9 | def test_range_min_age(self): 10 | self.calc = KPC(self.text) 11 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 12 | 13 | @expectedFailure 14 | def test_exact_min_age(self): 15 | self.calc = KPC(self.text) 16 | self.assertEqual(self.calc.min_age, self.min_age_test, "might break because of deps") 17 | 18 | def test_not_negative_min_age(self): 19 | self.calc = KPC("") 20 | self.assertGreaterEqual(self.calc.min_age, 0, "even with no text, min_age should not be negative") 21 | -------------------------------------------------------------------------------- /test/test_smog.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, expectedFailure 2 | from readability_score.calculatortest import CalculatorTest 3 | from readability_score.calculators.smog import * 4 | 5 | class SMOGTestCase(TestCase,CalculatorTest): 6 | def setUp(self): 7 | CalculatorTest.__init__(self, 20) 8 | 9 | def test_range_min_age(self): 10 | self.calc = SMOG(self.text) 11 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 12 | 13 | @expectedFailure 14 | def test_exact_min_age(self): 15 | self.calc = SMOG(self.text) 16 | self.assertEqual(self.calc.min_age, self.min_age_test, "might break because of deps") 17 | 18 | def test_not_negative_min_age(self): 19 | self.calc = SMOG("") 20 | self.assertGreaterEqual(self.calc.min_age, 0, "even with no text, min_age should not be negative") 21 | -------------------------------------------------------------------------------- /test/test_unicode.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase, skipIf 2 | from sys import version_info 3 | from readability_score.calculatortest import CalculatorTest 4 | from readability_score.calculators.fleschkincaid import * 5 | 6 | class UnicodeTestCase(TestCase,CalculatorTest): 7 | def setUp(self): 8 | CalculatorTest.__init__(self, 23) 9 | 10 | @skipIf(version_info.major > 2, "specific python2 test") 11 | def test_unicode_stringimport(self): 12 | self.calc = FleschKincaid(unicode(self.text_ro, "utf-8"),'ro_RO') 13 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 14 | 15 | def test_regular_stringimport(self): 16 | self.calc = FleschKincaid(self.text_ro,'ro_RO') 17 | self.assertIn(self.calc.min_age, self.test_range, self.test_range_fail_text) 18 | --------------------------------------------------------------------------------