Analyze Entity Co-Occurrence

User Intent

"How do I find content where multiple entities appear together? Show me entity co-occurrence patterns."

Operation

SDK Methods: queryContents() with multiple entity filters GraphQL: Multi-entity content queries Use Case: Relationship discovery through co-occurrence

Prerequisites

  • Content with extracted entities

  • Multiple entities to analyze

  • Understanding of entity relationships


Complete Code Example (TypeScript)

import { Graphlit } from 'graphlit-client';
import { ObservableTypes } from 'graphlit-client/dist/generated/graphql-types';

const graphlit = new Graphlit();

// Find entities
const kirk = await graphlit.queryObservables({
  search: "Kirk Marple",
  filter: { types: [ObservableTypes.Person] }
});

const maria = await graphlit.queryObservables({
  search: "Maria Garcia",
  filter: { types: [ObservableTypes.Person] }
});

// Find co-occurrence
const cooccurrence = await graphlit.queryContents({
  filter: {
    observations: [
      { observable: { id: kirk.observables.results[0].observable.id } },
      { observable: { id: maria.observables.results[0].observable.id } }
    ]
  }
});

console.log(`Kirk and Maria mentioned together in ${cooccurrence.contents.results.length} documents`);

// Build co-occurrence matrix
const people = await graphlit.queryObservables({
  filter: { types: [ObservableTypes.Person] }
});

const matrix = new Map<string, Map<string, number>>();

for (let i = 0; i < people.observables.results.length; i++) {
  for (let j = i + 1; j < people.observables.results.length; j++) {
    const personA = people.observables.results[i];
    const personB = people.observables.results[j];
    
    const together = await graphlit.queryContents({
      filter: {
        observations: [
          { observable: { id: personA.observable.id } },
          { observable: { id: personB.observable.id } }
        ]
      }
    });
    
    const count = together.contents.results.length;
    if (count > 0) {
      if (!matrix.has(personA.observable.name)) {
        matrix.set(personA.observable.name, new Map());
      }
      matrix.get(personA.observable.name)!.set(personB.observable.name, count);
    }
  }
}

// Display top co-occurrences
console.log('\nTop co-occurrences:');
const flat: Array<{ a: string; b: string; count: number }> = [];
matrix.forEach((bMap, a) => {
  bMap.forEach((count, b) => {
    flat.push({ a, b, count });
  });
});

flat.sort((x, y) => y.count - x.count)
  .slice(0, 10)
  .forEach(({ a, b, count }) => {
    console.log(`  ${a}${b}: ${count} times`);
  });

Key Patterns

1. Pairwise Co-occurrence

Two specific entities:

observations: [
  { observable: { id: entityA } },
  { observable: { id: entityB } }
]

2. Entity + Type

Specific entity with any of type:

observations: [
  { observable: { id: personId } },
  { type: ObservableTypes.Organization }
]

3. Co-occurrence Strength

Frequency indicates relationship strength


Use Cases

Team Collaboration Analysis: Who works with whom Influence Mapping: Entity co-mention networks Topic Association: Products + Organizations Relationship Discovery: Find hidden connections


Developer Hints

  • Co-occurrence implies relationship

  • Frequency = strength

  • Useful for network analysis

  • Export for graph visualization

  • Can be computationally expensive (cache results)


Last updated

Was this helpful?