Skip to content

Commit

Permalink
Merge pull request #77 from brunohjs/#73
Browse files Browse the repository at this point in the history
#73 - Section with element count and tables elements are numbered
  • Loading branch information
brunohjs authored Oct 7, 2023
2 parents 825994b + 4435ad5 commit a56cd0f
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 145 deletions.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _Put an `x` in the boxes that apply_
## Checklist
_Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code._

- [ ] I have read the [CONTRIBUTING](https://github.com/brunohjs/rasa-model-report/blob/main/CONTRIBUTING.md) doc
- [ ] I have read the [CONTRIBUTING](https://github.com/brunohjs/rasa-model-report/blob/main/CONTRIBUTING.md#-contributingsubmitting-changes) doc
- [ ] Lint and unit tests pass locally with my changes
- [ ] I have added or updated tests that prove my fix is effective or that my feature works (if appropriate)
- [ ] I have added or updated necessary documentation (if appropriate)
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [1.3.5] - In progress...

### Added
- [#73](https://github.com/brunohjs/rasa-model-report/issues/73) Created section with element count on model report. Additionally, now all tables elements are numbered.

### Fixed
- [#75](https://github.com/brunohjs/rasa-model-report/issues/75) Fixed bug with utters in actions that weren't being covered by the end-to-end tests.

Expand Down
104 changes: 83 additions & 21 deletions rasa_model_report/controllers/markdown_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,59 @@ def build_table(self, data: List[Union[str, float]]) -> str:
:param data: List representing the table data.
:return: Table in markdown format.
"""
header = "|" + "|".join(data[0]) + "|\n"
header += "|-" * len(data[0]) + "|\n"
header = f"|#|{'|'.join(data[0])}|\n"
header += f"{'|-' * (len(data[0]) + 1)}|\n"
content = ""
for row in data[1:]:
text_row = "|" + "|".join(row) + "|\n"
for index, row in enumerate(data[1:]):
text_row = f"|{index + 1}|{'|'.join(row)}|\n"
content += text_row
return header + content

def build_overview(self) -> str:
def build_overview_title(self) -> str:
"""
Build the report overview title block.
:return: Title block in markdown format.
"""
title = "## Overview <a name='overview'></a>\n"
return title

def build_grades(self) -> str:
"""
Build the text block responsible for the summary of the report.
:return: Overview report in markdown format.
"""
text = "### Grades\n"
style = "style='font-size:20px'"
overview = self.json.overview
for item in ["intent", "entity", "core", "nlu"]:
overview[item] = overview[item] if isinstance(overview.get(item), (float, int)) else "-"
text = "## Overview <a name='overview'></a>\n"
text += f"|Intent|Entity|NLU|Core|E2E Coverage|<span {style}>General</span>|\n"
text += "|:-:|:-:|:-:|:-:|:-:|:-:|\n"
text += f"|{utils.change_scale(overview['intent'], 10, self.precision)}\
|{utils.change_scale(overview['entity'], 10, self.precision)}\
|{utils.change_scale(overview['nlu'], 10, self.precision)}\
|{utils.change_scale(overview['core'], 10, self.precision)}\
|{utils.change_scale(overview['e2e_coverage'], 10, self.precision)}\
|<span {style}>**{utils.change_scale(overview['overall'], 10, self.precision)}**</span>|\n"
text += f"{utils.get_color(overview['intent'])}\
|{utils.get_color(overview['entity'])}\
|{utils.get_color(overview['nlu'])}\
|{utils.get_color(overview['core'])}\
|{utils.get_color(overview['e2e_coverage'])}\
|<span {style}>{utils.get_color(overview['overall'])}</span>|"
return text

def build_bot_info(self) -> str:
"""
Build the bot info block.
:return: Bot info block in markdown format.
"""
text = "### Bot info\n"
style = "style='font-size:16px'"
overview = self.json.overview
data = [
["Bot Name", self.project_name]
]
Expand All @@ -182,21 +216,49 @@ def build_overview(self) -> str:
text += "|" + "|".join([item[0] for item in data]) + "|\n"
text += "|" + "|".join([":-:" for i in range(len(data))]) + "|\n"
text += "|" + "|".join([f"<span {style}>{item[1]}</span>" for item in data]) + "|\n\n"
style = "style='font-size:20px'"
text += f"|Intent|Entity|NLU|Core|E2E Coverage|<span {style}>General</span>|\n"
text += "|:-:|:-:|:-:|:-:|:-:|:-:|\n"
text += f"|{utils.change_scale(overview['intent'], 10, self.precision)}\
|{utils.change_scale(overview['entity'], 10, self.precision)}\
|{utils.change_scale(overview['nlu'], 10, self.precision)}\
|{utils.change_scale(overview['core'], 10, self.precision)}\
|{utils.change_scale(overview['e2e_coverage'], 10, self.precision)}\
|<span {style}>**{utils.change_scale(overview['overall'], 10, self.precision)}**</span>|\n"
text += f"{utils.get_color(overview['intent'])}\
|{utils.get_color(overview['entity'])}\
|{utils.get_color(overview['nlu'])}\
|{utils.get_color(overview['core'])}\
|{utils.get_color(overview['e2e_coverage'])}\
|<span {style}>{utils.get_color(overview['overall'])}</span>|"
return text

def build_element_count(self) -> str:
"""
Build the report element count block.
:return: Element count block in markdown format.
"""
intents = len(self.json.intents)
entities = len(self.json.entities)
utters_actions = self.e2e_coverage.total_num_elements
stories_rules = utils.count_stories_and_rules(self.rasa_path)
stories = stories_rules.get("stories")
rules = stories_rules.get("rules")
data = [
{
"type": "Intents",
"total": intents
},
{
"type": "Entities",
"total": entities
},
{
"type": "Actions and Utters",
"total": utters_actions
},
{
"type": "Stories",
"total": stories
},
{
"type": "Rules",
"total": rules
}
]
text = "### Element count\n"
text += "Describe the number of elements in the chatbot.\n\n"
text += "|Element type|Total|\n"
text += "|:-:|:-:|\n"
for element in data:
text += f"|{element['type']}|{element['total']}|\n"
text += "\n"
return text

def build_intent_title(self) -> str:
Expand Down
5 changes: 4 additions & 1 deletion rasa_model_report/controllers/model_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def generate_report(self) -> None:
# Overview
self.markdown.add_text(self.markdown.title)
self.markdown.add_text(self.markdown.build_summary())
self.markdown.add_text(self.markdown.build_overview())
self.markdown.add_text(self.markdown.build_overview_title())
self.markdown.add_text(self.markdown.build_bot_info())
self.markdown.add_text(self.markdown.build_grades())
self.markdown.add_text(self.markdown.build_element_count())
self.markdown.break_line()

# Config
Expand Down
23 changes: 23 additions & 0 deletions rasa_model_report/helpers/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import datetime
import glob
import logging
import os
import re
from typing import Dict
from typing import List
from typing import Optional
from typing import Union
Expand Down Expand Up @@ -196,3 +198,24 @@ def remove_duplicate_slashs(text: str) -> str:
:return str: Text without duplicate slashs.
"""
return re.sub(r"\/+", "/", text)


def count_stories_and_rules(rasa_path: str) -> Dict[str, int]:
"""
Count the number of stories and rules.
:param rasa_path: Rasa project path.
:return: Dictionary with the number of stories and rules.
"""
data = {
"rules": 0,
"stories": 0
}
files = glob.glob(f"{rasa_path}/data/**/*yml", recursive=True)
for file in files:
file_data = load_yaml_file(file)
data.update({
"rules": data["rules"] + len(file_data.get("rules", [])),
"stories": data["stories"] + len(file_data.get("stories", []))
})
return data
30 changes: 25 additions & 5 deletions tests/test_markdown_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_build_table():
markdown_controller = pytest.markdown_controller
table = markdown_controller.build_table([["header_1", "header_2"], ["l1_c1", "l1_c2"], ["l2_c1", "l2_c2"]])
assert isinstance(table, str)
assert table == "|header_1|header_2|\n|-|-|\n|l1_c1|l1_c2|\n|l2_c1|l2_c2|\n"
assert table == "|#|header_1|header_2|\n|-|-|-|\n|1|l1_c1|l1_c2|\n|2|l2_c1|l2_c2|\n"


def test_build_summary():
Expand Down Expand Up @@ -102,22 +102,42 @@ def test_build_summary_without_config_section():
assert "#config" not in text


def test_build_overview():
def test_build_overview_title():
markdown_controller = pytest.markdown_controller
text = markdown_controller.build_overview()
text = markdown_controller.build_overview_title()
assert isinstance(text, str)
assert "## Overview" in text


def test_build_overview_with_model_link():
def test_build_grades():
markdown_controller = pytest.markdown_controller
text = markdown_controller.build_grades()
assert isinstance(text, str)
assert "### Grades" in text


def test_build_bot_info_with_model_link():
markdown_controller = pytest.markdown_controller
markdown_controller.model_link = "http://teste.com"
text = markdown_controller.build_overview()
text = markdown_controller.build_bot_info()
assert "### Bot info" in text
assert markdown_controller.model_link
assert "|Model|" in text
assert f"[Download]({markdown_controller.model_link})" in text


def test_build_element_count():
markdown_controller = pytest.markdown_controller
text = markdown_controller.build_element_count()
assert "### Element count" in text
assert "|Intents|" in text
assert "|Entities|" in text
assert "|Stories|" in text
assert "|Actions and Utters|" in text
assert "|Element type|" in text
assert "|Total|" in text


def test_build_intent_title():
markdown_controller = pytest.markdown_controller
text = markdown_controller.build_intent_title()
Expand Down
Loading

0 comments on commit a56cd0f

Please sign in to comment.