I'd like to split up a string like:
Input: Bangalore railway line of the Indian Railway. It comes under Nagpur division of the Central Railway.
Output:
Bangalore
railway
line
Indian Railway
comes
under
Nagpur
division
Central Railway
Notice that compound nouns would be kept together because they are Title Case.
I'm having trouble with the regex part specifically: split(/(?=\s[a-z]|[A-Z]\s|\.)/)
How do I get it to split on the 'water ꜜ Tor Museum' scenario ?
export function splitByPhrase(text: string) {
const outputFreq = text
.split(/(?=\s[a-z]|[A-Z]\s|\.)/)
.filter(Boolean)
.map((x) => x.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "").trim())
.filter((x) => !stopWords.includes(x));
return outputFreq;
}
describe("phrases", () => {
it("no punctuation", () => {
expect(splitByPhrase("test. Toronto")).toEqual(["test", "Toronto"]);
});
it("no spaces", () => {
expect(splitByPhrase(" test Toronto ")).toEqual(["test", "Toronto"]);
});
it("simple phrase detection", () => {
expect(splitByPhrase(" water Tor Museum wants")).toEqual(["water", "Tor Museum", "wants"]);
});
it("remove stop words", () => {
expect(splitByPhrase("Toronto a Museum with")).toEqual(["Toronto", "Museum"]);
});
});