我試圖從文本塊中移除常用詞(連詞,副詞,代詞等)。我使用的是正則表達式,但由於某種原因,我的過濾器中的一些常見詞彙沒有被濾除。未被過濾的正則表達式篩選常用詞
話的一些例子:「還沒有」,「爲什麼」,「應該」
任何想法,爲什麼?
splitResult = s.split()
p = re.compile(
"""^(&|also|a|about|again|all|after|are(nt)?|arent|as|an(y)?|at|
bcuz|before|be(low)?|between|bring|but|by|and|can(not)?|close(d)?|could(nt)?|
cuz|do(nt)?|down|decide(d)?|decision|on(to)?|or|of|our|over|out|have(nt)?|he(re)?|
her|his|other(s)?|even|got(ten)?|for|from|get(s)?|got(ten)?|has(nt)?|havent|he(s)?|
him|his|if|in|to|in(to)?|is(nt)?||make|me|once|play(ed)?|role|say(s)?|seen|she(s)?|
should(nt)?|stop(ped)?|time|my|no(t)?|must(nt)?|now|you(re)?|your|want|want(ed)?|
watch(ed)?|way|we(re)?|will|with||i|a|is(nt)?|just|would(nt)?|before|that|the(re)?|
their|them|they|this|turn|when|at|how|it(s)?|which|who|after|then|if|how|because|know(s)?|
yet|[A-Za-z]{1,2}|http(s)?://.*|www\..*)$""",re.I)
for word in splitResult:
m = p.findall(word)
if not m:
word = "".join(c for c in word if c not in ("?", ".", "!", '"', ",","'","(",")"))
wordsList.insert(ctr,word)
我認爲這是對自然語言處理更合適的工作,看到,例如:http://stackoverflow.com/questions/9953619/technique-to-remove-common-wordsand-their-plural-versions-from-a-string。 – alecxe
我把它放在[Regex101](https://regex101.com/r/wR0dJ2/1)中,你可以在* Explanation *部分看到錯誤(儘管它沒有突出顯示它)。基本上你有'是(nt)?|| make',它應該是'是(nt)?|| make'和'用|| I',它應該是'with | i'。兩個人都有2個'||'而不是1個。這並不能解決問題,但我建議你更新RegEx – Druzion
我找不到任何問題,我只是將捕獲組轉換爲非捕獲以獲得更清晰的輸出:請參見[演示(http://ideone.com/mnC7nr)。請注意,您可以通過對具有共同結局的關鍵字進行分組來使其更有效,從而使其更有效。 –