Skip to content

Commit

Permalink
fix: Merge pull request #275 from UniversalDataTool/fix/relations-fixes
Browse files Browse the repository at this point in the history
add support for import text snippets with relations,
  • Loading branch information
seveibar committed Aug 26, 2020
2 parents f458e15 + 89cf6fe commit 4975550
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-markdown": "^4.1.0",
"react-nlp-annotate": "^0.3.13",
"react-nlp-annotate": "^0.3.14",
"rimraf": "^3.0.2",
"spelling": "^2.0.1"
},
Expand Down
6 changes: 5 additions & 1 deletion src/components/ImportPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ export default ({
<Button
dialog="import-text-snippets"
Icon={TextFieldsIcon}
onlySupportType={["text_entity_recognition", "text_classification"]}
onlySupportType={[
"text_entity_recognition",
"text_classification",
"text_entity_relations",
]}
type={dataset.interface.type}
>
{t("import-text-snippets")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const simpleSequenceAndRelationsToEntitySequence = ({
textId: seq.textId,
label: seq.label,
start: charsPassed,
end: charsPassed + seq.text.length,
end: charsPassed + seq.text.length - 1,
})
}
charsPassed += seq.text.length
Expand All @@ -54,7 +54,8 @@ export const entitySequenceToSimpleSeq = (doc, entSeq) => {
textId:
entSeq[nextEntity].textId || Math.random().toString(36).slice(-6),
})
i = entSeq[nextEntity].end
// i = entSeq[nextEntity].end
i += entSeq[nextEntity].text.length - 1
simpleSeq.push({ text: "" })
nextEntity += 1
} else {
Expand All @@ -68,5 +69,13 @@ export const entitySequenceToSimpleSeq = (doc, entSeq) => {
}
}
}
if (simpleSeq.map((s) => s.text).join("") !== doc) {
throw new Error(
"Combining annotation entities with document didn't match document.\n\n" +
simpleSeq.map((s) => s.text).join("") +
"\n\nvs\n\n" +
doc
)
}
return simpleSeq.filter((item) => item.text.length > 0)
}
3 changes: 3 additions & 0 deletions src/components/TextEntityRelations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../TextEntityRecognition/convert-react-nlp-annotate-types"

export const TextEntityRelations = (props) => {
console.log(props)
const [currentSampleIndex, setCurrentSampleIndex] = useClobberedState(
props.sampleIndex,
0
Expand All @@ -28,6 +29,8 @@ export const TextEntityRelations = (props) => {
)
}

console.log({ initialSequence, initialRelations })

return (
<NLPAnnotator
key={currentSampleIndex}
Expand Down
211 changes: 163 additions & 48 deletions src/components/TextEntityRelations/index.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,172 @@ import { action } from "@storybook/addon-actions"

import TextEntityRelations from "./"

storiesOf("TextEntityRelations", module).add("Basic", () => (
<TextEntityRelations
onSaveTaskOutputItem={action("onSaveTaskOutputItem")}
onExit={action("onExit")}
interface={{
type: "text_entity_recognition",
description: "Label words or phrases as food or hat.",
overlapAllowed: false,
entityLabels: [
storiesOf("TextEntityRelations", module)
.add("Basic", () => (
<TextEntityRelations
onSaveTaskOutputItem={action("onSaveTaskOutputItem")}
onExit={action("onExit")}
interface={{
type: "text_entity_recognition",
description: "Label words or phrases as food or hat.",
overlapAllowed: false,
entityLabels: [
{
id: "food",
displayName: "Food",
},
{
id: "hat",
displayName: "Hat",
},
],
relationLabels: [
{
id: "subject-doing",
displayName: "Subject Doing",
},
],
}}
samples={[
{
id: "food",
displayName: "Food",
document:
"This strainer makes a great hat, I'll wear it while I serve spaghetti!",
annotation: {
entities: [
{
text: "strainer",
label: "hat",
start: 5,
end: 13,
textId: "id1",
},
{
text: "spaghetti",
label: "food",
start: 60,
end: 69,
textId: "id2",
},
{ text: "I'll", start: 33, end: 36, textId: "id3" },
{ text: "wear", start: 38, end: 41, textId: "id4" },
],
relations: [{ from: "id3", to: "id4", label: "subject-doing" }],
},
},
]}
/>
))
.add("Saving bug with relations (should not error)", () => (
<TextEntityRelations
onSaveTaskOutputItem={action("onSaveTaskOutputItem")}
onExit={action("onExit")}
interface={{
type: "text_entity_recognition",
description: "Label words or phrases as food or hat.",
overlapAllowed: false,
entityLabels: [
{
id: "food",
displayName: "Food",
},
{
id: "hat",
displayName: "Hat",
},
],
relationLabels: [
{
id: "subject",
displayName: "Subject",
},
{
id: "R1",
},
],
}}
samples={[
{
id: "hat",
displayName: "Hat",
document: "This is something i'd like to do",
annotation: {
entities: [
{ text: "This", textId: "82tm4y", start: 0, end: 3 },
{ text: "something", textId: "gejze6", start: 8, end: 16 },
{ text: "d", textId: "ixwjg9", start: 20, end: 20 },
{ text: "to", textId: "r8ayjq", start: 27, end: 28 },
{ text: "do", textId: "wt3hbx", start: 30, end: 31 },
],
relations: [
{
from: "82tm4y",
to: "gejze6",
label: "subject",
color: "#d32f2f",
},
{ from: "ixwjg9", to: "gejze6", label: "R1", color: "#c2185b" },
{
from: "r8ayjq",
to: "gejze6",
label: "subject",
color: "#d32f2f",
},
{ from: "ixwjg9", to: "wt3hbx", label: "R1", color: "#c2185b" },
{
from: "82tm4y",
to: "gejze6",
label: "subject",
color: "#d32f2f",
},
],
},
},
],
relationLabels: [
]}
/>
))
.add("Should not combine entities", () => (
<TextEntityRelations
onSaveTaskOutputItem={action("onSaveTaskOutputItem")}
onExit={action("onExit")}
interface={{
type: "text_entity_recognition",
description: "Label words or phrases as food or hat.",
overlapAllowed: false,
entityLabels: [
{
id: "food",
displayName: "Food",
},
{
id: "hat",
displayName: "Hat",
},
],
relationLabels: [
{
id: "subject",
displayName: "Subject",
},
{
id: "R1",
},
],
}}
samples={[
{
id: "subject-doing",
displayName: "Subject Doing",
document: "This is something i'd like to do",
annotation: {
entities: [
{
text: "This",
textId: "this",
start: 0,
end: 3,
label: "subject",
},
{ text: " is", textId: "is", start: 4, end: 6, label: "subject" },
],
relations: [],
},
},
],
}}
samples={[
{
document:
"This strainer makes a great hat, I'll wear it while I serve spaghetti!",
annotation: {
entities: [
{
text: "strainer",
label: "hat",
start: 5,
end: 13,
textId: "id1",
},
{
text: "spaghetti",
label: "food",
start: 60,
end: 69,
textId: "id2",
},
{ text: "I'll", start: 32, end: 36, textId: "id3" },
{ text: "wear", start: 37, end: 41, textId: "id4" },
],
relations: [{ from: "id3", to: "id4", label: "subject-doing" }],
},
},
]}
/>
))
]}
/>
))
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16303,10 +16303,10 @@ react-native-get-random-values@^1.4.0:
dependencies:
fast-base64-decode "^1.0.0"

react-nlp-annotate@^0.3.13:
version "0.3.13"
resolved "https://registry.yarnpkg.com/react-nlp-annotate/-/react-nlp-annotate-0.3.13.tgz#8652ebf35a5585d4ab921f1966470f5cda35d71c"
integrity sha512-JJjCAE8vzZxPP+u2BPAmCHvWDduBTPqOh13N+YGMUs26aqQYDrOOnjZQN9VLnySXtwcGkeJbufH6q3k+A8Hlbw==
react-nlp-annotate@^0.3.14:
version "0.3.14"
resolved "https://registry.yarnpkg.com/react-nlp-annotate/-/react-nlp-annotate-0.3.14.tgz#d96c495d0e5da259a5b02de11b72df3d44157267"
integrity sha512-SSg1jsD7rh6WA4OSCcs4VbaTfIibGlRd0qLcADDc8HdioALiodJcCvkPQG5A4nt1NfAZaBEn/QOs9FUoei57rg==
dependencies:
"@material-ui/lab" "^4.0.0-alpha.56"
chroma-js "^2.0.3"
Expand Down

0 comments on commit 4975550

Please sign in to comment.