2016-12-01 17 views
-1

私は、コード行を1つの "。"になるまで読み込むプログラムを作成する必要があります。句読点を削除し、すべて小文字に変更し、ストップワードとサフィックスを削除する必要があります。私はサフィックスを削除することを除いてこれをすべて管理しました。私はあなたが見ることができるように.stripを試しましたが、それは1つの引数を受け入れ、実際にはリスト要素からサフィックスを削除しませんでした。アドバイス/ポインタ/助け?ありがとうPythonのリスト要素から接尾辞を削除する

stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \ 
      "of", "from", "here", "even", "the", "but", "and", "is", "my", \ 
      "them", "then", "this", "that", "than", "though", "so", "are" ] 

noStemWords = [ "feed", "sages", "yearling", "mass", "make", "sly", "ring" ] 


# -------- Replace with your code - e.g. delete line, add your code here ------------ 

Text = raw_input("Indexer: Type in lines, that finish with a . at start of line only: ").lower() 
while Text != ".": 
    LineNo = 0 
    x=0 
    y=0 
    i= 0 

#creates new string, cycles through strint Text and removes puctutaiton 
    PuncRemover = "" 
    for c in Text: 
     if c in ".,:;!?&'": 
      c="" 
     PuncRemover += c 

    SplitWords = PuncRemover.split() 

#loops through SplitWords list, removes value at x if found in StopWords list 
    while x < len(SplitWords)-1: 
     if SplitWords[x] in stopWords: 
      del SplitWords[x] 
     else: 
      x=x+1 

    while y < len(SplitWords)-1: 
     if SplitWords[y] in noStemWords: 
      y=y+1 
     else: 
      SplitWords[y].strip("ed") 
      y=y+1 

    Text = raw_input().lower() 

print "lines with stopwords removed:" + str(SplitWords) 
print Text 
print LineNo 
print x 
print y 
print PuncRemover 
+0

あなたは 'について – martianwars

+1

カップルの事をraw_input'を見て、一度だけここで読んでいますコードスタイルを最初に使用します。 [Python命名規則](https://www.python.org/dev/peps/pep-0008/#naming-conventions)を見てください。大文字の単語は、一般的にクラスや型変数のために予約されています。また、 'while'ループは本当に' for'ループでなければなりません。あなたは繰り返し実行する回数を知っているからです。あなたの問題では、変更されているリスト要素を実際に割り当てる必要があります。一連の文字を削除するには、[この質問](http://stackoverflow.com/questions/3900054/python-strip-multiple-characters)を参照してください。 – danielunderwood

+0

読み込み行は辞書に追加されるためのもので、今は一度だけ読み込みます。 – Rydooo

答えて

0

次の関数は、任意の文字列から接尾辞を削除する必要があります。

from itertools import groupby 


def removeSuffixs(sentence): 

    suffixList = ["ing", "ation"] #add more as nessecary 

    for item in suffixList: 
     if item in sentence: 

      sentence = sentence.replace(item, "") 
      repeatLetters = next((True for char, group in groupby(sentence) 
            if sum(1 for _ in group) >= 2), False) 

      if repeatLetters: 

       sentence = sentence[:-1] 

    return sentence 

例:あなたのコードで

print(removeSuffixs("climbing running")) # 'climb run' 
print(removeSuffixs("summation")) # 'sum' 

、とSplitWords[y].strip("ed") を置き換える、

SplitWords[y] = removeSuffixs(SplitWords[y])

関連する問題