python - 将句子拆分为其标记作为字符注释 Python
问题描述
经过长时间的搜索,我没有找到任何问题的答案,这就是为什么我决定把我的问题放在这里。我正在尝试使用 RE 和 NLTK 获得一些特定的结果。给定一个句子,在每个字符上我必须使用BIS
格式,即将每个字符标记为B (beginning of the token)
, I (intermediate or end position of the token)
, S for space
。例如,给定句子:
笔在桌子上。
系统必须提供以下输出:
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
查看正则表达式演示
图案细节
(\w)(\w*)
- 第 1 组:任何字字符(字母、数字或_
),然后第 2 组:任何 0 个或多个字字符|
- 或者([^\w\s])
- 第 3 组:除单词和空格字符外的任何字符|
- 或者\s
- 一个空格字符
如果 Group 1 匹配,则返回值为B
+ 与I
Group 2 中的字符数相同的 s 数。如果 Group 3 匹配,则替换为B
。否则,匹配一个空格,替换为S
.
这可以进一步定制,例如
- 仅视为
_
标点符号:r'([^\W_])([^\W_]*)|([^\w\s]|_)|\s'
- 用单个替换 1 个或多个空格
S
:r'([^\W_])([^\W_]*)|([^\w\s]|_)|\s+'
在线查看Python 演示:
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
推荐阅读
- regex - /(?<=.*)(\w\w)(?=.*)/ 没有给出我所期望的
- c# - 将事件从 UserControl 传递到父窗体
- c# - 在 Dotnet 5 中反序列化 2.6k 行 XML
- c++ - 如何使用 ESP Now 在两个 ESP32 MCU 之间发送音频数据包?
- javascript - 如何恢复显示 [[PromiseResult]]: undefined 的承诺的价值?
- apache - Apache .htaccess 将不存在的文件映射到脚本
- javascript - Asp.Net Core MVC Google Maps api Plot Ployline from SQL
- r - 根据数据集列中的名称设置条件
- kotlin - Kotlin 中的函数式编程,Manning 书 - 二叉树结构
- pandas - Pandas DataFrame 中的 spark.createDataFrame 仅复制标头