chrislit/abydos

View on GitHub
abydos/stemmer/_porter2.py

Summary

Maintainability
F
4 days
Test Coverage

Function stem has a Cognitive Complexity of 199 (exceeds 5 allowed). Consider refactoring.
Open

    def stem(self, word: str) -> str:
        """Return the Porter2 (Snowball English) stem.

        Parameters
        ----------
Severity: Minor
Found in abydos/stemmer/_porter2.py - About 4 days to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Cyclomatic complexity is too high in method stem. (127)
Open

    def stem(self, word: str) -> str:
        """Return the Porter2 (Snowball English) stem.

        Parameters
        ----------
Severity: Minor
Found in abydos/stemmer/_porter2.py by radon

Cyclomatic Complexity

Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

Construct Effect on CC Reasoning
if +1 An if statement is a single decision.
elif +1 The elif statement adds another decision.
else +0 The else statement does not cause a new decision. The decision is at the if.
for +1 There is a decision at the start of the loop.
while +1 There is a decision at the while statement.
except +1 Each except branch adds a new conditional path of execution.
finally +0 The finally block is unconditionally executed.
with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
assert +1 The assert statement internally roughly equals a conditional statement.
Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

Source: http://radon.readthedocs.org/en/latest/intro.html

Cyclomatic complexity is too high in class Porter2. (64)
Open

