Comparisonframe
Comparison Frame is designed to automate and streamline the process of comparing textual data, particularly focusing on various metrics
such as character and word count, punctuation usage, and semantic similarity.
It's particularly useful for scenarios where consistent text analysis is required,
such as evaluating the performance of natural language processing models, monitoring content quality,
or tracking changes in textual data over time using manual evaluation.
import sys
sys.path.append('../')
from python_modules.comparisonframe import ComparisonFrame
Usage examples
The examples contain:
- creating validation set and saving it to be reused
- comparing newly generated data with expected results
- recording test statuses
- reseting statuses, flushing records and comparison results
1. Creating validation set
1.1 Initialize comparison class
comparer = ComparisonFrame(
# optionally
## provide filenames to persist state
record_file = "record_file.csv", # file where queries and expected results are stored
results_file = "comparison_results.csv", # file where comparison results will be stored
)
1.2 Recording queries and expected responses (validation set)
comparer.record_query(query = "Black metal",
expected_text = "Black metal is an extreme subgenre of heavy metal music.")
comparer.record_query(query = "Tribulation",
expected_text = "Tribulation are a Swedish heavy metal band from Arvika that formed in 2005.")
2. Comparing with expected results
2.1 Initialize new comparison class
comparer = ComparisonFrame()
2.2 Show validation set
untested_queries = comparer.get_all_queries(
## optionall
untested_only=True)
print(untested_queries)
['Black metal', 'Tribulation']
comparer.get_all_records()
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
id |
timestamp |
query |
expected_text |
tested |
test_status |
0 |
1 |
2024-07-01 02:22:37 |
Black metal |
Black metal is an extreme subgenre of heavy me... |
no |
NaN |
1 |
2 |
2024-07-01 02:22:37 |
Tribulation |
Tribulation are a Swedish heavy metal band fro... |
no |
NaN |
2.3 Compare newly generated with recorded
valid_answer_query_1 = "Black metal is an extreme subgenre of heavy metal music."
very_similar_answer_query_1 = "Black metal is a subgenre of heavy metal music."
unexpected_answer_query_1 = "Black metals are beautiful and are often used in jewelry design."
# with no entry to records
comparer.compare_with_record(query = "Black metal",
provided_text = valid_answer_query_1,
mark_as_tested=False)
comparer.compare_with_record(query = "Black metal",
provided_text = very_similar_answer_query_1,
mark_as_tested=False)
comparer.compare_with_record(query = "Black metal",
provided_text = unexpected_answer_query_1,
mark_as_tested=False)
2.4 Check comparison results
comparer.get_comparison_results()
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
query |
char_count_diff |
word_count_diff |
line_count_diff |
punctuation_diff |
semantic_similarity |
expected_text |
provided_text |
id |
0 |
Black metal |
0 |
0 |
0 |
0 |
1.000000 |
Black metal is an extreme subgenre of heavy me... |
Black metal is an extreme subgenre of heavy me... |
1 |
1 |
Black metal |
9 |
1 |
0 |
0 |
0.985985 |
Black metal is an extreme subgenre of heavy me... |
Black metal is a subgenre of heavy metal music. |
1 |
2 |
Black metal |
8 |
1 |
0 |
0 |
0.494053 |
Black metal is an extreme subgenre of heavy me... |
Black metals are beautiful and are often used ... |
1 |
3. Record test statuses
comparer.compare_with_record(query = "Black metal",
provided_text = very_similar_answer_query_1,
mark_as_tested=True)
comparer.get_all_records()
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
id |
timestamp |
query |
expected_text |
tested |
test_status |
0 |
1 |
2024-07-01 02:22:37 |
Black metal |
Black metal is an extreme subgenre of heavy me... |
yes |
pass |
1 |
2 |
2024-07-01 02:22:37 |
Tribulation |
Tribulation are a Swedish heavy metal band fro... |
no |
NaN |
4. Reseting and flushing results
4.1 Reselt test statuses
comparer.reset_record_statuses(
# optionally
record_ids = [1]
)
comparer.get_all_records()
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
id |
timestamp |
query |
expected_text |
tested |
test_status |
0 |
1 |
2024-07-01 02:22:37 |
Black metal |
Black metal is an extreme subgenre of heavy me... |
no |
NaN |
1 |
2 |
2024-07-01 02:22:37 |
Tribulation |
Tribulation are a Swedish heavy metal band fro... |
no |
NaN |
4.2 Flush comparison results
comparer.flush_comparison_results()
comparer.get_comparison_results()
ERROR:ComparisonFrame:No results file found. Please perform some comparisons first.
4.3 Flush records
comparer.flush_records()
comparer.get_all_records()
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
id |
timestamp |
query |
expected_text |
tested |
test_status |