ngram

package module
v0.0.0-...-e3980dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2022 License: MIT Imports: 3 Imported by: 0

README

NGram

Пакет предназначен для подготовки текста и преобразования слов в NGram для дальнейшего полнотекстового поиска

Тесты

go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NGram

type NGram struct {
	Texts []*Text // Тексты
	// contains filtered or unexported fields
}

Индексатор

func NewNGram

func NewNGram(size int) (ngram *NGram)

Create new object NGram

func (*NGram) AddText

func (gram *NGram) AddText(value string) error

Подготовка текста

func (*NGram) ListGrams

func (gram *NGram) ListGrams() (grams []string)

Список всех n-грамм

func (*NGram) ListTexts

func (gram *NGram) ListTexts() (texts []string)

Список всех текстов

func (*NGram) ListWords

func (gram *NGram) ListWords() (words []string)

Список всех слов

func (*NGram) Search

func (gram *NGram) Search(value string) string

Поиск наиболее подходящего значения

func (*NGram) SearchLimit

func (gram *NGram) SearchLimit(value string, limit float32) (result []NGramSearchResult)

Поиск с установленным порогом совпадения

func (*NGram) SearchLimitSQL

func (gram *NGram) SearchLimitSQL(
	tableNameTexts string,
	pkColumnNameTexts string,

	tableNameWords string,
	pkColumnNameWords string,

	tableNameTextsAndWords string,
	fkColumnNameWordsForTableWords string,
	fkColumnNameTextsForTableTexts string,

	tableNameNGrams string,
	pkColumnNameNGrams string,

	tableNameWordsAndNGrams string,
	columnNameNGram string,
	fkColumnNameWords2ForTableWords string,
	fkColumnNameNGramsForTableNGrams string,
) string

Генерирование SQL-запроса для выполнения в БД Для выполнения данного запроса потребуется примерно следующая структура таблиц:

CREATE TABLE texts (id BIGINT PRIMARY KEY, txt TEXT);
CREATE TABLE words (id BIGINT PRIMARY KEY, word VARCHAR (150));
CREATE TABLE ngram (id BIGINT PRIMARY KEY, ngram VARCHAR (20));
CREATE TABLE word_and_text (id_text BIGINT REFERENCES texts (id), id_word BIGINT REFERENCES words (id));
CREATE TABLE ngram_and_word (id_word  BIGINT REFERENCES words (id), id_ngram BIGINT REFERENCES ngram (id));

Соответственно таблицы и поля могут быть переименованы и дополнены в соответствии с нужными требованиями

type NGramSearchResult

type NGramSearchResult struct {
	Value string  // Результирующая строка
	Coeff float32 // Коэффициент соответствия
}

Результат поиска

type Text

type Text struct {
	Value string  // Текст (значение)
	Words []*Word // Слова
	Size  int     // Размерность n-грамм
}

Обработка текста

func BuildText

func BuildText(value string, size int) (text *Text, err error)

Разбивка текста на слова

func (*Text) Match

func (text *Text) Match(value string) float32

Определение соответствия такста заданному значению

type Word

type Word struct {
	Value string   // Слово
	Grams []string // n-граммы слова
	Size  int      // Размерность n-грамм
}

Слово

func BuildWord

func BuildWord(value string, size int) (word *Word, err error)

Разбиваем слово на n-граммы

func (*Word) Match

func (word *Word) Match(value string) float32

Коэффициент совпадения [0;1]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL