Skip to content

Commit

Permalink
Paragraph mode and vertical (line-height) density toggle for LM salie…
Browse files Browse the repository at this point in the history
…nce.

PiperOrigin-RevId: 608174566
  • Loading branch information
iftenney authored and LIT team committed Feb 18, 2024
1 parent 825c04e commit 4c8555a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lit_nlp/client/elements/token_chips.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
:host {
--block-line-height: 22px;
}

.tokens-group {
position: relative;
}
Expand Down Expand Up @@ -71,7 +75,7 @@
.tokens-holder-display-block {
display: block;
font-size: 0; /* hack to get zero spacing between elements */
line-height: 22px;
line-height: var(--block-line-height);
}

.tokens-holder-display-block > * {
Expand Down
30 changes: 28 additions & 2 deletions lit_nlp/client/modules/lm_salience_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {css, html} from 'lit';
// tslint:disable:no-new-decorators
import {customElement} from 'lit/decorators.js';
import {classMap} from 'lit/directives/class-map.js';
import {styleMap} from 'lit/directives/style-map.js';
import {computed, observable} from 'mobx';

import {LitModule} from '../core/lit_module';
Expand Down Expand Up @@ -39,6 +40,7 @@ enum SegmentationMode {
WORDS = 'Words',
SENTENCES = 'Sentences',
LINES = 'Lines',
PARAGRAPHS = '¶',
// TODO(b/324961811): add phrase or clause chunking?
// TODO(b/324961803): add custom regex?
}
Expand Down Expand Up @@ -148,10 +150,15 @@ class LMSalienceChips extends TokenChips {
--token-outline-color: var(--lit-neutral-300); /* outline in non-dense mode */
}
.tokens-holder-display-block .salient-token {
padding: 3px 0;
padding: 3px 0; /* this controls the visible highlight */
margin: 0;
margin-right: 4px;
}
.tokens-holder-display-block .salient-token span {
/* this controls the mouseover area, so there are no gaps */
/* or flickering when hovering over multiline tokens */
padding: 6px 0;
}
.salient-token.selected {
--token-outline-color: var(--lit-mage-700);
box-shadow: 0px 0px 3px var(--token-outline-color);
Expand Down Expand Up @@ -207,6 +214,7 @@ export class LMSalienceModule extends SingleExampleSingleModelModule {
@observable private selectedSalienceMethod? = 'grad_l2';
@observable private cmapGamma = 1.0;
@observable private denseView = true;
@observable private vDense = false; /* vertical spacing */
@observable private showSelfSalience = false;

@observable.ref private currentTokens: string[] = [];
Expand Down Expand Up @@ -302,6 +310,12 @@ export class LMSalienceModule extends SingleExampleSingleModelModule {
// - a run of consecutive \n as its own segment
// - any non-\n following \n
return groupTokensByRegexPrefix(this.currentTokens, /(\n+)|([^\n]+)/g);
} else if (this.segmentationMode === SegmentationMode.PARAGRAPHS) {
// Paragraph start is either:
// - two or more newlines as its own segment
// - any non-\n following \n\n
return groupTokensByRegexPrefix(
this.currentTokens, /(\n\n+)|(?<=\n\n)([^\n]+)/g);
} else {
throw new Error(
`Unsupported segmentation mode ${this.segmentationMode}.`);
Expand Down Expand Up @@ -541,6 +555,10 @@ export class LMSalienceModule extends SingleExampleSingleModelModule {
};
});

const onClickToggleVDense = () => {
this.vDense = !this.vDense;
};

// prettier-ignore
return html`
<div class="controls-group" style="gap: 8px;">
Expand All @@ -559,6 +577,10 @@ export class LMSalienceModule extends SingleExampleSingleModelModule {
grid_view
</mwc-icon>
</lit-switch>
<mwc-icon class='icon-button large-icon' title='Vertical density'
@click=${onClickToggleVDense}>
${this.vDense ? 'expand' : 'compress'}
</mwc-icon>
</div>
`;
}
Expand Down Expand Up @@ -854,12 +876,16 @@ export class LMSalienceModule extends SingleExampleSingleModelModule {
});
}

const chipHostStyle =
styleMap({'--block-line-height': this.vDense ? '16px' : '22px'});

// prettier-ignore
return html`
<div class='chip-container'>
<lm-salience-chips .tokensWithWeights=${segmentsWithWeights}
?dense=${this.denseView} ?preSpace=${this.denseView}
.cmap=${this.cmap} breakNewlines displayBlock>
.cmap=${this.cmap} breakNewlines displayBlock
style=${chipHostStyle}>
</lm-salience-chips>
</div>
`;
Expand Down

0 comments on commit 4c8555a

Please sign in to comment.