class Porter2(_Snowball):
    """Porter2 (Snowball English) stemmer.

    The Porter2 (Snowball English) stemmer is defined in :cite:`Porter:2002`.

Severity: Minor
Found in abydos/stemmer/_porter2.py by radon

Cyclomatic Complexity

Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

Construct Effect on CC Reasoning
if +1 An if statement is a single decision.
elif +1 The elif statement adds another decision.
else +0 The else statement does not cause a new decision. The decision is at the if.
for +1 There is a decision at the start of the loop.
while +1 There is a decision at the while statement.
except +1 Each except branch adds a new conditional path of execution.
finally +0 The finally block is unconditionally executed.
with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
assert +1 The assert statement internally roughly equals a conditional statement.
Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

Source: http://radon.readthedocs.org/en/latest/intro.html

File _porter2.py has 310 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# Copyright 2014-2020 by Christopher C. Little.
# This file is part of Abydos.
#
# Abydos is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Severity: Minor
Found in abydos/stemmer/_porter2.py - About 3 hrs to fix

    Avoid too many return statements within this function.
    Open

                return word
    Severity: Major
    Found in abydos/stemmer/_porter2.py - About 30 mins to fix

      Avoid too many return statements within this function.
      Open

              return word
      Severity: Major
      Found in abydos/stemmer/_porter2.py - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

                    return word
        Severity: Major
        Found in abydos/stemmer/_porter2.py - About 30 mins to fix

          Refactor this function to reduce its Cognitive Complexity from 201 to the 15 allowed.
          Open

              def stem(self, word: str) -> str:
          Severity: Critical
          Found in abydos/stemmer/_porter2.py by sonar-python

          Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

          See

          Too many return statements (7/6)
          Open

              def stem(self, word: str) -> str:
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          Used when a function or method has too many return statement, making it hard to follow.

          Too many statements (177/50)
          Open

              def stem(self, word: str) -> str:
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          Used when a function or method has too many statements. You should then split it in smaller functions / methods.

          Too few public methods (1/2)
          Open

          class Porter2(_Snowball):
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          Used when class has too few public methods, so be sure it's really worth it.

          Unnecessary elif after return
          Open

                  if word in self._exception1dict:
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          Used in order to highlight an unnecessary block of code following an if containing a return statement. As such, it will warn when it encounters an else following a chain of ifs, all of them containing a return statement.

          Too many branches (114/12)
          Open

              def stem(self, word: str) -> str:
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          Used when a function or method has too many branches, making it hard to follow.

          Merge this if statement with the enclosing one.
          Open

                          if (
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if len(word[r1_start:]) >= 4:
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if len(word[r1_start:]) >= 4:
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if len(word[r1_start:]) >= 4:
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if (
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                      if self._sb_has_vowel(word[:-2]):
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if self._sb_has_vowel(word[:-3]):
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if len(word[r1_start:]) >= 6:
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                      if len(word[r1_start:]) >= 3:
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if len(word[r1_start:]) >= 5:
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if len(word[r1_start:]) >= 5:
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                          if (
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Merge this if statement with the enclosing one.
          Open

                      if len(word[r2_start:]) >= 1 and word[-2] == 'l':
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Merging collapsible if statements increases the code's readability.

          Noncompliant Code Example

          if condition1:
              if condition2:
                  # ...
          

          Compliant Solution

          if condition1 and condition2:
              # ...
          

          Either merge this branch with the identical one on line "214" or change one of the implementations.
          Open

                          if self._sb_has_vowel(word[:-3]):
                              word = word[:-3]
                              step1b_flag = True
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Having two branches in the same if structure with the same implementation is at best duplicate code, and at worst a coding error. If the same logic is truly needed for both instances, then they should be combined.

          Noncompliant Code Example

          if 0 <= a < 10:
              do_the_thing()
          elif 10 <= a < 20:
              do_the_other_thing()
          elif 20 <= a < 50:
              do_the_thing()  # Noncompliant; duplicates first condition
          else:
              do_the_rest()
          
          b = 4 if a > 12 else 4
          

          Compliant Solution

          if (0 <= a < 10) or (20 <= a < 50):
              do_the_thing()
          elif 10 <= a < 20:
              do_the_other_thing()
          else:
              do_the_rest()
          
          b = 4
          

          or

          if 0 <= a < 10:
              do_the_thing()
          elif 10 <= a < 20:
              do_the_other_thing()
          elif 20 <= a < 50:
              do_the_third_thing()
          else:
              do_the_rest()
          
          b = 8 if a > 12 else 4
          

          Either remove or fill this block of code.
          Open

                      pass
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Most of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed.

          Noncompliant Code Example

          for i in range(3):
              pass
          

          Exceptions

          When a block contains a comment, this block is not considered to be empty.

          Either merge this branch with the identical one on line "224" or change one of the implementations.
          Open

                          word += 'e'
          Severity: Major
          Found in abydos/stemmer/_porter2.py by sonar-python

          Having two branches in the same if structure with the same implementation is at best duplicate code, and at worst a coding error. If the same logic is truly needed for both instances, then they should be combined.

          Noncompliant Code Example

          if 0 <= a < 10:
              do_the_thing()
          elif 10 <= a < 20:
              do_the_other_thing()
          elif 20 <= a < 50:
              do_the_thing()  # Noncompliant; duplicates first condition
          else:
              do_the_rest()
          
          b = 4 if a > 12 else 4
          

          Compliant Solution

          if (0 <= a < 10) or (20 <= a < 50):
              do_the_thing()
          elif 10 <= a < 20:
              do_the_other_thing()
          else:
              do_the_rest()
          
          b = 4
          

          or

          if 0 <= a < 10:
              do_the_thing()
          elif 10 <= a < 20:
              do_the_other_thing()
          elif 20 <= a < 50:
              do_the_third_thing()
          else:
              do_the_rest()
          
          b = 8 if a > 12 else 4
          

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              and word[-3] in self._li
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and word[-3] in self._li ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ent',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ent', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ous',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ous', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ive',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ive', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              and word[-4] == 'l'
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and word[-4] == 'l' ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ence',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ence', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ment',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ment', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                          and not self._sb_ends_in_short_syllable(word[:-1])
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and not self.sbendsinshort_syllable(word[:-1]) ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      and word[-1] in {'Y', 'y'}
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and word[-1] in {'Y', 'y'} ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ic',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ic', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              r1_start >= 1
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO r1_start >= 1 ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ance',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ance', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ate',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ate', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ism',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ism', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              len(word[r2_start:]) >= 3
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO len(word[r2_start:]) >= 3 ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                          len(word[r1_start:]) >= 1
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO len(word[r1_start:]) >= 1 ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'al',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'al', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'able',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'able', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              and len(word[r1_start:]) >= 2
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and len(word[r1_start:]) >= 2 ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      len(word) > 2
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO len(word) > 2 ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              and len(word) >= 4
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and len(word) >= 4 ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ant',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ant', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'er',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'er', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              and word[-4] in tuple('st')
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and word[-4] in tuple('st') ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'iti',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'iti', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              r1_start >= 1
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO r1_start >= 1 ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ible',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ible', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ize',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ize', ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      and word[-2] not in self._vowels
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and word[-2] not in self._vowels ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                              and len(word[r1_start:]) >= 3
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO and len(word[r1_start:]) >= 3 ^ |

          Wrong hanging indentation before block (add 4 spaces).
          Open

                      'ement',
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          TODO 'ement', ^ |

          Consider using enumerate instead of iterating with range and len
          Open

                  for i in range(0, len(word)):
          Severity: Info
          Found in abydos/stemmer/_porter2.py by pylint

          Emitted when code that iterates with range and len is encountered. Such code can be simplified by using the enumerate builtin.

          There are no issues that match your filters.

          Category
          Status