SUSI.AI had a thumbs up/down rating system till now, which has now been replaced by a five star skill rating system. Now, the user is allowed to rate the skill based on a five star rating system. The UI components include a rating bar and below the rating bar is a section that displays the skill rating statistics – total number of ratings, average rating and a graph showing the percentage of users who rated the skill with five stars, four stars and so on.
SUSI.AI Skills are rules that are defined in SUSI Skill Data repo which are basically the processed responses that SUSI returns to the user queries. When a user queries something from the SUSI Android app, a query to SUSI Server is made which in turn fetches data from SUSI Skill Data and returns a JSON response to the app. Similarly, to get skill ratings, a call to the ‘/cms/getSkillList.json’ API is made. In this API, the server checks the SUSI Skill Data repo for the skills and returns a JSON response consisting of all the required information like skill name, author name, description, ratings, etc. to the app. Then, this JSON response is parsed to extract individual fields to display the appropriate information in the skill details screen of the app.
The endpoint to fetch skills is ‘/cms/getSkillList.json’
The endpoints takes three parameters as input –
- model – It tells the model to which the skill belongs. The default value is set to general.
- group – It tells the group(category) to which the skill belongs. The default value is set to All.
- language – It tells the language to which the skill belongs. The default value is set to en.
Since all skills have to be fetched, this API is called for every group individually. For instance, call “https://api.susi.ai/cms/getSkillList.json?group=Knowledge” to get all skills in group “Knowledge”. Similarly, call for other groups.
Here is a sample response of a skill named ‘Capital’ from the group Knowledge :
It consists of all details about the skill called ‘Capital’:
- Model (model)
- Group (group)
- Language (language)
- Description (descriptions)
- Image (image)
- Author (author)
- Author URL (author_url)
- Skill name (skill_name)
- Content Type (dynamic_content)
- Examples (examples)
- Skill Rating (skill_rating)
- Creation Time (creationTime)
- Last Access Time (lastAccessTime)
- Last Modified Time (lastModifiedTime)
From among all this information, the information of interest for this blog is Skill Rating. This blog mainly deals with showing how to parse the JSON response to get the skill rating star values, so as to display the actual data in the skill rating graph.
A request to the getSkillList API is made for each group using the GET method.
It returns a JSON response consisting of all the aforementioned information. Now, to parse the JSON response, do the following :
- Add a response for the response received as a result of API call. ListSkillsResponse contains two objects – group and skills.
This blog is about getting the skill rating, so let us proceed with parsing the required response. The skills object contains the skill data that we need. Hence, next a SkillData class is created.
- Now, add the SkillData class. This class defines the response that we saw for ‘Capital’ skill above. It contains skill name, author, skill rating and so on.
- Now, add the SkillRating class. As what is required is the skill rating, narrowing down to the skill_rating object. The skill_rating object contains the actual rating for each skill i.e. the stars values. So, this files defines the response for the skill_rating object.
- Further, add a Stars class. Ultimately, the values that are needed are the number of users who rated a skill at five stars, four stars and so on and also the total number of users and the average rating. Thus, this file contains the values inside the ‘stars’ object.
Now, the parsing is all done. It is time to use these values to plot the skill rating graph and complete the section displaying the five star skill rating.
To plot these values on the skill rating graph refer to the blog on plotting horizontal bar graph using MPAndroid Chart library. In step 5 of the linked blog, replace the second parameter to the BarEntry constructor by the actual values obtained by parsing.
Here is how we do it.
- To get the total number of ratings
- To get the average rating
- To get number of users who rated the skill at five stars
Similarly, get the number of users for fourStar, threeStar, twoStar and oneStar.
Note : If the totalNumberOfRatings equals to zero, then the skill is unrated. In this case, display a message informing the user that the skill is unrated instead of plotting the graph.
Now, as the graph shows the percentage of users who rated the skill at a particular number of stars, calculate the percentage of users corresponding to each rating, parse the result to Float and place it as the second parameter to the BarEntry constructor as follows :
Similarly, replace the values for all five entries. Finally, add the total ratings and average rating section and display the detailed skill rating statistics for each skill, as in the following figure.
- To see how to make network calls in android using Retrofit
- To check out how to plot a chart use MPAndroidChart Library