Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import statistics
- # Get the markdown table from the input field
- markdown_table = pd['steps']['trigger']['data']['markdown_table']
- ratings = []
- types_of_result = []
- # Match the type of result and information gain rating using a regular expression
- pattern = re.compile(r'\|\s*(.*?)\s*\|\s*(.+?)\s*\|\s*(\d+)\s*\|\s*(.+?)\s*\|')
- # Find all matches and add the rating and type of result to the lists
- for match in pattern.findall(markdown_table):
- types_of_result.append(match[0])
- ratings.append(int(match[2]))
- # Calculate the average of the ratings and round to the nearest integer
- if ratings:
- avg_rating = round(statistics.mean(ratings))
- else:
- avg_rating = 0
- # Count the number of rows in the table (excluding headings)
- num_rows = markdown_table.count('\n') - 3
- # Determine the most common type(s) of result
- if types_of_result:
- freq_dict = {}
- for type in types_of_result:
- if type in freq_dict:
- freq_dict[type] += 1
- else:
- freq_dict[type] = 1
- max_freq = max(freq_dict.values())
- freq_types = [type for type, freq in freq_dict.items() if freq == max_freq]
- median_type = ','.join(freq_types)
- else:
- median_type = ''
- # Define the output dictionary
- output_data = {
- 'ratings': ','.join(map(str, ratings)),
- 'average_rating': avg_rating,
- 'num_rows': num_rows,
- 'median_type': median_type
- }
- # Return the output dictionary
- return output_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement