Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextProfiler: enable pop when remove_disabled_flag set to True #495

Merged
merged 13 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions dataprofiler/profilers/unstructured_text_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,26 @@ def diff(self, other_profile, options=None):

return diff

def report(self, remove_disabled_flag=False):
"""Report on profile attribute of the class and pop value
from self.profile if key not in self.__calculations
"""
calcs_dict_keys = self._TextProfiler__calculations.keys()
profile = self.profile

if remove_disabled_flag:
profile_keys = list(profile.keys())
for profile_key in profile_keys:
if profile_key == 'vocab':
if 'vocab' in calcs_dict_keys:
continue
if profile_key == 'words':
if 'words' in calcs_dict_keys:
continue
profile.pop(profile_key)

return profile

@property
def profile(self):
"""
Expand Down
46 changes: 46 additions & 0 deletions dataprofiler/tests/profilers/test_unstructured_text_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,52 @@ def test_options_default(self):
self.assertDictEqual(expected_word_count, text_profile.word_count)
self.assertDictEqual(expected_vocab, text_profile.vocab_count)

def test_report(self):
"""Test report method in TextProfiler class under four (4) scenarios.
First, test under scenario of disabling vocab and word. Second, test with no options and
`remove_disabled_flag`=True. Third, test no options and default
`remove_disabled_flag`. Lastly, test under scenario of disabling vocab but not word.
"""
options = TextProfilerOptions() # With TextProfilerOptions as False and remove_disabled_flag == True
options.vocab.is_enabled = False
options.words.is_enabled = False

profiler = TextProfiler("Name", options)
sample = pd.Series(["This is test, a Test sentence.!!!"])
profiler.update(sample)

report = profiler.report(remove_disabled_flag=True)
report_keys = list(report.keys())
self.assertNotIn('vocab', report_keys)
self.assertNotIn('words', report_keys)

profiler = TextProfiler("Name") # w/o TextProfilerOptions and remove_disabled_flag == True
report = profiler.report(remove_disabled_flag=True)
report_keys = list(report.keys())
self.assertIn('vocab', report_keys)
self.assertIn('words', report_keys)

profiler = TextProfiler("Name") # w/o TextProfilerOptions and remove_disabled_flag default
report = profiler.report()
report_keys = list(report.keys())
self.assertIn('vocab', report_keys)
self.assertIn('words', report_keys)

options = TextProfilerOptions() # With TextProfilerOptions True/False and remove_disabled_flag == True
options.vocab.is_enabled = True
options.words.is_enabled = False

profiler = TextProfiler("Name", options)
sample = pd.Series(["This is test, a Test sentence.!!!"])
profiler.update(sample)

report = profiler.report(remove_disabled_flag=True)
report_keys = list(report.keys())

self.assertIn('vocab', report_keys)
self.assertNotIn('words', report_keys)


def test_options_case_sensitive(self):
# change is_case_sensitive, other options remain the same as default values
options = TextProfilerOptions()
Expand Down