2016-10-07 1 views
0

를 사용하여 트위터의 텍스트 데이터를이 형식으로 mongoDB에서 검색 후 :어떻게 전처리 내가 텍스트 데이터를 파이썬

** 나는 모든 대체하고자하지만

[u'In', u'love', u'#Paralympics?\U0001f60d', u"We've", u'got', u'nine', u'different', u'sports', u'live', u'streams', u'https://not_a_real_link', u't_https://anotherLink'] 

[u't_https://somelink'] 

[u'RT', u'@sportvibz:', u'African', u'medal', u'table', u'#Paralympics', u't_https://somelink', u't_https://someLink'] 

**

목록에있는 다른 텍스트를 그대로 유지하면서 'URL'이라는 단어가 포함 된 목록의 URL : 예 :

[u'In', u'love', u'#Paralympics?\U0001f60d', u"We've", u'got', u'nine', u'different', u'sports', u'live', u'streams', u'URL', u'URL'] 

하지만 불용어 제거를위한 코드를 실행하고 또한 정규 표현식을 수행 할 때 나는이 결과 샘플을 얻을 :

**

In 

URL 

RT 

**

사람이 도움이 같은 수주십시오 I 이걸 찾기가 어려워.

def clean_text(self, rawText): 
    temp_raw = rawText 
    for i, text in enumerate(temp_raw): 
     temp = re.sub(r'https?:\/\/.*\/[a-zA-Z0-9]*', 'URL', text) 
    return temp 

대신 목록의 마지막 대체 문자열을 반환, 즉 당신의 rawText 입력 목록을 교체해야합니다 :

def stopwordsRemover(self, rawText): 
    stop = stopwords.words('english') 
    ##remove stop words from the rawText argument and store the result list in processedText variable 
    processedText = [i for i in rawText.split() if i not in stop] 
    return processedText 


def clean_text(self, rawText): 
    temp_raw = rawText 
    for i, text in enumerate(temp_raw): 
     temp = re.sub(r'https?:\/\/.*\/[a-zA-Z0-9]*', 'URL', text) 
    return temp 

답변

0

이 잘못 : 여기

내가 지금 가지고있는 코드입니다 (나는 당신이 첫 번째 항목을 얻는 것처럼 보이지만 나는 여전히 설명에 자신이 있다고 생각하는 것에 당혹감을 느낀다.)

가 대신 수행

def clean_text(self, rawText): 
    return [re.sub(r'https?:\/\/.*\/\w*', 'URL', text) for text in rawText] 

당신이 직접 rawText을 수정, 자리에서 일할 수 :

def clean_text(self, rawText): 
    rawText[:] = [re.sub(r'https?:\/\/.*\/\w*', 'URL', text) for text in rawText] 
listcomp와

def clean_text(self, rawText): 
    temp = list() 
    for text in rawText: 
     temp.append(re.sub(r'https?:\/\/.*\/\w*', 'URL', text)) # simpler regex with \w 
    return temp