Best way to create a weighted edge graph

Hi all, I’m new to graph databases, but I was showing my daughter how to make a random chatbot the other day and thought I’d try and use Dgraph as a backend for it. The bot’s graph has weighted edges, however, and nothing I’ve tried is working :sob:

The bot’s real basic, all it does is analyse sentences and store the frequencies that words follow one another. If I give it the following sentences:

  • “what is the time”
  • “the time is 4pm”

Analysing those as its first input would populate its database with:

  [start of sentence]: followed by ("what": 1 time, "the": 1 time)
  "what": followed by............. ("is": 1 time)
  "is": followed by................("the": 1 time, "4pm": 1 time)
  "the": followed by...............("time": 1 time)
  "time": followed by..............([end of sentence]: 1 time, "is": 1 time)
  "4pm": followed by...............([end of sentence]: 1 time)

My original thought was to have a schema like this:

type Word {
  text: String! @id
}
type Transition {
  from: Word 
  to: Word
  count: Int!
}

The Transition nodes would do the heavy lifting, the Word nodes are only really there for the Transitions to link to. Nulls in Transition.from and Transition.to would indicate the start and end of a sentence.

It’s pretty easy to find sentence-starting Transitions this way:

query GetFirstWords {
  queryTransition(filter: {not: {has: from}}) {
    to {
      text
    }
    count
  }
}

Except I can’t figure out how to continue from there! I’ve tried adding ID and @search and @hasinverse in various places, but no joy. Pretty much everything I’ve tried results in one of the following two errors:

Field 'from' is not defined by type 'TransitionFilter'.```

or

Syntax Error: Expected Name, found String 'time'.

I don’t feel like I have the data structure right, but I’ve no idea what else to try so any help would be amazing! (Even if that help is telling me I’m trying to do something GraphQL doesn’t do!)

Something is odd in your end. I have copied your schema, run in my end. And tried your query. It just runs, no errors.

@MichelDiz, sorry, the query there works for me… but it’s the only query I’ve managed to get working!

I was trying to figure out the next query, the "give me to.text and count from words where from.text is “whatever”. I’ve been trying things like:

query GetNextWords {
  queryTransition(filter: {from: {text: {eq: "the"}}})
    to {
      text
    }
    count
  }
}

You should be able to see aggregation queries generated by Dgraph. Use GraphQL Playground or any other that you can explore the documentation generated by GraphQL. Count is possible with aggregation queries in GraphQL. Another way is creating custom DQL queries. But that is a another level. You need more exposure time with Dgraph.

BTW, I think you should pick DQL instead of GraphQL. GraphQL is a bit hard, the spec is pretty limited and strict. It is nice when you know how to get around with the lang. And when you are using for front-end. But applications like AI, I think DQL is way better.

Looking at it from another angle…

Weighted edges can be accomplished thru edge facets. Dgraph supports that, but only in DQL. See https://dgraph.io/docs/query-language/facets/

Thank you, I’ll check them out. I think I need to go through the DQL tutorials now, since both you and @MichelDiz recommended that over GraphQL.