You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Semantic/Helpers/TextAnalizer.cs

118 lines
4.0 KiB

6 years ago
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ZeroLevel.Services.Semantic;
namespace ZeroLevel.Implementation.Semantic.Helpers
{
public static class TextAnalizer
{
internal static readonly Regex ReWord = new Regex("\\b[\\wА-Яа-я-]+\\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
5 years ago
internal static readonly Regex ReRuWord = new Regex("\\b[А-Яа-я-]+\\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
6 years ago
/// <summary>
6 years ago
/// Highlighting words from text
6 years ago
/// </summary>
6 years ago
/// <param name="text">Text</param>
/// <returns>Words</returns>
6 years ago
public static IEnumerable<string> ExtractWords(string text)
{
var result = new List<string>();
foreach (Match match in ReWord.Matches(text))
{
result.Add(match.Value);
}
return result;
}
5 years ago
public static IEnumerable<string> ExtractRuWords(string text)
{
var result = new List<string>();
foreach (Match match in ReRuWord.Matches(text))
{
result.Add(match.Value);
}
return result;
}
6 years ago
/// <summary>
6 years ago
/// Highlighting unique words from text
6 years ago
/// </summary>
6 years ago
/// <param name="text">Text</param>
/// <returns>List of unique words</returns>
6 years ago
public static IEnumerable<string> ExtractUniqueWords(string text)
{
return new HashSet<string>(ExtractWords(text));
}
/// <summary>
6 years ago
/// Highlighting unique words from text without stop words
6 years ago
/// </summary>
6 years ago
/// <param name="text">Text</param>
/// <returns>List of unique words without stop words</returns>
6 years ago
public static IEnumerable<string> ExtractUniqueWordsWithoutStopWords(string text)
{
return new HashSet<string>(ExtractUniqueWords(text).Where(w => StopWords.IsStopWord(w) == false));
}
/// <summary>
6 years ago
/// Extract tokens from text
6 years ago
/// </summary>
6 years ago
/// <param name="text">Text</param>
/// <returns>Tokens</returns>
6 years ago
public static IEnumerable<WordToken> ExtractWordTokens(string text)
{
var result = new List<WordToken>();
foreach (Match match in ReWord.Matches(text))
{
result.Add(new WordToken(match.Value, match.Index));
}
return result;
}
5 years ago
public static IEnumerable<WordToken> ExtractWordTokens(string[] words)
{
var result = new List<WordToken>();
for (int i = 0; i < words.Length; i++)
{
result.Add(new WordToken(words[i], i));
}
return result;
}
6 years ago
/// <summary>
6 years ago
/// Selection of unique tokens from the text (first entry)
6 years ago
/// </summary>
6 years ago
/// <param name="text">Text</param>
/// <returns>List of unique tokens</returns>
6 years ago
public static IEnumerable<WordToken> ExtractUniqueWordTokens(string text)
{
return ExtractWordTokens(text).DistinctBy(t => t.Word);
}
5 years ago
public static IEnumerable<WordToken> ExtractUniqueWordTokens(string[] words)
{
return ExtractWordTokens(words).DistinctBy(t => t.Word);
}
6 years ago
/// <summary>
6 years ago
/// Allocation of unique tokens from text with drop of stop words
6 years ago
/// </summary>
6 years ago
/// <param name="text">Text</param>
/// <returns>List of unique tokens without stop words</returns>
6 years ago
public static IEnumerable<WordToken> ExtractUniqueWordTokensWithoutStopWords(string text)
{
return ExtractWordTokens(text).DistinctBy(t => t.Word).Where(t => StopWords.IsStopWord(t.Word) == false);
}
5 years ago
public static IEnumerable<WordToken> ExtractUniqueWordTokensWithoutStopWords(string[] words)
{
return ExtractWordTokens(words).DistinctBy(t => t.Word).Where(t => StopWords.IsStopWord(t.Word) == false);
}
6 years ago
}
}

Powered by TurnKey Linux.