Skip to content

Commit

Permalink
[MergeDups] Show merge result when combining senses (#3324)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Aug 28, 2024
1 parent 0804ef9 commit 34b59b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
28 changes: 15 additions & 13 deletions src/goals/MergeDuplicates/MergeDupsStep/SenseCardContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IconButtonWithTooltip, PartOfSpeechButton } from "components/Buttons";
import MultilineTooltipTitle from "components/MultilineTooltipTitle";
import DomainChipsGrid from "components/WordCard/DomainChipsGrid";
import SenseCardText from "components/WordCard/SenseCardText";
import { combineSenses } from "goals/MergeDuplicates/Redux/reducerUtilities";

interface SenseCardContentProps {
senses: Sense[];
Expand All @@ -31,13 +32,14 @@ export default function SenseCardContent(
): ReactElement {
const { t } = useTranslation();

const gramInfo = props.senses
.map((s) => s.grammaticalInfo)
.find((g) => g.catGroup !== GramCatGroup.Unspecified);

const semDoms = props.senses
.flatMap((s) => s.semanticDomains)
.sort((a, b) => a.id.localeCompare(b.id));
const sense = combineSenses(props.senses);
const gramInfo =
sense.grammaticalInfo.catGroup === GramCatGroup.Unspecified
? undefined
: sense.grammaticalInfo;
const semDoms = sense.semanticDomains.sort((a, b) =>
a.id.localeCompare(b.id)
);

const reasonText = (reason: ProtectReason): string => {
// Backend/Helper/LiftHelper.cs > GetProtectedReasons(LiftSense sense)
Expand Down Expand Up @@ -96,9 +98,9 @@ export default function SenseCardContent(
};

const protectedWarning =
!props.sidebar && props.senses[0].accessibility === Status.Protected;
!props.sidebar && sense.accessibility === Status.Protected;
const tooltipTexts = [t("mergeDups.helpText.protectedSense")];
const reasons = props.senses[0]?.protectReasons;
const reasons = sense.protectReasons;
if (reasons?.length) {
tooltipTexts.push(
t("mergeDups.helpText.protectedData", {
Expand All @@ -114,7 +116,7 @@ export default function SenseCardContent(
<div style={{ position: "absolute", left: 0, top: 0 }}>
{gramInfo && (
<PartOfSpeechButton
buttonId={`sense-${props.senses[0].guid}-part-of-speech`}
buttonId={`sense-${sense.guid}-part-of-speech`}
gramInfo={gramInfo}
onlyIcon
/>
Expand All @@ -128,7 +130,7 @@ export default function SenseCardContent(
side="top"
size="small"
text={<MultilineTooltipTitle lines={tooltipTexts} />}
buttonId={`sense-${props.senses[0].guid}-protected`}
buttonId={`sense-${sense.guid}-protected`}
/>
)}
</div>
Expand All @@ -144,15 +146,15 @@ export default function SenseCardContent(
{props.senses.length > 1 && (
<IconButton
onClick={props.toggleFunction}
id={`sidebar-open-sense-${props.senses[0].guid}`}
id={`sidebar-open-sense-${sense.guid}`}
size="large"
>
<ArrowForwardIos />
</IconButton>
)}
</div>
{/* List glosses and (if any) definitions. */}
<SenseCardText languages={props.languages} sense={props.senses[0]} />
<SenseCardText languages={props.languages} sense={sense} />
{/* List semantic domains. */}
<DomainChipsGrid semDoms={semDoms} />
</CardContent>
Expand Down
46 changes: 38 additions & 8 deletions src/goals/MergeDuplicates/Redux/reducerUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "goals/MergeDuplicates/MergeDupsTreeTypes";
import { newMergeWords } from "goals/MergeDuplicates/MergeDupsTypes";
import { type Hash } from "types/hash";
import { newGrammaticalInfo, newSense } from "types/word";
import { compareFlags } from "utilities/wordUtilities";

// A collection of helper/utility functions only for use in the MergeDupsReducer.
Expand Down Expand Up @@ -112,15 +113,42 @@ export function createMergeParent(
export function combineIntoFirstSense(mergeSenses: MergeTreeSense[]): void {
// Set the main sense to the first sense (the top one when the sidebar was opened).
const mainSense = mergeSenses[0].sense;
const sep = "; ";
const senses: Sense[] = [mainSense];

// Merge the rest as duplicates.
// Mark the rest as duplicates.
// These were senses dropped into another sense.
mergeSenses.slice(1).forEach((mergeDupSense) => {
const dupSense = mergeDupSense.sense;
dupSense.accessibility = Status.Duplicate;
mergeDupSense.sense.accessibility = Status.Duplicate;
senses.push(mergeDupSense.sense);
});

// Combine the sense content and update the main sense.
const combinedSense = combineSenses(senses);
mainSense.definitions = combinedSense.definitions;
mainSense.glosses = combinedSense.glosses;
mainSense.grammaticalInfo = combinedSense.grammaticalInfo;
mainSense.semanticDomains = combinedSense.semanticDomains;
}

/** Create a copy of the first sense with the following content merged from all senses:
* definitions, glosses, grammaticalInfo, semanticDomains. */
export function combineSenses(senses: Sense[]): Sense {
if (!senses.length) {
return newSense();
}

// Merge the duplicate's definitions into the main sense.
const mainSense: Sense = {
...senses[0],
definitions: [],
glosses: [],
grammaticalInfo: newGrammaticalInfo(),
semanticDomains: [],
};

const sep = "; ";

senses.forEach((dupSense) => {
// Merge in the definitions.
dupSense.definitions.forEach((def) => {
const newText = def.text.trim();
if (newText) {
Expand All @@ -143,7 +171,7 @@ export function combineIntoFirstSense(mergeSenses: MergeTreeSense[]): void {
}
});

// Merge the duplicate's glosses into the main sense.
// Merge in the glosses.
dupSense.glosses.forEach((gloss) => {
const newDef = gloss.def.trim();
if (newDef) {
Expand All @@ -166,7 +194,7 @@ export function combineIntoFirstSense(mergeSenses: MergeTreeSense[]): void {
}
});

// Use the duplicate's part of speech if not specified in the main sense.
// Use the grammatical info if not already specified.
if (mainSense.grammaticalInfo.catGroup === GramCatGroup.Unspecified) {
mainSense.grammaticalInfo = { ...dupSense.grammaticalInfo };
} else if (
Expand All @@ -180,11 +208,13 @@ export function combineIntoFirstSense(mergeSenses: MergeTreeSense[]): void {
}
}

// Put the duplicate's domains in the main sense if the id is new.
// Merge in the semantic domains.
dupSense.semanticDomains.forEach((dom) => {
if (mainSense.semanticDomains.every((d) => d.id !== dom.id)) {
mainSense.semanticDomains.push({ ...dom });
}
});
});

return mainSense;
}

0 comments on commit 34b59b5

Please sign in to comment.