Skip to content

Commit 69e9f52

Browse files
committed
fix: custom regex
1 parent 4b7e647 commit 69e9f52

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

src/components/DocumentLabeler/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ export default function DocumentLabeler(props: LabelDocumentProps) {
1212
const [selectedLabels, changeSelectedLabels] = useState(
1313
props.initialLabels || (props.initialLabel ? [props.initialLabel] : [])
1414
)
15-
const sequence = useMemo(() => stringToSequence(props.document), [
16-
props.document
17-
])
15+
const sequence = useMemo(
16+
() => stringToSequence(props.document, props.separatorRegex),
17+
[props.document]
18+
)
1819
return (
1920
<div>
2021
<div>
@@ -58,7 +59,11 @@ export default function DocumentLabeler(props: LabelDocumentProps) {
5859
)
5960
})}
6061
</div>
61-
<Document nothingHighlighted sequence={sequence} />
62+
<Document
63+
nothingHighlighted
64+
sequence={sequence}
65+
separatorRegex={props.separatorRegex}
66+
/>
6267
</div>
6368
</div>
6469
)

src/components/NLPAnnotator/index.story.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ storiesOf("NLPAnnotator", module)
3232
]}
3333
/>
3434
))
35+
.add("Sequence Labeler with Custom Regex", () => (
36+
<NLPAnnotator
37+
hotkeysEnabled
38+
onChange={action("onChange")}
39+
onFinish={action("onFinish")}
40+
onNext={action("onNext")}
41+
onPrev={action("onPrev")}
42+
type="label-sequence"
43+
document={`Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pharetra ipsum tristique ligula venenatis placerat. Interdum et malesuada fames ac ante ipsum primis in faucibus. Fusce mollis velit nec tellus sollicitudin aliquam. In velit erat, iaculis id consectetur et, tincidunt sit amet mauris. Quisque ultricies, purus eleifend congue malesuada, ipsum erat molestie dolor, in pellentesque lacus purus vel nisl. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla sed vestibulum magna. Quisque ut lorem imperdiet, aliquam velit nec, dictum felis.`}
44+
labels={[
45+
{
46+
color: colors[0],
47+
id: "noun",
48+
displayName: "Noun"
49+
},
50+
{
51+
color: colors[1],
52+
id: "proper-noun",
53+
displayName: "Proper Noun"
54+
}
55+
]}
56+
separatorRegex="."
57+
/>
58+
))
3559
.add("Document Labeler", () => (
3660
<NLPAnnotator
3761
hotkeysEnabled

src/components/RelationshipAnnotator/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export default function RelationshipAnnotator(
133133
</LabelSelectorContainer>
134134
<div style={{ borderTop: "1px solid #ccc", marginTop: 8, paddingTop: 5 }}>
135135
<Document
136+
separatorRegex={props.separatorRegex}
136137
colorLabelMap={colorLabelMap}
137138
nothingHighlighted={highlightedItems.length === 0}
138139
onCreateEmptyRelationship={([first, second]) => {

src/components/SequenceAnnotator/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function SequenceAnnotator(props: SequenceAnnotatorProps) {
1818
? [entity]
1919
: stringToSequence(entity.text, props.separatorRegex)
2020
)
21-
: stringToSequence(props.document)
21+
: stringToSequence(props.document, props.separatorRegex)
2222
)
2323
const colorLabelMap = useMemo(
2424
() =>
@@ -71,6 +71,7 @@ export default function SequenceAnnotator(props: SequenceAnnotatorProps) {
7171
</div>
7272
<div style={{ borderTop: "1px solid #ccc", marginTop: 8, paddingTop: 5 }}>
7373
<Document
74+
separatorRegex={props.separatorRegex}
7475
colorLabelMap={colorLabelMap}
7576
nothingHighlighted={highlightedItems.length === 0}
7677
onHighlightedChanged={highlightedItems =>

src/string-to-sequence.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// @flow
22

3-
const stringToSequence = (doc: string, sepRe: RegExp = /[a-zA-ZÀ-ÿ]+/g) => {
3+
const stringToSequence = (
4+
doc: string,
5+
sepRe: RegExp | string = /[a-zA-ZÀ-ÿ]+/g
6+
) => {
47
if (typeof sepRe === "string") {
5-
sepRe = new RegExp(sepRe)
8+
sepRe = new RegExp(sepRe, "g")
69
}
710
let m
811
let indices = [0]
@@ -15,7 +18,6 @@ const stringToSequence = (doc: string, sepRe: RegExp = /[a-zA-ZÀ-ÿ]+/g) => {
1518
} while (m)
1619
indices = indices.concat([doc.length])
1720
return indices
18-
.filter((_, i) => indices[i] !== indices[i + 1])
1921
.map((_, i) => ({
2022
text: doc.slice(indices[i], indices[i + 1]),
2123
textId: Math.random()

src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type LabelDocumentProps = {
3232
multipleLabels?: boolean,
3333
document: string,
3434
initialLabels?: Array<string>,
35+
separatorRegex?: string,
3536
onChange: (Array<string> | string | null) => any
3637
}
3738

0 commit comments

Comments
 (0)