Documentation

SentenceService

Service class for sentence operations.

Tags
category

Lwt

author

HugoFara hugo.farajallah@protonmail.com

license

Unlicense http://unlicense.org/

link
https://hugofara.github.io/lwt/developer/api
since
3.0.0

Table of Contents

Properties

$textParsingService  : TextParsingService

Methods

__construct()  : mixed
Constructor - initialize dependencies.
findSentencesFromWord()  : array<int, array<string, mixed>>
Perform a SQL query to find sentences containing a word.
formatSentence()  : array<string|int, string>
Format the sentence(s) $seid containing $wordlc highlighting $wordlc.
get20Sentences()  : string
Show 20 sentences containing $wordlc.
getSentenceAtPosition()  : string|null
Get the sentence text at a specific position in a text.
getSentencesWithWord()  : array<string|int, array<string|int, string>>
Return sentences containing a word.
getSentenceText()  : string|null
Get the formatted text of a sentence by its ID.
renderExampleSentencesArea()  : string
Render the area for example sentences of a word.
convertZwsToSpacing()  : string
Convert zero-width space (ZWS) markers to proper spacing.
executeSentencesContainingWordQuery()  : array<int, array<string, mixed>>
Execute a SQL query to find sentences containing a word (complex search).
extractCenteredPortion()  : string
Extract a centered portion of text.
extractPortionAroundWord()  : string
Extract a portion of text centered around a specific word.
ownsSentence()  : bool
Check whether the parent text of a sentence is owned by the current user. In single-user mode this is always true. Used as a guard before {@see formatSentence} runs its content-fetching SQL — that SQL joins `word_occurrences` and `languages` with no UsID column to filter on, so an arbitrary SeID would otherwise return another user's sentence text.
parentTextUserScope()  : string
Build a parent-row user-scope clause for queries that touch the `sentences` table. The `sentences` table has no UsID column of its own — ownership is derived from the parent `texts` row via SeTxID.

Properties

Methods

findSentencesFromWord()

Perform a SQL query to find sentences containing a word.

public findSentencesFromWord(int|null $wid, string $wordlc, int $lid[, int $limit = -1 ]) : array<int, array<string, mixed>>
Parameters
$wid : int|null

Word ID or mode

  • null: use $wordlc instead, simple search
  • -1: use $wordlc with a more complex search
  • 0 or above: sentences containing $wid
$wordlc : string

Word to look for in lowercase

$lid : int

Language ID

$limit : int = -1

Maximum number of sentences to return

Return values
array<int, array<string, mixed>>

Query result rows

formatSentence()

Format the sentence(s) $seid containing $wordlc highlighting $wordlc.

public formatSentence(int $seid, string $wordlc, int $mode) : array<string|int, string>
Parameters
$seid : int

Sentence ID

$wordlc : string

Term text in lower case

$mode : int
  • Up to 1: return only the current sentence
  • Above 1: return previous sentence and current sentence
  • Above 2: return previous, current and next sentence
Return values
array<string|int, string>

[0]=html, word in bold, [1]=text, word in }

get20Sentences()

Show 20 sentences containing $wordlc.

public get20Sentences(int $lang, string $wordlc, int|null $wid, string $targetCtlId, int $mode) : string
Parameters
$lang : int

Language ID

$wordlc : string

Term in lower case.

$wid : int|null

Word ID

$targetCtlId : string

ID of the target textarea element

$mode : int
  • Up to 1: return only the current sentence
  • Above 1: return previous and current sentence * Above 2: return previous, current and next sentence
Return values
string

HTML-formatted string of which elements are candidate sentences to use.

getSentenceAtPosition()

Get the sentence text at a specific position in a text.

public getSentenceAtPosition(int $textId, int $position) : string|null

This method extracts the sentence containing the word at the given position. It handles cases where texts weren't properly split into sentences during parsing by finding sentence boundaries (punctuation) around the target position.

Parameters
$textId : int

Text ID

$position : int

Word position (Ti2Order)

Return values
string|null

The sentence containing the word, or null if not found

