#python #nlp #tokenize #re #countvectorizer
Вопрос:
Я использовал общий маркизатор, чтобы иметь дело с данными об удобствах airbnb.
def tokenizer(text): # Replace urls with a special token text = re.sub('https?S ', 'xxurl', text) text = re.sub('S*www[.]S ', 'xxurl', text) # Replace emoticons with special tokens text = re.sub(r'lt;3', 'xxheart', text) text = re.sub(r'amp;lt;3', 'xxheart', text) text = re.sub('lt;br /gt;',' ',text) text = re.sub('lt;bgt;',' ',text) text = re.sub('lt;/bgt;',' ', text) # Creating a special token for hashtags text = re.sub(r'#(S )', 'xxhashtag ' r'1', text) # hashtag # Stripping repeated whitespace text = re.sub(r's{2,}', ' ', text) doc = nlp(text) tokens = [] for token in doc: word = token.lemma_.lower() if not token.is_stop: if word == '!': tokens.append('!') elif token.like_num: tokens.append('xxnumber') elif (not token.is_punct) and word != '️': tokens.append(word) return tokens
Получите результаты, как показано ниже. Я обнаружил, что фразы были разделены на отдельные слова.
И я уравновешиваю их.
from sklearn.feature_extraction.text import CountVectorizer counter = CountVectorizer(tokenizer=id_tokenlize, max_features = 30, max_df= 0.9, stop_words='english' ,lowercase = False, ngram_range = (2,2)) corpus = train['amenities'] counter.fit(corpus) train_text = tfidf.transform(corpus).todense() name_list = tfidf.get_feature_names() amentities = pd.DataFrame(train_text, columns = name_list) amentities = amentities.add_prefix('amentities_') amentities
Заголовки должны быть такими: Патио или балкон, Сад или задний двор, разрешено длительное пребывание вместо отдельных слов. Как я могу этого достичь? и что я должен сделать для проверки данных? Спасибо