0

I am SQL developer and am really new to both F# and C#. I need help on how to pass a string to f# function below and to return the result from F# to C#.

Description of project: I am using stanford postagger to tag a sentence with the parts of speech. Reference link from where i copied this code. (http://sergey-tihon.github.io/Stanford.NLP.NET/StanfordPOSTagger.html)

module File1


open java.io
open java.util
open edu.stanford.nlp.ling
open edu.stanford.nlp.tagger.maxent

// Path to the folder with models

let modelsDirectry = 
__SOURCE_DIRECTORY__  + @'..\stanford-postagger-2013-06-20\models\'

// Loading POS Tagger

let tagger = MaxentTagger(modelsDirectry + 'wsj-0-18-bidirectional-nodistsim.tagger')

let tagTexrFromReader (reader:Reader) = 
let sentances = MaxentTagger.tokenizeText(reader).toArray()

sentances |> Seq.iter (fun sentence ->
    let taggedSentence = tagger.tagSentence(sentence :?> ArrayList)
    printfn "%O" (Sentence.listToString(taggedSentence, false))
)


// Text for tagging

let text = System.Console.ReadLine();

tagTexrFromReader <| new StringReader(text)

1 Answers1

0

it won't matter if C# or F# - do make a function that gets a string and returns ... let s say an int, you just need something like this (put it in some MyModule.fs):

namespace MyNamespace

module MyModule =

   // this is your function with one argument (a string named input) and result of int
   let myFun (input : string) : int =
      // do whatever you have to
      5 // the value of the last line will be your result - in this case a integer 5

call it in from C#/.net with

int result = MyNamespace.MyModule.myFun ("Hallo");

I hope this helps you out a bit

For your example this would be:

let myFun (text : string) =
   use reader = new StringReader(text)
   tagTexrFromReader reader

as you'll have this in the module File1 you can just call it with var res = Fiel1.myFun(text);

BTW: use is in there because StringReader is IDisposable and using use F# will dispose the object when you exit the scope.

PS: is tagTexrFromReader a typo?

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • Hi Carsten, Thank you for the quick reply. I tried coding the same in the above file1 module but ended up with errors. It would be great if you can please provide me a code that fits in module file1 mentioned above. Thanks again for your patience. – user3898531 Aug 01 '14 at 10:00
  • what are the errors? the only **magic** is the `let myFun input = output` - you just have somthing similar into your module. I would give a direct translation but I cannot see what you are really trying to return – Random Dev Aug 01 '14 at 10:32
  • Sorry Carsten, I should have given you details about what i need exactly. I need to pass the variable text123 from c# to tagTexrFromReader <| new StringReader(text123) and needed the output Sentence.listToString(taggedSentence, false) returned to c#.. – user3898531 Aug 01 '14 at 10:53
  • @user3898531 It appears you are starting with no knowledge of how to do C#/F# interop. You will probably be better off finding a blog post or tutorial on how to do that rather than asking questions on SO. Also see http://stackoverflow.com/questions/478531/call-f-code-from-c-sharp – N_A Aug 01 '14 at 15:05
  • Thanks Carsten for your help..i understand as the questions im asking are of basic category.. – user3898531 Aug 01 '14 at 17:45