2014-05-19 3 views
2

여러 단어로 된 텍스트가 있습니다. 단어의 모든 파생 확장을 제거하고 싶습니다. 예를 들어 확장 기능 -ed -ing을 제거하고 초기 동사를 유지하려고합니다. 본인이 확인 또는 확인을 계속하는 경우 파이썬에서 메서드 스트립을 발견했습니다.이 스트립은 문자열의 처음이나 끝에서 특정 문자열을 제거하지만 정확히 원하는 것은 아닙니다. 파이썬에서 그런 일을하는 라이브러리가 있습니까?파이썬에서 단어 확장자를 제거하십시오.

나는 제안 된 게시물에서 코드를 수행하려고 시도했으며 여러 단어로 이상한 트리밍을 발견했습니다. 예를 들어, 나는 다음과 같은 텍스트

We goin all the way βπƒβ΅οΈβ΅οΈ   
Think ive caught on to a really good song ! Im writing π  
Lookin back on the stuff i did when i was lil makes me laughh π‚  
I sneezed on the beat and the beat got sicka  
#nashnewvideo http://t.co/10cbUQswHR  
Homee βοΈβοΈβοΈπ΄  
So much respect for this man , truly amazing guy βοΈ @edsheeran 
http://t.co/DGxvXpo1OM"   
What a day ..  
RT @edsheeran: Having some food with @ShawnMendes  
#VoiceSave christina π   
Im gunna make the βοΈ sign my signature pose  
You all are so beautiful .. π soooo beautiful  
Thought that was a really awesome quote   
Beautiful things don't ask for attention""" 

있어 그리고 다음 코드의 사용 후에는 beauti에 아름답고 인용 트림 예를 들어

we goin all the way 
think ive caught on to a realli good song im write 
lookin back on the stuff i did when i wa lil make me laughh 
i sneez on the beat and the beat got sicka 
nashnewvideo 
home 
so much respect for thi man truli amaz guy 
what a day 
rt have some food with 
voicesav christina 
im gunna make the sign my signatur pose 
you all are so beauti soooo beauti 
thought that wa a realli awesom quot 
beauti thing dont ask for attent 

(또한 나는 비 라틴 문자와 URL을 제거) 진짜로 realli에 말하십시오.

reader = csv.reader(f) 
    print doc 
    for row in reader: 
     text = re.sub(r"(?:\@|https?\://)\S+", "", row[2]) 
     filter(lambda x: x in string.printable, text) 
     out = text.translate(string.maketrans("",""), string.punctuation) 
     out = re.sub("[\W\d]", " ", out.strip()) 
     word_list = out.split() 
     str1 = "" 
     for verb in word_list: 
       verb = verb.lower() 
       verb = nltk.stem.porter.PorterStemmer().stem_word(verb) 
       str1 = str1+" "+verb+" " 
     list.append(str1) 
     str1 = "\n" 
+0

네 줄기 내가 찾고 있던 단어입니다. 나는 제안 된 게시물의 예제를 시도했지만 심하게 말을 트리밍 것으로 나타났습니다. –

답변

2

가 대신 stemmer 당신이 lemmatizer을 사용할 수 있습니다 : 내 코드는 다음과 같다. 여기 파이썬 NLTK와 예는 다음과 같습니다 어떤 경우에는

from nltk.stem import WordNetLemmatizer 

s = """ 
You all are so beautiful soooo beautiful 
Thought that was a really awesome quote 
Beautiful things don't ask for attention 
""" 

wnl = WordNetLemmatizer() 
print " ".join([wnl.lemmatize(i) for i in s.split()]) #You all are so beautiful soooo beautiful Thought that wa a really awesome quote Beautiful thing don't ask for attention 

는, 당신이 무엇을 기대하지 않을 수 있습니다

print wnl.lemmatize('going') #going 

는 그런 다음 두 가지 접근 방식을 결합 할 수 있습니다 : stemminglemmatization.

+0

나는 lemmatizer로 다음과 같은 결과를 얻고 있습니다 : 당신은 모두 아름답습니다. soooo beautiful 정말 근사한 말을 생각했습니다. 아름다운 것은주의를 요구하지 않습니다. –

3

귀하의 질문은 조금 일반적이지만, 이미 정의 된 정적 텍스트가있는 경우 가장 좋은 방법은 자신의 stemmer을 작성하는 것입니다. PorterLancaster 스 트리머는 접미사 제거에 대한 자체 규칙을 따르므로 WordNet lemmatizer은 결과 단어가 사전에있는 경우에만 접미사를 제거하기 때문입니다.

당신은 같은 작성할 수 있도록 "처리 프로세스"에 대한

import re 


def stem(word): 
    for suffix in ['ing', 'ly', 'ed', 'ious', 'ies', 'ive', 'es', 's', 'ment']: 
     if word.endswith(suffix): 
      return word[:-len(suffix)] 
    return word 


def stemmer(phrase): 
    for word in phrase: 
     if stem(word): 
      print re.findall(r'^(.*)(ing|ly|ed|ious|ies|ive|es|s|ment)$', word) 

을 당신은해야합니다 :

>> stemmer('processing processes') 
[('process', 'ing'),('process', 'es')] 
관련 문제