11"""Fragment class for prosegrinder."""
2+
23import re
34from collections import Counter
45
89from prosegrinder .word import Word
910
1011
11- class Fragment :
12+ class Fragment : # pylint: disable=too-many-instance-attributes
1213 """A fragment of text."""
1314
14- def __init__ (self , text , dictionary = Dictionary ()):
15+ def __init__ (self , text : str , dictionary : Dictionary = Dictionary ()):
1516 self .text = text
1617 self .dictionary = dictionary
1718 self .normalized_sentence = dictionary .normalize_text (text )
@@ -21,8 +22,8 @@ def __init__(self, text, dictionary=Dictionary()):
2122 ]
2223 self .word_count = len (self .words )
2324 self .word_character_count = sum (word .character_count for word in self .words )
24- _pf = Counter ()
25- _pc = 0
25+ _pf : Counter = Counter ()
26+ _pc : int = 0
2627 for word in self .words :
2728 _pf .update (word .phone_frequency )
2829 _pc += word .phone_count
@@ -41,7 +42,7 @@ def __init__(self, text, dictionary=Dictionary()):
4142 self .third_person_word_count = sum (
4243 word .is_third_person_word for word in self .words
4344 )
44- self .word_frequency = dict (Counter (self .words ))
45+ self .word_frequency = dict (Counter (word . text for word in self .words ))
4546 self .unique_words = self .word_frequency .keys ()
4647 self .unique_word_count = len (self .unique_words )
4748 self .pov = pointofview .NONE
@@ -52,17 +53,19 @@ def __init__(self, text, dictionary=Dictionary()):
5253 elif self .third_person_word_count > 0 :
5354 self .pov = pointofview .THIRD
5455
55- def __eq__ (self , other ) :
56+ def __eq__ (self , other : object ) -> bool :
5657 """Equality operator for instance variables."""
58+ if not isinstance (other , Fragment ):
59+ return False
5760 return self .text == other .text
5861
59- def __hash__ (self ):
62+ def __hash__ (self ) -> int :
6063 """Hash operator for instance variables."""
6164 return hash (self .text )
6265
63- def frequency (self , word_string ) :
66+ def frequency (self , word_string : str ) -> int :
6467 """Returns the frequency of a word in the fragment."""
65- return self .word_frequency [ word_string ]
68+ return self .word_frequency . get ( word_string , 0 )
6669
6770 @property
6871 def stats (self ):
0 commit comments