Apache Mahout: Creating similarities for taste_item_similarity table for the MySQLJDBCItemSimilarity class - mahout

I've created a taste_preferences table with the user_id, item_id, and preference, but now I'm trying to figure out how to calculate the similarities and insert them into my taste_item_similarity table.
Here's the link to the MySQLJDBCDataModel class which talks about the taste_preferences table, and here's a link to the MySQLJDBCItemSimilarity class which talks about the taste_item_similarity table.
Any help would be great!

Related

I can't replicate this database model in Activerecord

I'm creating a product catalogue in Rails using data from Icecat product database.
I have some problems to replicate the Category - FeatureGroup - Feature structure.
This is the schema of the database, you can ignore vocabulary and language because they are for translation.
And this is the xml file of every single product to use as reference
XML Sample file
In my rails app I create three models:
Category
FeatureGroup
Feature
And I add a many to many relation between Category and FeatureGroup and between FeatureGroup and Feature.
Basically I think that every category has many FeatureGroups and every FeatureGroup has many Features.
But in the XML file every feature node has this structure
<ProductFeature Localized="0" ID="150078803" Local_ID="0" Value="OLED" CategoryFeature_ID="85325" CategoryFeatureGroup_ID="26690" ...
So basically the Feature doesn't belongs to a FeatureGroup but to CategoryFeature_ID and to CategoryFeatureGroup_ID
I assume these tables are the join table between Category and Feature and beetween Category and FeatureGroup but I cannot understand the schema. And I don't have any idea how to replicate this schema in my rails app database...
I try to import the data with a task but I have about 28410 feature for one group so I think that something is wrong.
It looks like the feature_group table is used as a join table between the category_feature_group and the vocabulary tables. If you only need the Category and Feature models it seems to me you do not need the FeatureGroup model.
A feature has many categories through category_features. The category_feature table has a feature_id and a catid. So you can connect these two tables.
If you do need the feature_group table, you can find that through your category_features table and your category_feature_group table using the category_feature_group_id in the category_features table and the feature_group_id in the category_feature_group table.

Joining two tables that share the same id

Let's say I have two tables: NormalClass Table and SpecialClass Table.
Each table contains classes for students.
I want to join tables so that I can access the normal classes of each student in that has a special class for students.
Both tables share the student_id key.
Trying to do this:
NormalClass.includes(:specialClasses)..
results in:
ActiveRecord::ConfigurationError: Association named 'specialClasses' was not found on NormalClass.includes; perhaps you misspelled it?
Should I be doing something else?
Gerbil,
Basically, you need to first set your associations properly.
Look at section 2.4 The has_many :through Association in Rails guides
Once you set it, you can pull up the data you want by pulling the student_id key

How to display queried records in a specific order

I have HABTM relationships between Topic and Chapter Now I want to display chapters in a specific order under topics.
For Eg: A particular chapter might be 2nd chapter under one topic, and might be 8th chapter under another topic.
How Should I do it? Thanks in advance.
In this case you have a non-trivial many-to-many relationship, the join table needs to contain more than just foreign keys to each of main tables.
As a result, you'll want to move from HABTM to has_many :through on both Topic and Chapter, and change the join table to be its own model that not only relates the two main tables to each other, but also stores the order in a column (which, by the way, you should not name "order", since it will confuse SQL and Rails :-).
Converting from a standard Rails HABTM model is pretty easy, but one bit of advice: think of what it is that makes what used to be just a join table a real model ... and use that as a name for the new model (and associated new table). In a simple HABTM, you would create a table chapters_topics, when it becomes a model it might be "ChapterTopic" (resulting in a table chapter_topics).

How do I link several elements of different types to one another in RoR?

I have Events, Documents, and Surveys which all need to be able to link to one another, I was planning on having a linking table with four columns as follows:
link_elements{
element1_type CHAR(1)
element1_id INTEGER
element2_type CHAR(1)
element2_id INTEGER
}
The problem is I can't figure out how to make the model in RoR so that I can use the element type field to identify which table the corresponding element id belongs to (Documents, Events, or Surveys). I'm really new to Ruby and any help would really be appreciated.
I think you're just looking for the has_many and belongs_to associations.
If you have an Event model, a Document model, and a Survey model, then you can specify in their respective .rb files in the Models folder if they have or belong to the other models.
Ex: you want Surveys to belong to Documents. In Survey.rb, add the line belongs_to :document. In Document.rb, add the line has_many :surveys.
Now if you add a new "document_id" column in the Surveys table, it will look for a a Document object that corresponds to the id integer in that column.
For more info check out the Rails API.

Rails ActiveRecord Relationships

How do the relationships magically function when only the models are altered?
If I want a "has__and___belongs___to__many" relationship, what should I name the table (so Rails can use it) that contains the two foreign keys?
Short answer: You can't just tell the models that they're related; there have to be columns in the database for it too.
When you set up related models, Rails assumes you've followed a convention which allows it to find the things you wrote. Here's what happens:
You set up the tables.
Following conventions in Rails, you name the table in a particular, predictable way (a plural noun, e.g. people). In this table, when you have a relationship to another table, you have to create that column and name it in another predictable way (e.g. bank_account_id, if you're relating to the bank_accounts table).
You write a model class inheriting from ActiveRecord::Base
class Person < ActiveRecord::Base
When you instantiate one of these models, the ActiveRecord::Base constructor looks at the name of the class, converts it to lowercase and pluralizes it. Here, by reading Person, it yields people, the name of the table we created earlier. Now ActiveRecord knows where to get all the information about a person, and it can read the SQL output to figure out what the columns are.
You add relationships to the model: has_many, belongs_to or has_one.
When you type something like, has_many :bank_accounts, it assumes a few things:
The name of the model that you relate to is BankAccount (from camel-casing :bank_accounts).
The name of the column in the people table which refers to a bank account is bank_account_id (from singularizing :bank_accounts).
Since the relationship is has_many, ActiveRecord knows to give you methods like john.bank_accounts, using plural names for things.
Putting all of that together, ActiveRecord knows how to make SQL queries that will give you a person's bank accounts. It knows where to find everything, because you followed a naming convention that it understands when you created the table and its colums.
One of the neat things about Ruby is that you can run methods on a whole class, and those methods can add other methods to a class. That's exactly what has_many and friends are doing.
This works because you are following "Convention over Configuration".
If you state that a customer model has many orders then rails expects there to be a customer_id field on the orders table.
If you have followed these conventions then rails will use them and will be able to build the necessary SQL to find all the orders for a given customer.
If you look at the development.log file when you are developing your application you will be able to see the necessary SQL being built to select all orders for a given customer.
Rails does not create tables without you asking it to. The creation of tables is achieved by generating a migration which will create/alter tables for you. The fact that you create a customer model and then state within it that it has_many :orders will not create you an orders table. You will need to do that for yourself within a migration to create an orders table. Within that migration you will need to either add a customer_id column or use the belongs_to: customer statement to get the customer_id field added to the orders table.
The rails guide for this is pretty useful

Resources