经过长时间的搜索,我没有找到我的问题的任何答案,这就是为什么我决定把我的问题放在这里。我正在尝试使用 RE 和 NLTK 获得一些特定的结果。
给定一个句子,我必须在每个字符上使用 BIS
格式,即将每个字符标记为 B (token 的开头)
, I (intermediate或 token 的结束位置)
,S 代表空格
。
例如,给定句子:
The pen is on the table.
系统必须提供以下输出:
BIISBIISBISBISBIISBIIIIB
可以理解为:
<3-char token> <space> <3-char token> <space> <2-char token> <space> <2-char token> <space> <3-char token> <space> <5-char token> <1-char token>)
我的结果有点接近,但不是:
BIISBIISBISBISBIISBIIIIB
我得到:
BIISBIISBISBISBIISBIIIISB
意思是我在 table
和点 之间得到了空间。
输出应该是:
<3-char token> <space> <3-char token> <space> <2-char token> <space> <2-char token> <space> <3-char token> <space> <5-char token> <1-char token>
我的是:
<3-char token> <space> <3-char token> <space> <2-char token> <space> <2-char token> <space> <3-char token> <space> <5-char token> <space> <1-char token>
到目前为止我的代码:
from nltk.tokenize import word_tokenize
import re
p = "The pen is on the table."
# Split text into words using NLTK
text = word_tokenize(p)
print(text)
initial_char = [x.replace(x[0],'B') for x in text]
print(initial_char)
def listToString(s):
# initialize an empty string
str1 = " "
# return string
return (str1.join(s))
new = listToString(initial_char)
print(new)
def start_from_sec(my_text):
return ' '.join([f'{word[0]}{(len(word) - 1) * "I"}' for word in my_text.split()])
res = start_from_sec(new)
p = re.sub(' ', 'S', res)
print(p)
最佳答案
您可以使用单个正则表达式来标记字符串:
(\w)(\w*)|([^\w\s])|\s
参见 regex demo
图案细节
(\w)(\w*)
- 第 1 组:任何字符字符(字母、数字或_
),然后第 2 组:任何 0 个或多个单词字符|
- 或者([^\w\s])
- 第 3 组:除单词和空白字符之外的任何字符|
- 或者\s
- 一个空白字符
如果第 1 组匹配,则返回值为 B
+ 与第 2 组中的字符数相同的 I
。如果第 3 组匹配,则替换为B
。否则,匹配一个空格,替换为 S
。
这可以进一步定制,例如
- 仅将
_
视为标点符号:r'([^\W_])([^\W_]*)|([^\w\s]|_)|\s'
- 用单个
S
替换 1 个或多个空格:r'([^\W_])([^\W_]*)|([^\w\s]| _)|\s+'
参见 Python demo online :
import re
p = "The pen is on the table."
def repl(x):
if x.group(1):
return "B{}".format("I"*len(x.group(2)))
elif x.group(3):
return "B"
else:
return "S"
print( re.sub(r'(\w)(\w*)|([^\w\s])|\s', repl, p) )
# => BIISBIISBISBISBIISBIIIIB
关于python - 将句子拆分成它的标记作为字符注释 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124955/