得到一个特定的单词后的模式

2024-04-20 02:26:42 发布

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

我想从下面的例子中取“期望”这个词。在python中我将如何做到这一点。比如在单行注释的情况下要做什么,如下所示。我试过打印之类的文本.拆分(“”)[1]但它只在没有单行注释的情况下工作。你知道吗

    text = /*   */
      The desired {word}
      /* */

Tags: thetext文本情况例子word单行desired
1条回答
网友
1楼 · 发布于 2024-04-20 02:26:42

一种方法是使用^{},它是ConcordanceIndex按偏移量查找周围的单词:

import nltk

text = """/*   */
  The desired word
  /* */"""

tokens = nltk.word_tokenize(text)

c = nltk.ConcordanceIndex(tokens)
print([tokens[offset - 1] for offset in c.offsets("word")])

打印['desired']。你知道吗

相关问题 更多 >