If you have a look at the entity relationship diagram (ERD) which illustrates relationships between the Salesforce Knowledge objects, you will see that the Vote object is related with the KnowledgeArticle. So to create a rating with Apex, you will simply have to create a new record of VOTE sObject and set the parentId as the KnowledgeArticleId and the Type as the rating score i.e. 1,2,3,4 or 5.
One important thing that you need to keep in mind while creating votes through APEX is that each user can rate an article only once. If you attempt to rate the same article again by the same user then an exception would be thrown as APEX would not allow duplicate vote entries by the same user for the same article