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: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
#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 ]

// Freebase type provider creates types with special names that 
// represent individuals, collections etc. We can skip them.
let specialTypeName (n:string) =
  n.EndsWith("Individuals") || 
  n.EndsWith("Individuals10") || 
  n.EndsWith("Individuals100") || 
  n.EndsWith("IndividualsAZ") || 
  n.EndsWith("Data") || 
  n.EndsWith("DataCollection")

// For "level=2", this returns reasonable type names
// (representing the different entities in Freebase)

// For "level=3", this returns more fine-grained type
// names (including some individuals with special properties)
for n in getFreebaseTypes 2 do 
  if not (specialTypeName n) then 
    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 specialTypeName : n:string -> bool

Full name: Script.specialTypeName
val n : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
String.EndsWith(value: string) : bool
String.EndsWith(value: string, comparisonType: StringComparison) : bool
String.EndsWith(value: string, ignoreCase: bool, culture: Globalization.CultureInfo) : bool
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

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

More information

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