带有unicode和标点符号的Javascript regexp

2024-05-23 18:58:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的测试用例用于拆分单字,但不知道如何使用javascript。你知道吗

describe("garden: utils", () => {
  it("should split correctly", () => {
    assert.deepEqual(segmentation('Hockey is a popular sport in Canada.'), [
      'Hockey', 'is', 'a', 'popular', 'sport', 'in', 'Canada', '.'
    ]);

    assert.deepEqual(segmentation('How many provinces are there in Canada?'), [
      'How', 'many', 'provinces', 'are', 'there', 'in', 'Canada', '?'
    ]);

    assert.deepEqual(segmentation('The forest is on fire!'), [
      'The', 'forest', 'is', 'on', 'fire', '!'
    ]);

    assert.deepEqual(segmentation('Emily Carr, who was born in 1871, was a great painter.'), [
      'Emily', 'Carr', ',', 'who', 'was', 'born', 'in', '1871', ',', 'was', 'a', 'great', 'painter', '.'
    ]);

    assert.deepEqual(segmentation('This is David\'s computer.'), [
      'This', 'is', 'David', '\'', 's', 'computer', '.'
    ]);

    assert.deepEqual(segmentation('The prime minister said, "We will win the election."'), [
      'The', 'prime', 'minister', 'said', ',', '"', 'We', 'will', 'win', 'the', 'election', '.', '"'
    ]);

    assert.deepEqual(segmentation('There are three positions in hockey: goalie, defence, and forward.'), [
      'There', 'are', 'three', 'positions', 'in', 'hockey', ':', 'goalie', ',', 'defence', ',', 'and', 'forward', '.'
    ]);

    assert.deepEqual(segmentation('The festival is very popular; people from all over the world visit each year.'), [
      'The', 'festival', 'is', 'very', 'popular', ';', 'people', 'from', 'all', 'over', 'the', 'world',
      'visit', 'each', 'year', '.'
    ]);

    assert.deepEqual(segmentation('Mild, wet, and cloudy - these are the characteristics of weather in Vancouver.'), [
      'Mild', ',', 'wet', ',', 'and', 'cloudy', '-', 'these', 'are', 'the', 'characteristics', 'of', 'weather',
      'in', 'Vancouver', '.'
    ]);

    assert.deepEqual(segmentation('sweet-smelling'), [
      'sweet', '-', 'smelling'
    ]);
  });

  it("should not split unicoded words", () => {
    assert.deepEqual(segmentation('hacer a propósito'), [
      'hacer', 'a', 'propósito'
    ]);

    assert.deepEqual(segmentation('nhà em có con mèo'), [
      'nhà', 'em', 'có', 'con', 'mèo'
    ]);
  });

  it("should group periods", () => {
    assert.deepEqual(segmentation('So are ... the fishes.'), [
      'So', 'are', '...', 'the', 'fishes', '.'
    ]);

    assert.deepEqual(segmentation('So are ...... the fishes.'), [
      'So', 'are', '......', 'the', 'fishes', '.'
    ]);

    assert.deepEqual(segmentation('arriba arriba ja....'), [
      'arriba', 'arriba', 'ja', '....'
    ]);
  });
});

下面是python中的等效表达式:

class Segmentation(BaseNLPProcessor):
    pattern = re.compile('((?u)\w+|\.{2,}|[%s])' % string.punctuation)

    @classmethod
    def ignore_value(cls, value):
        # type: (str) -> bool
        return negate(compose(is_empty, string.strip))(value)

    def split(self):
        # type: () -> List[str]
        return filter(self.ignore_value, self.pattern.split(self.value()))

我想在python中编写一个等价的函数,让javascript按单字和标点符号拆分,按多个点分组。。。你知道吗

Segmentation("Hockey is a popular sport in Canada.").split()

Tags: andtheinsoisvalueassertare
2条回答

非常复杂,因为JavaScript RegExp中的断言后面没有负面的外观,而且Unicode支持还不是官方的(目前只在Firefox中支持一个标志)。它使用一个库(XRegExp)来处理unicode类。如果您需要完整的正规正则表达式,它是巨大的。只需注释并让我知道,我将更新答案以使用包含Unicode范围的分解正常RegExp语句。你知道吗

const rxLetterToOther = XRegExp('(\\p{L})((?!\\s)\\P{L})','g');
const rxOtherToLetter = XRegExp('((?!\\s)\\P{L})(\\p{L})','g');
const rxNumberToOther = XRegExp('(\\p{N})((?!\\s)\\P{N})','g');
const rxOtherToNumber = XRegExp('((?!\\s)\\P{N})(\\p{N})','g');
const rxPuctToPunct = XRegExp('(\\p{P})(\\p{P})','g');
const rxSep = XRegExp('\\s+','g');

function segmentation(s) {
  return s
    .replace(rxLetterToOther, '$1 $2')
    .replace(rxOtherToLetter, '$1 $2')
    .replace(rxNumberToOther, '$1 $2')
    .replace(rxOtherToNumber, '$1 $2')
    .replace(rxPuctToPunct, '$1 $2')
    .split(rxSep);
}

Here it is passing all the test cases!

<object data="https://fiddle.jshell.net/a3tf68ae/14/show/" />

编辑:更新测试用例以打印测试结果下面的巨大RegExp源。运行代码段以查看嵌入的测试用例。

我找到了答案,但很复杂。有人对此有其他简单的答案吗

module.exports = (string) => {
  const segs = string.split(/(\.{2,}|!|"|#|$|%|&|'|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|\?|¿|@|[|]|\\|^|_|`|{|\||}|~| )/);

  return segs.filter((seg) => seg.trim() !== "");
};

相关问题 更多 >