Skip to content

Commit

Permalink
segment and align USX texts
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaspit committed Jan 15, 2018
1 parent 6a91cd5 commit 6766487
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ export class MachineService {
}
}

initialise(projectId: string, isScripture: boolean): void {
initialise(projectId: string): void {
this.engine = new TranslationEngine(this.$window.location.origin + '/machine', projectId);
this.updateConfidence();
const segmentType = isScripture ? 'line' : 'latin';
this.sourceSegmentTokenizer = new SegmentTokenizer(segmentType);
this.targetSegmentTokenizer = new SegmentTokenizer(segmentType);
this.sourceSegmentTokenizer = new SegmentTokenizer('latin');
this.targetSegmentTokenizer = new SegmentTokenizer('latin');
}

translate(sourceSegment: string): angular.IPromise<void> {
Expand Down
89 changes: 36 additions & 53 deletions src/angular-app/languageforge/translate/editor/document-editor.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import * as angular from 'angular';
import Quill, { RangeStatic } from 'quill';

import { InputSystem } from '../../../bellows/shared/model/input-system.model';
import { DocType, SaveState } from '../core/constants';
import { MachineService } from '../core/machine.service';
import { RealTimeService } from '../core/realtime.service';
import { MetricService } from './metric.service';
import { SuggestionsTheme } from './quill/suggestions-theme';
import { Segment } from './segment';
import { MachineSegmenter, Segmenter, UsxSegmenter } from './segmenter';

export abstract class DocumentEditor {
static isSelectionCollapsed(selection: RangeStatic): boolean {
return selection != null && selection.length === 0;
}

static isTextEmpty(text: string): boolean {
text = text.endsWith('\n') ? text.substr(0, text.length - 1) : text;
return text === '';
}

modulesConfig: any = {};
inputSystem: any = {};
inputSystem: InputSystem = new InputSystem();

protected currentSegment: Segment;
protected segmentRanges: RangeStatic[];
protected segmenter: Segmenter;
private _isScripture: boolean = false;

private documentSetId: string = '';
private readonly _created: angular.IDeferred<boolean>;
Expand All @@ -31,6 +30,7 @@ export abstract class DocumentEditor {
constructor(private readonly $q: angular.IQService, protected readonly machine: MachineService,
private readonly realTime: RealTimeService) {
this._created = this.$q.defer();
this.segmenter = new MachineSegmenter(this, machine);
}

abstract get docType(): string;
Expand All @@ -57,8 +57,8 @@ export abstract class DocumentEditor {
return this.currentSegment == null ? '' : this.currentSegment.documentSetId;
}

get currentSegmentIndex(): number {
return this.currentSegment == null ? -1 : this.currentSegment.index;
get currentSegmentRef(): string {
return this.currentSegment == null ? '' : this.currentSegment.ref;
}

get saveState(): SaveState {
Expand All @@ -69,11 +69,22 @@ export abstract class DocumentEditor {
return DocumentEditor.isTextEmpty(this.quill.getText());
}

get isScripture(): boolean {
return this._isScripture;
}

set isScripture(value: boolean) {
if (value !== this._isScripture) {
this._isScripture = value;
this.segmenter = value ? new UsxSegmenter(this) : new MachineSegmenter(this, this.machine);
}
}

openDocumentSet(collection: string, documentSetId: string): void {
if (this.documentSetId !== documentSetId) {
this.documentSetId = documentSetId;
this.realTime.createAndSubscribeRichTextDoc(collection, this.docId, this.quill);
this.segmentRanges = null;
this.segmenter.reset();
}
}

Expand All @@ -86,22 +97,18 @@ export abstract class DocumentEditor {
}

update(textChange: boolean): boolean {
if (this.segmentRanges == null || textChange) {
this.segmentRanges = this.getSegmentRanges();
}
this.segmenter.update(textChange);

const selection = this.quill.getSelection();
if (selection == null) {
return false;
}
let segmentIndex = -1;
if (DocumentEditor.isSelectionCollapsed(selection)) {
segmentIndex = this.segmentRanges.findIndex(range => selection.index <= range.index + range.length);
}
if (segmentIndex === -1) {
segmentIndex = this.currentSegment == null ? this.segmentRanges.length - 1 : this.currentSegment.index;
let segmentRef = this.segmenter.getSegmentRef(selection);
if (segmentRef == null) {
segmentRef = this.currentSegment == null ? this.segmenter.lastSegmentRef : this.currentSegment.ref;
}

if (this.switchCurrentSegment(segmentIndex)) {
if (this.switchCurrentSegment(segmentRef)) {
// the selection has changed to a different segment
return true;
} else {
Expand All @@ -111,15 +118,15 @@ export abstract class DocumentEditor {
}
}

switchCurrentSegment(segmentIndex: number): boolean {
switchCurrentSegment(segmentRef: string): boolean {
if (this.currentSegment != null && this.documentSetId === this.currentSegment.documentSetId
&& segmentIndex === this.currentSegment.index
&& segmentRef === this.currentSegment.ref
) {
// the selection has not changed to a different segment
return false;
}

this.currentSegment = new Segment(this.documentSetId, segmentIndex);
this.currentSegment = new Segment(this.documentSetId, segmentRef);
this.updateCurrentSegment();
return true;
}
Expand Down Expand Up @@ -162,32 +169,8 @@ export abstract class DocumentEditor {
return this.documentSetId + ':' + this.docType;
}

private getSegmentRange(index: number): RangeStatic {
if (this.isTextEmpty) {
return { index: 0, length: 0 };
}

const segments = this.getSegmentRanges();
return index < segments.length ? segments[index] : { index: this.quill.getLength() - 1, length: 0 };
}

private getSegmentRanges(): RangeStatic[] {
const text = this.quill.getText().substr(0, this.quill.getLength() - 1);
const segmentRanges = this.machine.tokenizeDocumentText(this.docType, text);
if (segmentRanges.length === 0) {
segmentRanges.push({ index: 0, length: 0 });
} else {
const lastSegmentRange = segmentRanges[segmentRanges.length - 1];
const lastSegmentEnd = lastSegmentRange.index + lastSegmentRange.length;
if (lastSegmentEnd < text.length) {
segmentRanges.push({ index: text.length, length: 0 });
}
}
return segmentRanges;
}

private updateCurrentSegment() {
const range = this.getSegmentRange(this.currentSegment.index);
const range = this.segmenter.getSegmentRange(this.currentSegment.ref);
const text = this.quill.getText(range.index, range.length);
this.currentSegment.update(text, range);
}
Expand Down Expand Up @@ -247,16 +230,16 @@ export class TargetDocumentEditor extends DocumentEditor {
this.updateSuggestions();
}

if (textChange && this.currentSegment != null && this.currentSegment.index === this.segmentRanges.length - 1) {
if (textChange && this.currentSegment != null && this.currentSegment.ref === this.segmenter.lastSegmentRef) {
this.updateHighlight(this.currentSegment.range);
}

return segmentChanged;
}

switchCurrentSegment(segmentIndex: number): boolean {
switchCurrentSegment(segmentRef: string): boolean {
const previousSegment = this.currentSegment;
const segmentChanged = super.switchCurrentSegment(segmentIndex);
const segmentChanged = super.switchCurrentSegment(segmentRef);
if (segmentChanged) {
this.trainSegment(previousSegment);
}
Expand Down Expand Up @@ -358,7 +341,7 @@ export class TargetDocumentEditor extends DocumentEditor {
return this.machine.trainSegment()
.then(() => {
segment.acceptChanges();
this.$window.console.log('Segment ' + segment.index + ' of document ' + segment.documentSetId
this.$window.console.log('Segment ' + segment.ref + ' of document ' + segment.documentSetId
+ ' was trained successfully.');
})
.finally(() => this.pendingTrainCount--);
Expand Down Expand Up @@ -428,11 +411,11 @@ export class SourceDocumentEditor extends DocumentEditor {
return segmentChanged;
}

switchCurrentSegment(segmentIndex: number): boolean {
switchCurrentSegment(segmentRef: string): boolean {
if (!this.hasFocus) {
this.isCurrentSegmentHighlighted = false;
}
const segmentChanged = super.switchCurrentSegment(segmentIndex);
const segmentChanged = super.switchCurrentSegment(segmentRef);
if (!this.hasFocus) {
this.isCurrentSegmentHighlighted = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<i class="fa fa-exchange"></i></button>
</div>
<ng-quill-editor class="notranslate"
id="editorSource" data-modules="$ctrl.source.modulesConfig" data-theme="suggestions"
id="editorSource" spellcheck="false" data-modules="$ctrl.source.modulesConfig" data-theme="suggestions"
data-on-editor-created="$ctrl.onQuillCreated(editor, $ctrl.source)"
data-on-content-changed="$ctrl.onContentChanged($ctrl.source)"
data-on-selection-changed="$ctrl.onSelectionChanged($ctrl.source)"
Expand All @@ -152,7 +152,7 @@
<label class="editor-label">{{$ctrl.getEditorLabel($ctrl.right)}}</label>
</div>
<ng-quill-editor class="notranslate"
id="editorTarget" data-modules="$ctrl.target.modulesConfig" data-theme="suggestions"
id="editorTarget" spellcheck="false" data-modules="$ctrl.target.modulesConfig" data-theme="suggestions"
data-on-editor-created="$ctrl.onQuillCreated(editor, $ctrl.target)"
data-on-content-changed="$ctrl.onContentChanged($ctrl.target)"
data-on-selection-changed="$ctrl.onSelectionChanged($ctrl.target)"
Expand Down
16 changes: 11 additions & 5 deletions src/angular-app/languageforge/translate/editor/editor.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as angular from 'angular';
import { setTimeout } from 'core-js/library/web/timers';
import { SmtTrainProgress } from 'machine';
import Quill from 'quill';

Expand Down Expand Up @@ -141,9 +140,12 @@ export class TranslateEditorController implements angular.IController {
new TranslateUserPreferences();
this.source.inputSystem = this.tecProject.config.source.inputSystem;
this.target.inputSystem = this.tecProject.config.target.inputSystem;
this.machine.initialise(this.tecProject.slug, this.tecProject.config.isTranslationDataScripture);
this.machine.initialise(this.tecProject.slug);
this.showFormats = this.tecProject.config.userPreferences.isFormattingOptionsShown;

this.source.isScripture = this.tecProject.config.isTranslationDataScripture;
this.target.isScripture = this.tecProject.config.isTranslationDataScripture;

if (this.tecProject.config.documentSets.idsOrdered != null &&
this.tecProject.config.documentSets.idsOrdered.length > 0
) {
Expand Down Expand Up @@ -204,7 +206,11 @@ export class TranslateEditorController implements angular.IController {
}
});

this.machine.initialise(this.tecProject.slug, this.tecProject.config.isTranslationDataScripture);
this.machine.initialise(this.tecProject.slug);

this.source.isScripture = this.tecProject.config.isTranslationDataScripture;
this.target.isScripture = this.tecProject.config.isTranslationDataScripture;

this.listenForTrainingStatus();
}
}
Expand Down Expand Up @@ -600,7 +606,7 @@ export class TranslateEditorController implements angular.IController {

if (segmentChanged) {
// select the corresponding source segment
this.source.switchCurrentSegment(this.target.currentSegmentIndex);
this.source.switchCurrentSegment(this.target.currentSegmentRef);

if (this.currentDocType) {
this.metricService.sendMetrics(true, this.target.currentSegmentDocumentSetId);
Expand All @@ -625,7 +631,7 @@ export class TranslateEditorController implements angular.IController {

case DocType.SOURCE:
if (segmentChanged) {
this.target.switchCurrentSegment(this.source.currentSegmentIndex);
this.target.switchCurrentSegment(this.source.currentSegmentRef);

if (!this.currentDocType && this.selectedDocumentSetIndex in this.documentSets) {
this.metricService.currentDocumentSetId = this.documentSets[this.selectedDocumentSetIndex].id;
Expand Down
2 changes: 1 addition & 1 deletion src/angular-app/languageforge/translate/editor/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class Segment {
private _range: RangeStatic;
private initialText: string;

constructor(public readonly documentSetId: string, public readonly index: number) { }
constructor(public readonly documentSetId: string, public readonly ref: string) { }

get text(): string {
return this._text;
Expand Down
Loading

0 comments on commit 6766487

Please sign in to comment.