Class NGramModel

Namespace
NOpenNLP.Tools.Ngram
Assembly
NOpenNLP.Tools.dll

The NGramModel can be used to crate ngrams and character ngrams.

public class NGramModel : IEnumerable<StringList>, IEnumerable
Inheritance
NGramModel
Implements
Derived
Inherited Members

Constructors

NGramModel()

Initializes an empty instance.

public NGramModel()

NGramModel(Stream)

Initializes the current instance.

public NGramModel(Stream @in)

Parameters

in Stream

the serialized model stream

Exceptions

IOException

Fields

COUNT

protected const string COUNT = "count"

Field Value

string

Properties

Count

Retrieves the number of StringList entries in the current instance.

public virtual int Count { get; }

Property Value

int

number of different grams

NumberOfGrams

Retrieves the total count of all Ngrams.

public virtual int NumberOfGrams { get; }

Property Value

int

total count of all ngrams

Methods

Add(StringList)

Adds one NGram, if it already exists the count increase by one.

public virtual void Add(StringList ngram)

Parameters

ngram StringList

Add(StringList, int, int)

Adds NGrams up to the specified length to the current instance.

public virtual void Add(StringList ngram, int minLength, int maxLength)

Parameters

ngram StringList

the tokens to build the uni-grams, bi-grams, tri-grams, .. from.

minLength int
  • minimal length
maxLength int
  • maximal length

Add(string, int, int)

Adds character NGrams to the current instance.

public virtual void Add(string chars, int minLength, int maxLength)

Parameters

chars string
minLength int
maxLength int

Contains(StringList)

Checks fit he given tokens are contained by the current instance.

public virtual bool Contains(StringList tokens)

Parameters

tokens StringList

Returns

bool

true if the ngram is contained

Cutoff(int, int)

Deletes all ngram which do appear less than the cutoffUnder value and more often than the cutoffOver value.

public virtual void Cutoff(int cutoffUnder, int cutoffOver)

Parameters

cutoffUnder int
cutoffOver int

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

GetCount(StringList)

Retrieves the count of the given ngram.

public virtual int GetCount(StringList ngram)

Parameters

ngram StringList

an ngram

Returns

int

count of the ngram or 0 if it is not contained

GetEnumerator()

Retrieves an IEnumerator<T> over all StringList entries.

public IEnumerator<StringList> GetEnumerator()

Returns

IEnumerator<StringList>

iterator over all grams

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

Remove(StringList)

Removes the specified tokens form the NGram model, they are just dropped.

public virtual void Remove(StringList tokens)

Parameters

tokens StringList

Serialize(Stream)

Writes the ngram instance to the given Stream.

public virtual void Serialize(Stream @out)

Parameters

out Stream

the Stream to write the ngram instance into.

Exceptions

IOException

if writing to the Stream fails

SetCount(StringList, int)

Sets the count of an existing ngram.

public virtual void SetCount(StringList ngram, int count)

Parameters

ngram StringList
count int

ToDictionary()

Creates a dictionary which contain all StringList which are in the current NGramModel.

Entries which are only different in the case are merged into one.

Calling this method is the same as calling ToDictionary(bool) with true.

public virtual Dictionary ToDictionary()

Returns

Dictionary

a dictionary of the ngrams

ToDictionary(bool)

Creates a dictionary which contains all StringLists which are in the current NGramModel.

public virtual Dictionary ToDictionary(bool caseSensitive)

Parameters

caseSensitive bool

Specifies whether case distinctions should be kept in the creation of the dictionary.

Returns

Dictionary

a dictionary of the ngrams

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.

See Also