【发布时间】:2020-04-09 15:44:52
【问题描述】:
经过长时间的搜索,我没有找到任何问题的答案,这就是我决定将问题放在这里的原因。我正在尝试使用 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)
【问题讨论】:
-
请看我的方法。我没有提供所有可能的替代方案,您需要首先提出更精确的规范/示例。随意发表评论。
-
@Wiktor Stribiżew 有没有办法使用 nltk tokenizer 来实现它?
-
我不这么认为。
标签: python python-3.x nltk tokenize python-re