getSentencesWithWord()

Return sentences containing a word.

public getSentencesWithWord(int $lang, string $wordlc, int|null $wid[, int|null $mode = 0 ][, int $limit = 20 ]) : array<string|int, array<string|int, string>>
Parameters
$lang : int

Language ID

$wordlc : string

Word to look for in lowercase

$wid : int|null

Word ID

  • null: use $wordlc instead, simple search
  • -1: use $wordlc with a more complex search
  • 0 or above: find sentences containing $wid
$mode : int|null = 0

Sentences to get:

  • Up to 1 is 1 sentence,
  • 2 is previous and current sentence,
  • 3 is previous, current and next one
$limit : int = 20

Maximum number of sentences to return

Return values
array<string|int, array<string|int, string>>

Array of sentences found

getSentenceText()

Get the formatted text of a sentence by its ID.

public getSentenceText(int $seid) : string|null

Reconstructs the sentence from word_occurrences table with proper spacing. Use this instead of reading SeText directly from sentences table.

Parameters
$seid : int

Sentence ID

Return values
string|null

Formatted sentence text, or null if not found

renderExampleSentencesArea()

Render the area for example sentences of a word.

public renderExampleSentencesArea(int $lang, string $termlc, string $targetCtlId, int $wid) : string
Parameters
$lang : int

Language ID

$termlc : string

Term text in lowercase

$targetCtlId : string

ID of the target textarea element

$wid : int

Word ID

Return values
string

HTML output

convertZwsToSpacing()

Convert zero-width space (ZWS) markers to proper spacing.

private convertZwsToSpacing(string $text, string $termchar) : string

For languages that use spaces between words (LgRemoveSpaces = 0), this method converts ZWS markers in the text to actual spaces where appropriate (between words and after punctuation).

Parameters
$text : string

Text with ZWS markers between tokens

$termchar : string

Language's word character regex pattern

Return values
string

Text with proper spacing

executeSentencesContainingWordQuery()

Execute a SQL query to find sentences containing a word (complex search).

private executeSentencesContainingWordQuery(string $wordlc, int $lid[, int $limit = -1 ]) : array<int, array<string, mixed>>
Parameters
$wordlc : string

Word to look for in lowercase

$lid : int

Language ID

$limit : int = -1

Maximum number of sentences to return

Return values
array<int, array<string, mixed>>

Query result rows

extractCenteredPortion()

Extract a centered portion of text.

private extractCenteredPortion(string $text, int $maxLength) : string
Parameters
$text : string

The text to extract from

$maxLength : int

Maximum length of the result

Return values
string

The extracted portion

extractPortionAroundWord()

Extract a portion of text centered around a specific word.

private extractPortionAroundWord(string $text, string $word, int $maxLength) : string
Parameters
$text : string

The text to extract from

$word : string

The word to center around

$maxLength : int

Maximum characters on each side of the word

Return values
string

The extracted portion

ownsSentence()

Check whether the parent text of a sentence is owned by the current user. In single-user mode this is always true. Used as a guard before {@see formatSentence} runs its content-fetching SQL — that SQL joins `word_occurrences` and `languages` with no UsID column to filter on, so an arbitrary SeID would otherwise return another user's sentence text.

private ownsSentence(int $seid) : bool
Parameters
$seid : int
Return values
bool

parentTextUserScope()

Build a parent-row user-scope clause for queries that touch the `sentences` table. The `sentences` table has no UsID column of its own — ownership is derived from the parent `texts` row via SeTxID.

private parentTextUserScope(array<int, mixed> &$bindings) : string

Returns an SQL fragment like AND SeTxID IN (SELECT TxID FROM texts WHERE TxUsID = ?) and pushes the current user ID onto $bindings, or an empty string when multi-user mode is off / no user is authenticated. Without this gate, raw FROM sentences … queries leak rows from every user's texts as long as the language ID matches.

Parameters
$bindings : array<int, mixed>

Reference to bindings array

Return values
string

SQL fragment (with leading space) or empty string


        
On this page

Search results