gpt4 book ai didi

python - 如何使用 Python `string.find()` 找到段落的边界?

转载 作者:太空宇宙 更新时间:2023-11-03 17:03:58 25 4
gpt4 key购买 nike

我有一个文本语料库,它用 \nn 分成段落。

\n\n"Well done, Mrs. Martin!" thought Emma.  "You know what you are about."\n\n"And when she had come away, Mrs. Martin was so very kind as to send\nMrs. Goddard a beautiful goose--the finest goose Mrs. Goddard had\never seen.  Mrs. Goddard had dressed it on a Sunday, and asked all\nthe three teachers, Miss Nash, and Miss Prince, and Miss Richardson,\nto sup with her."\n\n"Mr. Martin, I suppose, is not a man of information beyond the line\nof his own business? He does not read?"\n\n"Oh yes!--that is, no--I do not know--but I believe he has\nread a good deal--but not what you would think any thing of.\nHe reads the Agricultural Reports, and some other books that lay\nin one of the window seats--but he reads all _them_ to himself.\nBut sometimes of an evening, before we went to cards, he would read\nsomething aloud out of the Elegant Extracts, very entertaining.\nAnd I know he has read the Vicar of Wakefield.  He never read the\nRomance of the Forest, nor The Children of the Abbey.  He had never\nheard of such books before I mentioned them, but he is determined\nto get them now as soon as ever he can."\n\nThe next question was--\n\n"What sort of looking man is Mr. Martin?"

或者如果打印出来,

"Well done, Mrs. Martin!" thought Emma.  "You know what you are about."

"And when she had come away, Mrs. Martin was so very kind as to send
Mrs. Goddard a beautiful goose--the finest goose Mrs. Goddard had
ever seen. Mrs. Goddard had dressed it on a Sunday, and asked all
the three teachers, Miss Nash, and Miss Prince, and Miss Richardson,
to sup with her."

"Mr. Martin, I suppose, is not a man of information beyond the line
of his own business? He does not read?"

"Oh yes!--that is, no--I do not know--but I believe he has
read a good deal--but not what you would think any thing of.
He reads the Agricultural Reports, and some other books that lay
in one of the window seats--but he reads all _them_ to himself.
But sometimes of an evening, before we went to cards, he would read
something aloud out of the Elegant Extracts, very entertaining.
And I know he has read the Vicar of Wakefield. He never read the
Romance of the Forest, nor The Children of the Abbey. He had never
heard of such books before I mentioned them, but he is determined
to get them now as soon as ever he can."

The next question was--

"What sort of looking man is Mr. Martin?"

给定某个段落,我想知道该段落的边界在哪里。也就是说,我想通过换行符 \n\n 找到该段落的位置。

我的目标是让我的光标点击某个段落,我就会根据\n\n的位置知道这个段落的边界。

import string
string.find("\n\n")

将输出字符串中空格的位置。但是某个段落呢?如果我“单击”第四段(位于 Vicar of Wakefield),我如何搜索上面的第一个 \n\n 并搜索第一个 \n\n 低于此?

最佳答案

假设您知道在长文本字符串中“单击”的位置 pos,那么您可以使用 str.findstr.rfind() 来解决您的问题。

要“向前看”,您需要执行以下操作:

string.find("\n\n", pos)  # searches for "\n\n" starting from position `pos`, returning the first match

“向后”你会做:

string.rfind("\n\n", 0, pos) # searches for "\n\n" from the beginning up-to `pos` but will return you the last match

有关这两种方法的文档请参阅 https://docs.python.org/2/library/string.html

关于python - 如何使用 Python `string.find()` 找到段落的边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34756611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com