我想从文本中删除停用词,但无法正确使用正则表达式和变量.例如,我删除了停用词"he",但这也会影响单词"when".我尝试使用这样的单词边界:
new RegExp('\b'+stopwords[i]+'\b' , 'g') 但不起作用......
在这里看一个小例子:jsFiddle
var stopwords = ['as', 'at', 'he', 'the', 'was'];
for (i = 0; i < stopwords.length; i++) {
str = str.replace(new RegExp(stopwords[i], 'g'), '');
}
Run Code Online (Sandbox Code Playgroud) 我在修改R的TM包中的english.dat stopword文件时遇到问题.我添加到它的任何东西都是无法识别的.我尝试在文件的开头添加,中间,结尾,仍然无效.仅识别文件的原始文本.我尝试将文件保存为ASCI,UTF,UTF-8,但无济于事.
有任何想法吗?
谢谢
我想避免删除停用词,但我发现无论参数设置如何tm,总是删除一些停用词.
library(tm)
documents <- c("This is a list containing the tallest buildings in San Francisco")
corpus <- Corpus(VectorSource(documents))
matrix <- DocumentTermMatrix(corpus,control=list(stopwords=FALSE))
colnames(matrix)
# [1] "buildings" "containing" "francisco" "list" "san"
# [6] "tallest" "the" "this"
Run Code Online (Sandbox Code Playgroud)
DocumentTermMatrix 似乎删除了"是"和"在"中的停用词.
我怎么能避免这个?设置stopwords=TRUE仅阻止删除"the".我怎样才能防止删除"是"和"进入"?
我有一个Python脚本,它接收'.html'文件删除停用词并返回python词典中的所有其他单词.但是如果在多个文件中出现相同的单词,我希望它只返回一次.即包含不间断的单词,每次只包含一次.
def run():
filelist = os.listdir(path)
regex = re.compile(r'.*<div class="body">(.*?)</div>.*', re.DOTALL | re.IGNORECASE)
reg1 = re.compile(r'<\/?[ap][^>]*>', re.DOTALL | re.IGNORECASE)
quotereg = re.compile(r'"', re.DOTALL | re.IGNORECASE)
puncreg = re.compile(r'[^\w]', re.DOTALL | re.IGNORECASE)
f = open(stopwordfile, 'r')
stopwords = f.read().lower().split()
filewords = {}
htmlfiles = []
for file in filelist:
if file[-5:] == '.html':
htmlfiles.append(file)
totalfreq = {}
for file in htmlfiles:
f = open(path + file, 'r')
words = f.read().lower()
words = regex.findall(words)[0]
words = quotereg.sub(' ', words)
words = reg1.sub(' …Run Code Online (Sandbox Code Playgroud) 我有这个函数,如果在数组中找到一个坏词,则返回true $stopwords
function stopWords($string, $stopwords) {
$stopwords = explode(',', $stopwords);
$pattern = '/\b(' . implode('|', $stopwords) . ')\b/i';
if(preg_match($pattern, $string) > 0) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作正常.
问题是,当数组$stopwords为空(所以没有指定坏字)时,它总是返回true,就好像空值被识别为坏词并且它总是返回true(我认为这是问题,但可能是另一个) ).
任何人都可以帮我解决这个问题吗?
谢谢
我正在预处理文本并想删除德语中的常见停用词。使用以下代码 [final_wordlist 作为示例数据] 几乎可以正常工作:
from nltk.corpus import stopwords
final_wordlist =['Status', 'laufende', 'Projekte', 'bei', 'Stand', 'Ende', 'diese', 'Bei']
stopwords_ger = stopwords.words('german')
filtered_words = [w for w in final_wordlist if w not in stopwords_ger]
print(filtered_words)
Run Code Online (Sandbox Code Playgroud)
这产生:
['Status', 'laufende', 'Projekte', 'Stand', 'Ende', 'Bei']
Run Code Online (Sandbox Code Playgroud)
但是正如您所看到的,大写的 'Bei' 没有被删除(应该如此),因为来自 nltk 的停用词都是小写的。有没有一种简单的方法可以不区分大小写地删除所有停用词?
我需要从文本中删除停用词,而不将对象标记化或更改为列表。使用 rm_stopwords 函数时出现错误。谁能帮我吗?
test<- data.frame(words = c("hello there, everyone", "the most amazing planet"), id = 1:2)
test$words <- rm_stopwords(test$words, tm::stopwords("english"), separate = F, unlist = T)
#Error in `$<-.data.frame`(`*tmp*`, words, value = c("hello", "everyone", :
#replacement has 4 rows, data has 2
#I want something like this, where the stopwords are removed but the rest of the formatting remains intact (e.g. punctuation)
# words id
#1 hello , everyone 1
#2 amazing planet 2
Run Code Online (Sandbox Code Playgroud) 我在 python 的 pandas 数据框中有一列标记。看起来像这样的东西:
word_tokens
(the,cheeseburger,was,great)
(i,never,did,like,the,pizza,too,much)
(yellow,submarine,was,only,an,ok,song)
Run Code Online (Sandbox Code Playgroud)
我想使用 spacy 库在此数据框中再获得两个新列。一列包含删除了停用词的每一行的标记,另一列包含第二列中的引理。我怎么能这么做呢?
stop-words ×8
python ×3
r ×3
regex ×2
tm ×2
dictionary ×1
duplicates ×1
javascript ×1
nlp ×1
nltk ×1
pandas ×1
php ×1
spacy ×1
text ×1
text-mining ×1
tidyr ×1
tidyverse ×1
variables ×1