2017-09-29 4 views

Répondre

3

Utilisation d'expressions régulières pour correspondre uniquement des lettres (et souligne), vous pouvez le faire:

import re 

s = "[email protected] said: I've taken 2 reports to the boss" 
# s = open('text.txt').read() 

tokens = s.strip().split() 
clean_tokens = [t for t in tokens if re.match(r'[^\W\d]*$', t)] 
# ['taken', 'reports', 'to', 'the', 'boss'] 
clean_s = ' '.join(clean_tokens) 
# 'taken reports to the boss' 
0

peut cela contribuera

array = string.split(' ') 
result = [] 
for word in array 
if word.isalpha() 
    result.append(word) 
string = ' '.join(result) 
2

Essayez ceci:

sentence = "[email protected] said: I've taken 2 reports to the boss" 
words = [word for word in sentence.split() if word.isalpha()] 
# ['taken', 'reports', 'to', 'the', 'boss'] 

result = ' '.join(words) 
# taken reports to the boss 
0

Vous peut soit utiliser regex ou peut utiliser python dans la fonction de construction tels que isalpha()

Exemple d'utilisation isalpha()

result = '' 
with open('file path') as f: 
line = f.readline() 
a = line.split() 
for i in a: 
    if i.isalpha(): 
     print(i+' ',end='') 
0

str.join() + compréhension vous donnera une solution d'une ligne:

sentence = "[email protected] said: I've taken 2 reports to the boss" 
' '.join([i for i in sentence.split() if i.isalpha()]) 
#'taken reports to the boss' 
2

Vous pouvez utiliser split() et est isalpha() pour obtenir une liste de mots qui ne avoir des caractères alphabétiques ET il y a au moins un caractère.

>>> sentence = "[email protected] said: I've taken 2 reports to the boss" 
>>> alpha_words = [word for word in sentence.split() if word.isalpha()] 
>>> print(alpha_words) 
['taken', 'reports', 'to', 'the', 'boss'] 

Vous pouvez ensuite utiliser join() pour faire la liste en une chaîne:

>>> alpha_only_string = " ".join(alpha_words) 
>>> print(alpha_only_string) 
taken reports to the boss 
1

Le paquet nltk est spécialisée dans la gestion du texte et a diverses fonctions que vous pouvez utiliser pour texte « tokenize » en mots.

Vous pouvez utiliser le RegexpTokenizer ou le word_tokenize avec une légère adaptation.

Le plus simple et le plus simple est le RegexpTokenizer:

import nltk 

text = "[email protected] said: I've taken 2 reports to the boss. I didn't do the other things." 

result = nltk.RegexpTokenizer(r'\w+').tokenize(text) 

qui retourne:

`['asdf', 'gmail', 'com', 'said', 'I', 've', 'taken', '2', 'reports', 'to', 'the', 'boss', 'I', 'didn', 't', 'do', 'the', 'other', 'things']` 

Ou vous pouvez utiliser le word_tokenize un peu plus intelligent qui est capable de diviser la plupart des contractions comme didn't en did et n't .

import re 
import nltk 
nltk.download('punkt') # You only have to do this once 

def contains_letters(phrase): 
    return bool(re.search('[a-zA-Z]', phrase)) 

text = "[email protected] said: I've taken 2 reports to the boss. I didn't do the other things." 

result = [word for word in nltk.word_tokenize(text) if contains_letters(word)] 

qui retourne:

['asdf', 'gmail.com', 'said', 'I', "'ve", 'taken', 'reports', 'to', 'the', 'boss', 'I', 'did', "n't", 'do', 'the', 'other', 'things']