<p>我试图删除缩写列表中的点,这样它们就不会混淆句子标记器。这应该是非常直接的。不知道为什么我的代码不起作用。你知道吗</p>
<p>请在下面找到我的代码:</p>
<pre><code>abbrevs = [
"No.", "U.S.", "Mses.", "B.S.", "B.A.", "D.C.", "B.Tech.", "Pte.", "Mr.", "O.E.M.",
"I.R.S", "sq.", "Reg.", "S-K."
]
def replace_abbrev(abbrs, text):
re_abbrs = [r"\b" + re.escape(a) + r"\b" for a in abbrs]
abbr_no_dot = [a.replace(".", "") for a in abbrs]
pattern_zip = zip(re_abbrs, abbr_no_dot)
for p in pattern_zip:
text = re.sub(p[0], p[1], text)
return text
text = "Test No. U.S. Mses. B.S. Test"
text = replace_abbrev(abbrevs, text)
print(text)
</code></pre>
<p>这是结果。什么都没发生。怎么了?谢谢。你知道吗</p>
<pre><code> Test No. U.S. Mses. B.S. Test
</code></pre>
<pre><code>re_abbrs = [r"\b" + re.escape(a) for a in abbrs]
</code></pre>
<p>你需要使用这个。那里在<code>.</code>之后没有<code>\b</code>。这将给出正确的输出。你知道吗</p>
<p><code>Test No US Mses BS Test</code></p>