using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace BotSharp.NLP.Corpus { /// /// Fasttext labeled data reader /// public class FasttextDataReader { public List Read(ReaderOptions options) { if (String.IsNullOrEmpty(options.LabelPrefix)) { options.LabelPrefix = "__label__"; } var sentences = new List(); using (StreamReader reader = new StreamReader(Path.Combine(options.DataDir, options.FileName))) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!String.IsNullOrEmpty(line)) { var ms = Regex.Matches(line, options.LabelPrefix + @"\S+") .Cast() .ToList(); var text = line.Substring(ms.Last().Index + ms.Last().Length + 1); ms.ForEach(m => { sentences.Add(new Sentence { Label = m.Value.Substring(options.LabelPrefix.Length), Text = text }); }); } } } return sentences; } } }