0 people like it.

Get entity names from Freebase type provider

This sample uses the internals of the Freebase type provider to get the names of all the entities that Freebase knows about. The snippet returns the type names, so some of the type provider internals are exposed, but it still returns nice list of entities.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
#if INTERACTIVE
#r "FSharp.Data.dll"
#r "FSharp.Data.DesignTime.dll"
#endif

// NOTE: This code is using the type provider internals and so it needs
// reference to both "FSharp.Data.dll" and "FSharp.Data.DesignTime.dll"
open System
open FSharp.Data
open ProviderImplementation

// Initialize the type provider & get ITypeProvider implementation
let loc = typeof<FSharp.Data.CsvFile>.Assembly.Location
let fl _ = false
let config = CompilerServices.TypeProviderConfig(fl, RuntimeAssembly=loc)
let fb = new FreebaseTypeProvider(config)
let tp = (fb :> CompilerServices.ITypeProvider)

// Given a System.Type, get the names of all its nested 
// types (recursively until 'level' reaches zero)
let rec getTypeNames level (typ:System.Type) = seq {
  for nested in typ.GetNestedTypes() do
    yield nested.Name 
    if level > 0 then 
      yield! getTypeNames (level - 1) nested }

// Get the types in the Freebase namespace 
// (recursively using 'getTypeNames')
let getFreebaseTypes level = 
  set [ for ns in tp.GetNamespaces() do
          for t in ns.GetTypes() do
            yield! t |> getTypeNames level ]

// For "level=2", this returns reasonable type names
// (representing the different entities in Freebase)
for n in getFreebaseTypes 2 do 
  printfn "%s" n
namespace System
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Data
val loc : string

Full name: Script.loc
val typeof<'T> : Type

Full name: Microsoft.FSharp.Core.Operators.typeof
val fl : 'a -> bool

Full name: Script.fl
val config : CompilerServices.TypeProviderConfig

Full name: Script.config
namespace Microsoft.FSharp.Core.CompilerServices
Multiple items
type TypeProviderConfig =
  new : systemRuntimeContainsType:(string -> bool) -> TypeProviderConfig
  member SystemRuntimeContainsType : string -> bool
  member IsHostedExecution : bool
  member IsInvalidationSupported : bool
  member ReferencedAssemblies : string []
  member ResolutionFolder : string
  member RuntimeAssembly : string
  member SystemRuntimeAssemblyVersion : Version
  member TemporaryFolder : string
  member IsHostedExecution : bool with set
  ...

Full name: Microsoft.FSharp.Core.CompilerServices.TypeProviderConfig

--------------------
new : systemRuntimeContainsType:(string -> bool) -> CompilerServices.TypeProviderConfig
val fb : CompilerServices.ITypeProvider

Full name: Script.fb
val tp : CompilerServices.ITypeProvider

Full name: Script.tp
type ITypeProvider =
  interface
    inherit IDisposable
    abstract member ApplyStaticArguments : typeWithoutArguments:Type * typePathWithArguments:string [] * staticArguments:obj [] -> Type
    abstract member GetGeneratedAssemblyContents : assembly:Assembly -> byte []
    abstract member GetInvokerExpression : syntheticMethodBase:MethodBase * parameters:Expr [] -> Expr
    abstract member GetNamespaces : unit -> IProvidedNamespace []
    abstract member GetStaticParameters : typeWithoutArguments:Type -> ParameterInfo []
    abstract member add_Invalidate : EventHandler -> unit
    abstract member Invalidate : IEvent<EventHandler,EventArgs>
    abstract member remove_Invalidate : EventHandler -> unit
  end

Full name: Microsoft.FSharp.Core.CompilerServices.ITypeProvider
val getTypeNames : level:int -> typ:Type -> seq<string>

Full name: Script.getTypeNames
val level : int
val typ : Type
type Type =
  inherit MemberInfo
  member Assembly : Assembly
  member AssemblyQualifiedName : string
  member Attributes : TypeAttributes
  member BaseType : Type
  member ContainsGenericParameters : bool
  member DeclaringMethod : MethodBase
  member DeclaringType : Type
  member Equals : o:obj -> bool + 1 overload
  member FindInterfaces : filter:TypeFilter * filterCriteria:obj -> Type[]
  member FindMembers : memberType:MemberTypes * bindingAttr:BindingFlags * filter:MemberFilter * filterCriteria:obj -> MemberInfo[]
  ...

Full name: System.Type
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val nested : Type
Type.GetNestedTypes() : Type []
Type.GetNestedTypes(bindingAttr: Reflection.BindingFlags) : Type []
property Reflection.MemberInfo.Name: string
val getFreebaseTypes : level:int -> Set<string>

Full name: Script.getFreebaseTypes
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val ns : CompilerServices.IProvidedNamespace
abstract member CompilerServices.ITypeProvider.GetNamespaces : unit -> CompilerServices.IProvidedNamespace []
val t : Type
abstract member CompilerServices.IProvidedNamespace.GetTypes : unit -> Type []
val n : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn

More information

Link:http://fssnip.net/nF
Posted:10 years ago
Author:Tomas Petricek
Tags: freebase , type providers