【问题标题】:Get a specific word after pattern获取模式后的特定单词
【发布时间】:2016-03-28 01:32:17
【问题描述】:

我想从下面的示例中提取“期望”这个词。我将如何在 python 中做到这一点。如下图在单行 cmets 的情况下该怎么做。我尝试过类似 print text.split(" ")[1] 的方法,但它只适用于没有单行 cmets 的情况。

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

【问题讨论】:

  • 是什么让desired 成为特殊词?
  • @zondo,这是 desired :D(不过不知道,让提问者回复)

标签: python string split


【解决方案1】:

一种选择是使用nltk,它是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']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 2021-08-01
    • 2020-01-10
    • 2021-06-21
    • 2022-07-19
    • 1970-01-01
    • 2020-09-15
    • 2018-05-12
    相关资源
    最近更新 更多