Repository navigation
Expand file tree
/
Copy pathTokenizerFactory.cs
More file actions
78 lines (62 loc) · 2.44 KB
/
Copy pathTokenizerFactory.cs
File metadata and controls
78 lines (62 loc) · 2.44 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
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
{
private SupportedLanguage _lang;
private ITokenizer _tokenizer;
private TokenizationOptions _options;
public ITokenizer GetTokenizer<ITokenize>() where ITokenize : ITokenizer, new()
{
return _tokenizer = new ITokenize();
}
public ITokenizer GetTokenizer(string name)
{
List<Type> types = new List<Type>();
types.AddRange(Assembly.Load(new AssemblyName("BotSharp.Core"))
.GetTypes().Where(x => !x.IsAbstract && !x.FullName.StartsWith("<>f__AnonymousType")).ToList());
types.AddRange(Assembly.Load(new AssemblyName("BotSharp.NLP"))
.GetTypes().Where(x => !x.IsAbstract && !x.FullName.StartsWith("<>f__AnonymousType")).ToList());
Type type = types.FirstOrDefault(x => x.Name == name);
var instance = (ITokenizer)Activator.CreateInstance(type);
return _tokenizer = instance;
}
public TokenizerFactory(TokenizationOptions options, SupportedLanguage lang)
{
_lang = lang;
_options = options;
}
public List<Token> Tokenize(string sentence)
{
var tokens = _tokenizer.Tokenize(sentence, _options);
tokens.ForEach(x => x.Lemma = x.Text.ToLower());
return tokens;
}
public List<Sentence> Tokenize(List<String> sentences)
{
var sents = sentences.Select(s => new Sentence { Text = s }).ToList();
Parallel.ForEach(sents, (sentence) =>
{
sentence.Words = Tokenize(sentence.Text);
sentence.Words.ForEach(x => x.Lemma = x.Text.ToLower());
});
return sents;
}
private class ParallelToken
{
public String Text { get; set; }
public List<Token> Tokens { get; set; }
}
}
}