グラフを取得して、Y軸の下にAを、Aを上に、Zを((0,0)
の近くに)リストします。アルファベット順のキー/値のリストをソート
それ石畳の混乱は、私はこれを行う方法を学んでいるが、ここに関連するコードなので、今だ:
from collections import Counter
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
from collections import OrderedDict
from operator import itemgetter
myFile = "D:/User/Documents/Data/English Words/words_alpha.txt"
ie_prefix = {}
ie_prefix.setdefault("letter", [])
ie_prefix.setdefault("word",[])
ie_prefix.setdefault("beforec",[])
ie_prefix_sorted = {}
def get_words(file_import):
global total_words, ie_after_c, ie_after_not_c
#if I don't do this and just keep the below, I get an error: 'total_words' is not defined when running the print(..., total_words, "total words and", ...) line.
total_words = 0
ie_after_not_c = 0
ie_after_c = 0
results = []
with open(file_import) as inputfile:
for line in inputfile:
total_words = total_words + 1
if line.find("ie") != -1:
pos = line.find("ie")
ie_prefix["letter"].append(line[pos-1:pos])
ie_prefix["word"].append(line.strip('\n'))
if line[pos-1:pos] == "c":
ie_prefix["beforec"].append(line.strip('\n'))
ie_after_c += 1
elif line[pos-1:pos] != "c":
ie_after_not_c += 1
ie_prefix_sorted = OrderedDict(sorted(ie_prefix.items()))
return ie_prefix, total_words, ie_after_not_c, ie_after_c, ie_prefix_sorted
def create_graph(total_words, y_axis, x_axis):
y_pos = np.arange(len(y_axis))
fig, ax = plt.subplots()
plt.barh(y_pos, x_axis, align='center', alpha=0.5)
plt.yticks(y_pos, y_axis)
plt.ylabel('Usage')
plt.title('I before E rule')
plt.legend()
plt.show()
get_words(myFile)
# https://stackoverflow.com/questions/20316299/formatting-output-of-counter
ie_count = Counter(ie_prefix["letter"])
ie_count_sorted = sorted(ie_count) #sorted(ie_count.items()) ## THis will just sort the KEYS I believe
ie_letters = list(ie_count_sorted)
###
## How to use the SORTED IE Count in the graph, so it goes from A-Z where A is at the TOP, and Z is at the BOTTOM of Y-Axis (Z closest to (0,0))?
create_graph(total, ie_count, ie_count.values())
FYIここprint(ie_count)
です:
Counter({'r': 2417, 't': 1771, 'l': 1304, 'f': 1034, 'd': 778, 'h': 765, 'p': 753, 'c': 729, 'n': 647, 'm': 536, 'g': 492, 's': 470, 'k': 443, 'v': 273, 'b': 260, 'z': 154, 'u': 134, 'w': 93, 'o': 75, 'x': 73, 'y': 49, 'e': 29, 'a': 26, '': 3, 'j': 2, 'i': 1})
は、私が理解することはできませんie_count
をアルファベット順に並べ替える方法を説明し、値(2417,171など)をキー(文字)で保持します。
私は、Pythonを学んでいるので、このような私は何の何名/タイプとして非常に何かを明らかに(見逃しているかもしれやろうとしている)。私はそのような、またはdownvotesの理由についての任意のノートに感謝したい。 – BruceWayne
私はdownvotedではなく、 'get_words'関数の代わりに(ファイルにアクセスすることができないので少し不必要なので、単に質問から削除することができます)、代わりに' ie_count'を挿入することができます** **関数 'create_graph'は面白いです – MSeifert
@MSeifert - ああ、申し訳ありません!私はそれを忘れてしまった。私は編集します。 FYI単語リストはGitHubの[this file](https://github.com/dwyl/english-words/blob/master/words_alpha.txt)です。 – BruceWayne