Skip to content

User_defined_models

MiguelMJ edited this page May 31, 2021 · 2 revisions

Recommendation models

Answerable allows you to specify which recommendation model to use to make the recommendations with the -m|--model option.

Using a custom model

  1. Create a new .py file in the models folder (for example, your_model.py).

  2. Define the recommend function in it (details below).

  3. a) Use the -m or --model with your model's filename without extension.

    python answerable.py recommend -m your_model
  4. b) Optionally, set it as default adding a "model": "your_model" field to the .config file, so you don't have to use the console option anymore.

The recommend function

Arguments

Answerable will pass to the function the following arguments:

  • user_qa: List with the questions answered by the user.

    Format: [(Question1, UserAnswer1), (Question2, UserAnswer2), ...]

    • Questions:

      {
          "tags": [ str ],
          "answers": [ { "owner": { "user_id": int } } ],
          "score": int,
          "creation_date": int (timestamp),
          "question_id": int,
          "link": str,
          "title": str,
          "body": str (html)
      }
    • Answers:

      {
      	"is_accepted": bool,
      	"score": int,
      	"questions_id": int,
      	"link": str,
      	"title": str,/MiguelMJ/Answerable/wiki/save
      	"body": str (html),
      }
  • feed: List with the newest questions retrieved.

    Format: [Question1, Question2, ...]

    • Questions:

      {
          "link": str,
          "title": str,
          "body": str (html),
          "tags": [ str ]
      }
Observations
  • The format of the questions in both arguments are different because the first ones come from the API and the second ones, from the RSS feed.
  • If the user includes questions that they have not answered, the user_qa pairs will have the corresponding answers as None.

Return

Answerable will require the function to return a pair of lists.

  • The first one with the indexes of feed sorted by the recommendation system.
  • The second one with the information about the recommendation for each question in feed in their initial order.

Here's some pseudo-code to illustrate how the values should be returned:

user_qa = [ ... ]
feed = [
	InterestingQuestion, 
    BoringQuestion,
    VeryInterestingQuestion,
    VeryBoringQuestion
]
indices, info = your_model.recommend(user_qa, feed)
print(indices)
# [2, 0, 1, 3]
print(info)
# [
#   InterestingQuestionInfo,
#   BoringQuestionInfo,
#   VeryInterestingQuestionInfo,
#   VeryBoringQuestionInfo
# ]
Observations

If your project won't return additional information about the recommendations, then the second value returned should be None. You can compare the return values of the recommend functions in content_based_0 and content_based_1 as an example.