Example - A Recommender

We have demonstrated the basic pattern match syntax. You should have mastered the basics by this point. In this section, we show two end-to-end solutions using the pattern match syntax.

A Recommendation Application

In this example, we want to recommend some messages (comments or posts) to the person Viktor Akhiezer.

How do we do this?

One way is to find others who like the same messages Viktor likes, then recommend the messages that others like but Viktor has not seen. The pattern can be sketched out as follows:

  • Viktor - (Likes>) - Message - (<Likes) - Others

  • Others - (Likes>) - NewMessage

  • Recommend NewMessage to Viktor

However, this is too granular. We are overfitting the message-level data with a collaborative filtering algorithm.

Intutively, two persons are similar to each other when their "liked" messages fall into the same category - here represented by the set of tags attached to each message.

As a result, one way to avoid overfitting is to go one level upward. Instead of looking at common messages, we look at their tags. We consider Person A and Person B similar if they like messages that belong to the same tag. This scheme fixes the overfitting problem. In pattern match vocabulary, we have

  • Viktor - (Likes>) - Message - (Has>) - Tag - (<Has) - Message - (<Likes) - Others

  • Others - (Likes>) - NewMessage

  • Recommend NewMessage to Viktor

GSQL. RecommendMessage Application.

This time, we create the query first and interpret the query by calling the query name with parameters.
If we are satisfied with this query, we can use INSTALL QUERY queryName to install the query, increasing performance.

GSQL Recommendation Algorithm
use graph ldbc_snb
set query_timeout=60000
DROP QUERY RecommendMessage

CREATE QUERY RecommendMessage (String fn, String ln) SYNTAX v2{

  SumAccum<int> @TagInCommon;
  SumAccum<float> @SimilarityScore;
  SumAccum<float> @Rank;
  OrAccum @Liked = false;

   #1. mark messages liked by Viktor
   #2. calculate log similarity score for each persons share the same
   #   interests at Tag level.
    Others =
       SELECT p
       FROM Person:s-(LIKES>)-:msg - (HAS_TAG>.<HAS_TAG.<LIKES)- :p
       WHERE s.firstName == fn AND s.lastName == ln
       ACCUM msg.@Liked = true, p.@TagInCommon +=1
       POST-ACCUM p.@SimilarityScore = log (1 + p.@TagInCommon);

    # recommend new messages to Viktor that have not liked by him.
    RecommendedMessage =
             SELECT msg
             FROM Others:o-(LIKES>) - :msg
             WHERE  msg.@Liked == false
             ACCUM msg.@Rank +=o.@SimilarityScore
             ORDER BY msg.@Rank DESC
             LIMIT 2;

  PRINT   RecommendedMessage[RecommendedMessage.content, RecommendedMessage.@Rank];
}


INTERPRET QUERY RecommendMessage ("Viktor", "Akhiezer")
#try the second person with just parameter change.
INTERPRET QUERY RecommendMessage ("Adriaan", "Jong")

You can copy the above GSQL script to a file named app1.gsql and invoke this script file in the command line.

Linux Bash
gsql app1.gsql
Output of App1
Using graph 'ldbc_snb'
The query RecommendMessage is dropped.
The query RecommendMessage has been added!
{
  "error": false,
  "message": "",
  "version": {
    "schema": 0,
    "edition": "enterprise",
    "api": "v2"
  },
  "results": [{"RecommendedMessage": [
    {
      "v_id": "549760294602",
      "attributes": {
        "RecommendedMessage.@Rank": 4855.49219,
        "RecommendedMessage.content": "About Indira Gandhi, Gandhi established closer relatAbout Mick Jagger, eer of the band. In 1989, he waAbout Ho Chi Minh, ce Unit and ECA International, About Ottoman Empire,  After t"
      },
      "v_type": "Post"
    },
    {
      "v_id": "549760292109",
      "attributes": {
        "RecommendedMessage.@Rank": 4828.7251,
        "RecommendedMessage.content": "About Ho Chi Minh, nam, as an anti-communist state, fought against the communisAbout Shiny Happy People, sale in the U."
      },
      "v_type": "Post"
    }
  ]}]
}

Install the query

