BotSharp/BotSharp.NLP/Tokenize/TokenizerFactory.cs

55 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2018-09-06 22:32:51 +00:00
using System.Linq;
using System.Text;
2018-09-06 22:32:51 +00:00
using System.Threading.Tasks;
namespace BotSharp.NLP.Tokenize
{
/// <summary>
/// BotSharp Tokenizer Factory
/// Tokenizers divide strings into lists of substrings.
/// The particular tokenizer requires implement interface
/// models to be installed.BotSharp.NLP also provides a simpler, regular-expression based tokenizer, which splits text on whitespace and punctuation.
/// </summary>
public class TokenizerFactory<ITokenize> where ITokenize : ITokenizer, new()
{
private SupportedLanguage _lang;
private ITokenize _tokenizer;
private TokenizationOptions _options;
public TokenizerFactory(TokenizationOptions options, SupportedLanguage lang)
{
_lang = lang;
_options = options;
_tokenizer = new ITokenize();
}
public List<Token> Tokenize(string sentence)
{
return _tokenizer.Tokenize(sentence, _options);
}
2018-09-06 22:32:51 +00:00
2018-09-10 03:56:32 +00:00
public List<Sentence> Tokenize(List<String> sentences)
2018-09-06 22:32:51 +00:00
{
2018-09-10 03:56:32 +00:00
var sents = sentences.Select(s => new Sentence { Text = s }).ToList();
2018-09-06 22:32:51 +00:00
Parallel.ForEach(sents, (sentence) =>
{
2018-09-10 03:56:32 +00:00
sentence.Words = Tokenize(sentence.Text);
2018-09-06 22:32:51 +00:00
});
2018-09-10 03:56:32 +00:00
return sents;
2018-09-06 22:32:51 +00:00
}
private class ParallelToken
{
public String Text { get; set; }
public List<Token> Tokens { get; set; }
}
}
}