Skip to content
This repository was archived by the owner on Jan 4, 2021. It is now read-only.

Commit ea0128a

Browse files
author
Francois-Xavier Gentilhomme
committed
[DEV] fixes text candidates alg
1 parent ec22f91 commit ea0128a

File tree

1 file changed

+37
-55
lines changed

1 file changed

+37
-55
lines changed

myscript-text-web.html

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
max-height: 95px;
4343
}
4444

45-
.candidate {
45+
.text {
4646
color: #1580CD;
4747
background-color: #EDF0F2;
4848
font-size: 1.75rem;
@@ -51,7 +51,7 @@
5151
cursor: pointer;
5252
}
5353

54-
.candidate[selected] {
54+
.text[selected] {
5555
color: #FFFFFF;
5656
background-color: #1580CD;
5757
}
@@ -71,9 +71,24 @@
7171
</style>
7272
<template>
7373
<div class="resultField" hidden="[[ displayResultZone(hideresult, unloaded) ]]">
74-
<template is="dom-repeat" id="candidateList" items="[[ candidates ]]" as="candidate">
75-
<span id="candidate" class="candidate" selected$="[[ _isSelected(index, result.textSegmentResult.selectedCandidateIdx) ]]"
76-
inner-h-t-m-l="[[ candidate.customlabel ]]" on-tap="_select"></span>
74+
<template is="dom-repeat" id="textCandidateList" items="[[ candidates ]]" as="textCandidate" index-as="textIndex">
75+
<span class$="text {{ _getCandidateFlags(candidate) }}" selected$="[[ _isSelected(textIndex, result.textSegmentResult.selectedCandidateIdx) ]]" on-tap="_select">
76+
<template is="dom-if" if="[[ !textCandidate.children ]]">
77+
{{ textCandidate.label }}
78+
</template>
79+
<template is="dom-if" if="[[ textCandidate.children ]]"><!--WARN: templates needs to be inline to avoid carriage return after span-->
80+
<template is="dom-repeat" id="wordCandidateList" items="[[ _getChildCandidates(textCandidate, result, 'text') ]]" as="wordCandidate">
81+
<span class$="word {{ _getCandidateFlags(wordCandidate) }}">
82+
<template is="dom-if" if="[[ !wordCandidate.children ]]">
83+
{{ wordCandidate.label }}
84+
</template>
85+
<template is="dom-if" if="[[ wordCandidate.children ]]">
86+
<template is="dom-repeat" id="charCandidateList" items="[[ _getChildCandidates(wordCandidate, result, 'word') ]]" as="charCandidate"><span class$="char {{ _getCandidateFlags(charCandidate) }}">{{ charCandidate.label }}</span></template>
87+
</template>
88+
</span>
89+
</template>
90+
</template>
91+
</span>
7792
</template>
7893
</div>
7994
<myscript-common-element
@@ -162,9 +177,8 @@
162177
eventDetail.rawResult.result.textSegmentResult.candidates) {
163178

164179
myscriptTextWebElement.result = eventDetail.rawResult.result;
165-
myscriptTextWebElement.resultlabel = eventDetail.rawResult.result.textSegmentResult.candidates[eventDetail.rawResult.result.textSegmentResult.selectedCandidateIdx].label;
166-
myscriptTextWebElement.candidates = eventDetail.rawResult.result.textSegmentResult.candidates;
167-
textUpdateCandidatesCustomLabel(myscriptTextWebElement);
180+
myscriptTextWebElement.resultlabel = myscriptTextWebElement.result.textSegmentResult.candidates[myscriptTextWebElement.result.textSegmentResult.selectedCandidateIdx].label;
181+
myscriptTextWebElement.candidates = myscriptTextWebElement.result.textSegmentResult.candidates;
168182
}
169183
}
170184

@@ -176,52 +190,6 @@
176190
}
177191
}
178192

179-
/**
180-
* Update custom label of candidates by theirs owns properties
181-
*/
182-
function textUpdateCandidatesCustomLabel(myscriptTextWebElement) {
183-
var result = myscriptTextWebElement.result;
184-
result.textSegmentResult.candidates.forEach(function (candidate) {
185-
var textClasses = ['text'];
186-
if (candidate.flags) {
187-
textClasses.push(candidate.flags.join(' ').toLowerCase());
188-
}
189-
var textLabel = '<span class="' + textClasses.join(' ') + '">';
190-
if (result.wordSegments && result.wordSegments.length > -1) {
191-
myscriptTextWebElement._getChildSegments(candidate, result, 'text').forEach(function (wordSegment) {
192-
var wordCandidate = wordSegment.candidates[wordSegment.selectedCandidateIdx];
193-
if (wordCandidate) {
194-
var wordClasses = ['word'];
195-
if (candidate.flags) {
196-
wordClasses.push(wordCandidate.flags.join(' ').toLowerCase());
197-
}
198-
var wordLabel = '<span class="' + wordClasses.join(' ') + '">';
199-
if (result.wordSegments && result.wordSegments.length > -1) {
200-
myscriptTextWebElement._getChildSegments(wordCandidate, result, 'word').forEach(function (charSegment) {
201-
var charCandidate = charSegment.candidates[charSegment.selectedCandidateIdx];
202-
if (charCandidate) {
203-
var charClasses = ['char'];
204-
if (charCandidate.flags) {
205-
charClasses.push(charCandidate.flags.join(' ').toLowerCase());
206-
}
207-
wordLabel += '<span class="' + charClasses.join(' ') + '">' + charCandidate.label + '</span>';
208-
209-
}
210-
});
211-
} else {
212-
wordLabel += wordCandidate.label;
213-
}
214-
wordLabel += '</span>';
215-
textLabel += wordLabel;
216-
}
217-
});
218-
} else {
219-
textLabel += candidate.label;
220-
}
221-
candidate.customlabel = textLabel + '</span>';
222-
});
223-
}
224-
225193
function textAttachElementToDom(myscriptTextWebElement) {
226194
if (myscriptTextWebElement.unloaded !== true && myscriptTextWebElement.connected === true) {
227195

@@ -516,7 +484,7 @@
516484
*/
517485
resultdetail: {
518486
type: String,
519-
value: 'TEXT'
487+
value: 'WORD'
520488
},
521489
/**
522490
* The content types to use for the recognition.
@@ -798,6 +766,20 @@
798766
});
799767
}
800768
return segments;
769+
},
770+
_getChildCandidates: function (candidate, result, type) {
771+
var candidates = [];
772+
this._getChildSegments(candidate, result, type)
773+
.forEach(function (segment) {
774+
candidates.push(segment.candidates[segment.selectedCandidateIdx]);
775+
});
776+
return candidates;
777+
},
778+
_getCandidateFlags: function (candidate) {
779+
if (candidate.flags) {
780+
return candidate.flags.join(' ').toLowerCase();
781+
}
782+
return '';
801783
}
802784
})
803785
;

0 commit comments

Comments
 (0)