# A Hybrid Ontology-Based Recommendation System in e-Commerce
## 3 The Hybrid Ontology-based Approach
### Rules Approach  
Algorithm 1, 2 and 3  
In this subsection, we detail the first step of the recommendation system, which starts by creating a user profile. Algorithm 1 presents the first step, which is the rule to extract the products purchased by the user. ```
Input: ID of the active user (string) Result: List of reviews that active user have made return reviews on reviews Database that have revision.reviewerID equal to ID of the active user
```  
**Algorithm 1**Find reviews made by the active user  
The first step consists of searching the database for reviews that have the same reviewerID as the active user. After extracting the revisions made by the user, we create a User Profile, with the attributes explained in Section 3.2. Algorithm 2 shows the process to find the IDs of all reviewers that have purchased at least one product from the ProductsIds from the User Profile. ```
Input: List of IDs product that active user have purchased Result: List of distinct elements which contains the IDS of neighbours who have made at least one review that have revision.ProductID equal to the ID of the active user return List of IDs of neighbours
```  
**Algorithm 2**Find IDs of reviewers that have bought at least one product of the List of products from active user  
Observing the pseudocode of Algorithm 2, we see that for each element in the list of products IDs from the active user, a search in Reviews Database is made, in order to find the IDs of all the reviewers. This excludes the active user, who bought the same product. Then these IDs are stored in the global variable idsNeighbours. After completing this procedure to all elements in the list of products Ids from the active user, we select the distinct elements in idsNeighbours. In order to avoid applying the KNN algorithm to Neighbours who didn't buy different products from the user, it is fundamental to select only the reviewers that have purchased other products. This procedure is explained in Algorithm 3. \begin{table}
\begin{tabular}{c c c} \hline \hline
**Class** & **Properties** & **Facets** \\ \hline \multirow{3}{*}{User} & MinPrice & number \\ \cline{2-3}  & MaxPrice & number \\ \cline{2-3}  & ReviewersIds & list \\ \hline \multirow{3}{*}{Neighbour} & OtherCategories & boolean \\ \cline{2-3}  & features & vector \\ \hline \multirow{3}{*}{Product} & ProductID & string \\ \cline{2-3}  & Price & number \\ \cline{2-3}  & TopFiveWords & list \\ \cline{2-3}  & Overall & number \\ \cline{2-3}  & Category & string \\ \cline{2-3}  & Features & vector \\ \hline \hline \end{tabular}
\end{table} 
Table 1: _Cont._```
Input: ID of Neighbour, list of products of the active user Result: The condition about wheter Neighbour has made at one least review which not have review.ProductID in the list of products of the active user return True or False
```

**Algorithm 3**Find if Neighbour has bought different products from the active user

From the pseudocode presented in Algorithm 3, we see that, in order to find whether a Neighbour has purchased different products from those the active user has, it is necessary to access the Reviews Database and search for the products that the first has purchased and then check each of them in order to find out whether they belong or not, to the active users's list of products. At the moment, the previous condition is found to be true, the procedure stops and creates the respective Neighbour Profile, as previously explained in Section 3.2. The previous procedure is performed to each element of the attribute ReviewersIds of active User. ### KNN Algorithm to Find Neighbours

As mentioned before, the KNN algorithm is used to find the nearest Neighbours among the Neighbours of the user. In order to apply the Euclidean distance, we need to create a vector of features for each Neighbour. Figure 3 shows an example of this procedure.