Regular expression to find last word in sentence

前端 未结 2 656
一向
一向 2020-12-17 21:51

How can I find last word in a sentence with a regular expression?

2条回答
  •  感动是毒
    2020-12-17 22:19

    In general you can't correctly parse English text with regular expressions.

    The best you can do is to look for some punctuation that usually terminates a sentence but unfortunately this is not a guarantee. For example the text Mr. Bloggs is here. Do you want to talk to him? contains two periods which have different meanings. There is no way for a regular expression to distinguish between the two uses of the period.

    I'd suggest instead that you look at a natural language parsing library. For example the Stanford Parser has no trouble at all correctly parsing the above text into the two sentences:

    Mr./NNP Bloggs/NNP is/VBZ here/RB ./.
    Do/VBP you/PRP want/VB to/TO talk/VB to/TO him/PRP ?/.
    

    There are lots of other freely available NLP libraries that you could use too, I'm not endorsing that one product in particular - it's just an example to demonstrate that it is possible to parse text into sentences with a fairly high reliability. Note though that even a natural language parsing library will still occasionally make a mistake - parsing human languages correctly is hard.

提交回复
热议问题