if word.isalpha():
                freq[word] = 1 + freq.get(word, 0)

#for (w,c) in freq.items():
#print (w,c)

print("""
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" 
xml:space="preserve" style="shape-rendering:geometricPrecision; 
text-rendering:geometricPrecision; image-rendering:optimizeQuality; 
fill-rule:evenodd; clip-rule:evenodd" viewBox="0 0 2000 1000">""")
string = ""
betweenSpace = 0
currentDepth = 0
for node in freq.breadthfirst():
    if freq.depth(node) > 6:
        break
    else:
        if freq.depth(node) == 0 or freq.depth(node) != currentDepth:
            x = 2000 / (2**(freq.depth(node) + 1))
            betweenSpace = x * 2
            currentDepth = freq.depth(node)
        else:
            x = x + betweenSpace
        string += "<circle style='fill:url(#toning);stroke:#010101;stroke-width:1.6871;stroke-miterlimit:10;' cx='"
        string += str(x)
        string += "' cy='"
        y = str((freq.depth(node) + 1) * 20)
        string += y
        string += "' r='19'></circle><text x='"
        string += str(x)
Пример #2
0
max_word = ''
max_count = 0
for (w, c) in freq.items():  # (key, value) tuples represent (word, count)
    if c > max_count:
        max_word = w
        max_count = c
print('The most frequent word is', max_word)
print('Its number of occurrences is', max_count)

print("""
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd"
viewBox="0 0 2000 1000">
    <circle style="fill:url(#toning);stroke:#010101;stroke-width:1.6871;stroke-miterlimit:10;" cx="1000" cy="20" r="19">
    </circle>
    <text x="1000" y="20" text-anchor="middle" >Root</text>

    <circle style="fill:url(#toning);stroke:#010101;stroke-width:1.6871;stroke-miterlimit:10;" cx="500" cy="40" r="19">
    </circle>
    <text x="500" y="40" text-anchor="middle" >Left</text>
</svg>
""")

# how to get information out of a node
for node in freq.breadthfirst():
    print("Node depth:" + str(freq.depth(node)))
    print("Node height:" + str(freq.height(node)))
    print("Node value:" + str(node.value()))
    print("Node key:" + str(node.key()))
    print("<br/>")