2017-09-11 24 views
0

는 데이터 집합 : 아래 일치 단어에 관계없이 사건의

> df 
Id  Clean_Data 
1918916 Luxury Apartments consisting 11 towers Well equipped gymnasium Swimming Pool Toddler Pool Health Club Steam Room Sauna Jacuzzi Pool Table Chess Billiards room Carom Table Tennis indoor games 
1495638 near medavakkam junction calm area near global hospital 
1050651 No Pre Emi No Booking Amount No Floor Rise Charges No Processing Fee HLPROJECT HIGHLIGHTS 

성공적으로
df['one_word_tokenized_text'] =df["Clean_Data"].str.split() 
df['bigram'] = df['Clean_Data'].apply(lambda row: list(ngrams(word_tokenize(row), 2))) 
df['trigram'] = df['Clean_Data'].apply(lambda row: list(ngrams(word_tokenize(row), 3))) 
df['four_words'] = df['Clean_Data'].apply(lambda row: list(ngrams(word_tokenize(row), 4))) 
token=pd.Series(df["one_word_tokenized_text"]) 
Lid=pd.Series(df["Id"]) 
matches= token.apply(lambda x: pd.Series(x).str.extractall("|".join(["({})".format(cat) for cat in Categories.HealthCare]))) 
match_list= [[m for m in match.values.ravel() if isinstance(m, str)] for match in matches] 
match_df = pd.DataFrame({"ID":Lid,"jc1":match_list}) 


def match_word(feature, row): 
    categories = [] 

    for bigram in row.bigram: 
     joined = ' '.join(bigram) 
     if joined in feature: 
      categories.append(joined) 
    for trigram in row.trigram: 
     joined = ' '.join(trigram) 
     if joined in feature: 
      categories.append(joined) 
    for fourwords in row.four_words: 
     joined = ' '.join(fourwords) 
     if joined in feature: 
      categories.append(joined) 
    return categories 

match_df['Health1'] = df.apply(partial(match_word, HealthCare), axis=1) 
match_df['HealthCare'] = match_df[match_df.columns[[1,2]]].apply(lambda x: ','.join(x.dropna().astype(str)),axis=1) 
Category.py

에 값 목록에서 ngrams에 일치하는 단어를 반환하는 코드입니다

Category.py

category = [('steam room','IN','HealthCare'), 
     ('sauna','IN','HealthCare'), 
     ('Jacuzzi','IN','HealthCare'), 
     ('Aerobics','IN','HealthCare'), 
     ('yoga room','IN','HealthCare'),] 
    HealthCare= [e1 for (e1, rel, e2) in category if e2=='HealthCare'] 

출력 : 여기에

ID HealthCare 
1918916 Jacuzzi 
1495638 
1050651 Aerobics, Jacuzzi, yoga room 

데이터 세트에서 언급 한 바와 같이 나는 정확한 대소 문자에서 "카테고리 목록"의 기능을 언급하는 경우, 다음 코드는 다른, 그것을 식별하고 값을 반환 그렇지 않을 것이다. 그래서 코드를 대소 문자를 구분하지 않고 건강 카테고리 아래의 "Steam Room", "사우나"를 추적하기를 원합니다. ".lower()"함수로 시도했지만 구현 방법을 모르겠습니다.

답변

1

편집 2 : 만 category.py가 업데이트

Category.py

category = [('steam room','IN','HealthCare'), 
     ('sauna','IN','HealthCare'), 
     ('jacuzzi','IN','HealthCare'), 
     ('aerobics','IN','HealthCare'), 
     ('Yoga room','IN','HealthCare'), 
     ('booking','IN','HealthCare'),   
     ] 
category1 = [value[0].capitalize() for index, value in enumerate(category)] 
category2 = [value[0].lower() for index, value in enumerate(category)] 

test = [] 
test2 =[] 

for index, value in enumerate(category1): 
    test.append((value, category[index][1],category[index][2])) 

for index, value in enumerate(category2): 
    test2.append((value, category[index][1],category[index][2])) 

category = category + test + test2 


HealthCare = [e1 for (e1, rel, e2) in category if e2=='HealthCare'] 