When you are satisfied with your query in the GSQL interpreted mode, you can install it as a generic service. This speeds up querying considerably. When we ran CREATE QUERY RecommendMessage…​, this added RecommendMessage as a query string to the catalog.

To install the query as a compiled procedure, we first set the syntax version and then run the INSTALL QUERY command. If you don’t explicitly set the syntax version, then the query will be installed using the syntax version specified in the query itself, if provided, or using the default syntax version.

GSQL Prepare to Install Query
#before installing the query, need to set the syntax version
SET syntax_version="v2"
USE GRAPH ldbc_snb

#install query
INSTALL QUERY RecommendMessage
GSQL Run the Installed Query
GSQL > install query RecommendMessage
Start installing queries, about 1 minute ...
RecommendMessage query: curl -X GET 'http://127.0.0.1:9000/query/ldbc_snb/RecommendMessage?fn=VALUE&ln=VALUE'. Add -H "Authorization: Bearer TOKEN" if authentication is enabled.

[========================================================================================================] 100% (1/1)
GSQL > run query RecommendMessage("Viktor", "Akhiezer")
{
  "error": false,
  "message": "",
  "version": {
    "schema": 0,
    "edition": "enterprise",
    "api": "v2"
  },
  "results": [{"RecommendedMessage": [
    {
      "v_id": "549760294602",
      "attributes": {
        "RecommendedMessage.@Rank": 4855.49219,
        "RecommendedMessage.content": "About Indira Gandhi, Gandhi established closer relatAbout Mick Jagger, eer of the band. In 1989, he waAbout Ho Chi Minh, ce Unit and ECA International, About Ottoman Empire,  After t"
      },
      "v_type": "Post"
    },
    {
      "v_id": "549760292109",
      "attributes": {
        "RecommendedMessage.@Rank": 4828.7251,
        "RecommendedMessage.content": "About Ho Chi Minh, nam, as an anti-communist state, fought against the communisAbout Shiny Happy People, sale in the U."
      },
      "v_type": "Post"
    }
  ]}]
}

The above uses log-cosine as a similarity measurement. We can also use cosine similarity by looking at two persons' liked messages.

GSQL Recommendation Algorithm 2
use graph ldbc_snb
set query_timeout=60000
DROP QUERY RecommendMessage

CREATE QUERY RecommendMessage (String fn, String ln) SYNTAX v2{

  SumAccum<int> @MsgInCommon = 0;
  SumAccum<int> @MsgCnt = 0 ;
  SumAccum<int> @@InputPersonMsgCnt = 0;
  SumAccum<float> @SimilarityScore;
  SumAccum<float> @Rank;
  SumAccum<float> @TagCnt = 0;
  OrAccum @Liked = false;
  float sqrtOfInputPersonMsgCnt;

   #1. mark messages liked by input user
   #2. find common messages between input user and other persons
    Others =
       SELECT p
       FROM Person:s-(LIKES>)-:msg -(<LIKES)-:p
       WHERE s.firstName == fn AND s.lastName == ln
       ACCUM msg.@Liked = true, @@InputPersonMsgCnt += 1,
             p.@MsgInCommon += 1;

    sqrtOfInputPersonMsgCnt = sqrt(@@InputPersonMsgCnt);

    #calculate cosine similarity score.
    #|AxB|/(sqrt(Sum(A_i^2)) * sqrt(Sum(B_i^2)))
    Others  =
        SELECT o
        FROM Others:o-(LIKES>)-:msg
        ACCUM o.@MsgCnt += 1
        POST-ACCUM o.@SimilarityScore = o.@MsgInCommon/(sqrtOfInputPersonMsgCnt * sqrt(o.@MsgCnt));

   #recommend new messages to input user that have not been liked by him.
    RecommendedMessage =
             SELECT msg
             FROM Others:o-(LIKES>) - :msg
             WHERE  msg.@Liked == false
             ACCUM msg.@Rank +=o.@SimilarityScore
             ORDER BY msg.@Rank DESC
             LIMIT 3;

  PRINT   RecommendedMessage[RecommendedMessage.content, RecommendedMessage.@Rank];
}

INTERPRET QUERY RecommendMessage ("Viktor", "Akhiezer")
#try the second person with just parameter change.
INTERPRET QUERY RecommendMessage ("Adriaan", "Jong")