我正在尝试使用lemmatizer
预处理字符串,然后删除标点符号和数字。我正在使用下面的代码来执行此操作。我没有收到任何错误,但文本没有得到适当的预处理。只删除了停用词,但是lemmatizing不起作用,标点符号和数字也保留。
from nltk.stem import WordNetLemmatizer
import string
import nltk
tweets = "This is a beautiful day16~. I am; working on an exercise45.^^^45 text34."
lemmatizer = WordNetLemmatizer()
tweets = lemmatizer.lemmatize(tweets)
data=[]
stop_words = set(nltk.corpus.stopwords.words('english'))
words = nltk.word_tokenize(tweets)
words = [i for i in words if i not in stop_words]
data.append(' '.join(words))
corpus = " ".join(str(x) for x in data)
p = string.punctuation
d = string.digits
table = str.maketrans(p, len(p) * " ")
corpus.translate(table)
table = str.maketrans(d, len(d) * " ")
corpus.translate(table)
print(corpus)
我得到的最终结果是:
This beautiful day16~ . I ; working exercise45.^^^45 text34 .
预期输出应该如下:
This beautiful day I work exercise text
答案 0 :(得分:1)
我认为这就是你要找的东西,但是在评论者注意到这个词之前就这样做了。
>>>import re
>>>s = "This is a beautiful day16~. I am; working on an exercise45.^^^45text34."
>>>s = re.sub(r'[^A-Za-z ]', '', s)
This is a beautiful day I am working on an exercise text
答案 1 :(得分:1)
不,你当前的方法不起作用,因为你必须一次将一个单词传递给lemmatizer / stemmer,否则,这些函数不会知道将你的字符串解释为句子(他们期待的话)。
import re
__stop_words = set(nltk.corpus.stopwords.words('english'))
def clean(tweet):
cleaned_tweet = re.sub(r'([^\w\s]|\d)+', '', tweets.lower())
return ' '.join([lemmatizer.lemmatize(i, 'v')
for i in cleaned_tweet.split() if i not in __stop_words])
或者,您可以使用PorterStemmer
,它与lemmatisation相同,但没有上下文。
from nltk.stem.porter import PorterStemmer
stemmer = PorterStemmer()
然后,像这样叫做词干分析器:
stemmer.stem(i)
答案 2 :(得分:0)
要正确处理一条推文,您可以使用以下代码:
<app-parking-form [mode]="formMode" [parkingModel]="currentParking" (parkingUpdated)="onParkingUpdated()"></app-parking-form>
在这里,我使用了post_tag(仅N,V,J,R并将其余的都转换为名词)。这将返回一个标记化和去词化的单词列表。