는 귀하의 변경되지 않은 데이터 집합

import pandas as pd 
from nltk import ngrams, word_tokenize 
import Categories 
from Categories import * 
from functools import partial 


data = {'Clean_Data':['Luxury Apartments consisting 11 towers Well equipped gymnasium Swimming Pool Toddler Pool Health Club Steam Room Sauna Jacuzzi Pool Table Chess Billiards room Carom Table Tennis indoor games', 
        'near medavakkam junction calm area near global hospital', 
        'No Pre Emi No Booking Amount No Floor Rise Charges No Processing Fee HLPROJECT HIGHLIGHTS '], 
'Id' : [1918916, 1495638,1050651]} 

df = pd.DataFrame(data) 


df['one_word_tokenized_text'] =df["Clean_Data"].str.split() 
df['bigram'] = df['Clean_Data'].apply(lambda row: list(ngrams(word_tokenize(row), 2))) 
df['trigram'] = df['Clean_Data']).apply(lambda row: list(ngrams(word_tokenize(row), 3))) 
df['four_words'] = df['Clean_Data'].apply(lambda row: list(ngrams(word_tokenize(row), 4))) 
token=pd.Series(df["one_word_tokenized_text"]) 
Lid=pd.Series(df["Id"]) 
matches= token.apply(lambda x: pd.Series(x).str.extractall("|".join(["({})".format(cat) for cat in Categories.HealthCare]))) 
match_list= [[m for m in match.values.ravel() if isinstance(m, str)] for match in matches] 
match_df = pd.DataFrame({"ID":Lid,"jc1":match_list}) 


def match_word(feature, row): 
    categories = [] 

    for bigram in row.bigram: 
     joined = ' '.join(bigram) 
     if joined in feature: 
      categories.append(joined) 
    for trigram in row.trigram: 
     joined = ' '.join(trigram) 
     if joined in feature: 
      categories.append(joined) 
    for fourwords in row.four_words: 
     joined = ' '.join(fourwords) 
     if joined in feature: 
      categories.append(joined) 
    return categories 

match_df['Health1'] = df.apply(partial(match_word, HealthCare), axis=1) 
match_df['HealthCare'] = match_df[match_df.columns[[1,2]]].apply(lambda x: ','.join(x.dropna().astype(str)),axis=1)enize(row), 4))) 

는 출력이

print match_df 

+--------+----------------+-------------+------------------------------------+ 
|ID  |jc1    |Health1  |HealthCare       | 
+--------+----------------+-------------+------------------------------------+ 
|1918916 |[sauna, jacuzzi]|    |['sauna', 'jacuzzi'],['steam room'] | 
+--------+----------------+-------------+------------------------------------+ 
|1495638 |    |    |         | 
+--------+----------------+-------------+------------------------------------+ 
|1050651 | [Booking] |    | ['Booking'],[]     |    | 
+--------+----------------+-------------+------------------------------------+ 
+0

아니, 난은 가정하지 있어요 내 데이터 세트 값을 수정하십시오. 나는 단지 단어를 사건과 관계없이 범주 값과 매치시키고 싶다. –

+0

데이터 세트에 이미 열을 추가하고 있습니다. 방금 본 방법으로 내 대답을 편집했습니다. - a) 작성중인 3 열의 모든 항목을 위/아래로 만듭니다. 아래쪽/대문자 변수 - b) Category.py 에서 가능한 모든 대문자 사용 형식을 (파이썬 코드를 사용하여) 재현 해보십시오. 후자는 과도한 것으로 보입니다. – Pelican

+0

죄송합니다. 제 질문이 혼란 스럽다면, 귀하의 요지를 이해합니다. 그러나 제 관심사는 최종 산출 값의 대소 문자가 데이터 세트에서받은 값과 다르지 않아야한다는 것입니다. "사우나", "스팀 룸"에 InitialCaps가있는 경우 동일한 것을 출력으로 제시해야합니다. 내 데이터 집합에 장래에 대문자와 소문자가 비슷한 단어가 포함되어 있으면 내 코드가 대/소문자를 구분하지 않아야합니다. :) –