Python 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 19:08:05)
Type "copyright", "credits" or "license" for more information.
IPython 7.18.1 -- An enhanced Interactive Python.
In [1]: ls
11/ 26/ 27/ 54/ 'FILE E WEB.txt' files/ files.zip
In [2]: pwd
Out[2]: '/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20'
In [3]: ls /mnt/D/
'$Recycle.Bin'/
{10E7BD57-C5FE-484f-A3F2-A1755286C0A7}/
{7A7ACBDD-FED6-4ec5-BD26-5549FEB5B968}/
adb/
Anaconda3/
andrea/
BACKUP/
Config.Msi/
CONSEGNE-esame-16-7-19/
cygwin64/
'Documents and Settings'@
DumpStack.log.tmp*
emu/
hiberfil.sys*
HW2req/
iso/
msdia80.dll*
pagefile.sys*
ProgramData/
'Program Files'/
'Program Files (x86)'/
Programmi@
Recovery/
stanford-corenlp-4.0.0/
SteamLibrary/
swapfile.sys*
'System Volume Information'/
Videos/
WindowsApps/
wpa_supplicantlun-2017-8-14-9-10-13.conf*
WpSystem/
WUDownloadCache/
In [4]: ls /mnt/C/Users
'All Users'@ Default/ desktop.ini* Test/
Andrea/ 'Default User'@ Public/
In [5]: ls
11/ 26/ 27/ 54/ 'FILE E WEB.txt' files/ files.zip
In [6]: ls files
alice_it.txt frankenstein.txt log.log prince.txt testo2.txt words.py
alice.txt holmes.txt logo.png results.txt testo.txt
In [7]: ls ..
AA1920-HW4obb/ lezione04-16-10-20/ lezione08-30-10-20/
lezione01-6-10-20/ lezione05-20-10-20/ lezione09-3-11-20/
lezione02-9-10-20/ lezione06-23-10-20/
lezione03-13-10-20/ lezione07-27-10-20/
In [8]: ls ../..
Esami/ Lezioni/ usermeeting_2020-10-01_2020-10-13.csv Voti/
HW/ token varie/
In [9]: ls .
11/ 26/ 27/ 54/ 'FILE E WEB.txt' files/ files.zip
In [10]: ls ./FILE\ E\ WEB.txt
'./FILE E WEB.txt'
In [11]: ls ./*.txt
'./FILE E WEB.txt'
In [12]: alice = open('files/alice.txt')
In [13]: type(alice)
Out[13]: _io.TextIOWrapper
In [14]: testo = alice.read()
In [15]: len(testo)
Out[15]: 163780
In [16]: testo[:100]
Out[16]: "\ufeffProject Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll\n\nThis eBook is for the use o"
In [17]: alice = open('files/alice.txt', encoding='utf-8-sig')
In [18]: testo = alice.read()
In [19]: len(testo)
Out[19]: 163779
In [20]: testo[:100]
Out[20]: "Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll\n\nThis eBook is for the use of"
In [21]: righe = testo.split('\n')
In [22]: len(righe)
Out[22]: 3736
In [23]: righe[120:130]
Out[23]:
["bats eat cats?' for, you see, as she couldn't answer either question,",
"it didn't much matter which way she put it. She felt that she was dozing",
'off, and had just begun to dream that she was walking hand in hand with',
"Dinah, and saying to her very earnestly, 'Now, Dinah, tell me the truth:",
"did you ever eat a bat?' when suddenly, thump! thump! down she came upon",
'a heap of sticks and dry leaves, and the fall was over.',
'',
'Alice was not a bit hurt, and she jumped up on to her feet in a moment:',
'she looked up, but it was all dark overhead; before her was another',
'long passage, and the White Rabbit was still in sight, hurrying down it.']
In [24]: with open('files/alice.txt', encoding='utf-8-sig') as F:
...: lista = []
...: for i, line in enumerate(F):
...: if 'Alice' in line:
...: lista.append(i)
...:
...:
In [25]: %pprint
Pretty printing has been turned OFF
In [26]: lista
Out[26]: [0, 8, 41, 44, 53, 59, 65, 69, 85, 93, 98, 112, 117, 127, 130, 138, 143, 151, 156, 160, 167, 171, 182, 193, 201, 207, 215, 222, 228, 235, 253, 261, 279, 283, 294, 300, 335, 343, 356, 367, 375, 386, 390, 397, 399, 403, 409, 412, 416, 424, 426, 432, 440, 446, 460, 463, 470, 504, 506, 521, 546, 550, 558, 560, 568, 579, 583, 586, 637, 640, 645, 651, 656, 666, 672, 682, 687, 702, 709, 712, 725, 727, 730, 759, 764, 774, 778, 787, 793, 794, 797, 822, 827, 844, 862, 868, 871, 874, 876, 882, 893, 897, 910, 918, 921, 929, 933, 941, 959, 965, 973, 978, 984, 991, 997, 1003, 1010, 1014, 1019, 1025, 1031, 1035, 1080, 1090, 1095, 1101, 1106, 1113, 1120, 1125, 1149, 1155, 1171, 1177, 1183, 1190, 1198, 1203, 1212, 1219, 1224, 1229, 1245, 1261, 1274, 1279, 1288, 1294, 1308, 1313, 1321, 1325, 1333, 1342, 1348, 1357, 1362, 1371, 1378, 1385, 1408, 1419, 1424, 1426, 1435, 1438, 1441, 1444, 1450, 1454, 1469, 1475, 1480, 1484, 1489, 1496, 1501, 1505, 1510, 1516, 1521, 1526, 1533, 1538, 1548, 1555, 1576, 1580, 1581, 1586, 1591, 1596, 1599, 1602, 1608, 1614, 1618, 1632, 1637, 1641, 1656, 1663, 1668, 1678, 1681, 1687, 1693, 1698, 1710, 1716, 1728, 1744, 1749, 1755, 1759, 1765, 1776, 1785, 1791, 1796, 1800, 1802, 1808, 1812, 1819, 1824, 1831, 1839, 1841, 1844, 1851, 1856, 1863, 1867, 1876, 1881, 1888, 1913, 1935, 1939, 1950, 1959, 1965, 1972, 1977, 1979, 1989, 1995, 2019, 2021, 2033, 2036, 2038, 2044, 2051, 2055, 2058, 2066, 2071, 2082, 2091, 2106, 2108, 2113, 2124, 2130, 2133, 2142, 2144, 2157, 2165, 2167, 2172, 2182, 2198, 2215, 2218, 2237, 2240, 2243, 2245, 2255, 2259, 2263, 2269, 2275, 2280, 2283, 2287, 2296, 2303, 2308, 2315, 2321, 2323, 2334, 2348, 2351, 2354, 2358, 2362, 2371, 2372, 2378, 2380, 2385, 2390, 2404, 2413, 2422, 2429, 2435, 2439, 2444, 2449, 2453, 2459, 2465, 2471, 2476, 2481, 2490, 2504, 2510, 2515, 2521, 2532, 2539, 2540, 2544, 2578, 2580, 2584, 2591, 2621, 2628, 2634, 2647, 2653, 2657, 2663, 2669, 2674, 2681, 2686, 2692, 2700, 2714, 2720, 2742, 2753, 2759, 2777, 2783, 2815, 2818, 2837, 2842, 2851, 2860, 2866, 2871, 2875, 2877, 2943, 2952, 2956, 3016, 3030, 3046, 3077, 3081, 3086, 3089, 3104, 3106, 3120, 3122, 3126, 3140, 3147, 3149, 3155, 3160, 3203, 3249, 3272, 3294, 3299, 3304, 3313, 3316, 3321, 3325, 3329, 3374]
In [27]: len(lista)
Out[27]: 396
In [28]: F.read()
Traceback (most recent call last):
File "<ipython-input-28-6eeb9eceb3e2>", line 1, in <module>
F.read()
ValueError: I/O operation on closed file.
In [29]: alice
Out[29]: <_io.TextIOWrapper name='files/alice.txt' mode='r' encoding='utf-8-sig'>
In [30]: alice.read(10)
Out[30]: ''
In [31]: alice.seek(10000)
Out[31]: 10000
In [32]: alice.read(10)
Out[32]: '\nwhat I sh'
In [33]: alice.seek(0)
Out[33]: 0
In [34]: alice.read(100)
Out[34]: "Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll\n\nThis eBook is for the use of"
In [35]: alice.read(100)
Out[35]: ' anyone anywhere at no cost and with\nalmost no restrictions whatsoever. You may copy it, give it aw'
In [36]: len(testo)
Out[36]: 163779
In [37]: 'Turtle' in testo
Out[37]: True
In [38]: testo.count('Turtle')
Out[38]: 59
In [39]: presenti = set(testo)
In [40]: presenti
Out[40]: {')', 'f', 'P', ' ', 'e', '0', '7', 'O', 'S', '3', 'R', 'G', 'Q', 'z', 'k', 'E', '.', '4', 'X', 'M', "'", 't', '_', 'H', 'v', 'h', 'g', ':', '[', ';', 'i', '*', '/', '5', 'x', 'r', 'K', '%', 'A', 'u', 'F', 'C', 'c', ']', 'B', '2', '$', 's', 'Z', '#', 'n', 'T', '8', '"', 'o', 'y', '!', 'p', 'L', 'q', 'W', 'm', '6', '?', 'j', 'J', 'N', '-', '@', ',', 'b', 'a', 'I', 'd', '1', 'Y', 'U', 'D', '\n', 'V', '9', '(', 'l', 'w'}
In [41]: for c in presenti.copy():
...: if c.isalpha():
...: presenti.remove(c)
...:
In [42]: presenti
Out[42]: {')', ' ', '0', '7', '3', '.', '4', "'", '_', ':', '[', ';', '*', '/', '5', '%', ']', '2', '$', '#', '8', '"', '!', '6', '?', '-', '@', ',', '1', '\n', '9', '('}
In [43]: presenti.remove('\n')
In [44]: presenti
Out[44]: {')', ' ', '0', '7', '3', '.', '4', "'", '_', ':', '[', ';', '*', '/', '5', '%', ']', '2', '$', '#', '8', '"', '!', '6', '?', '-', '@', ',', '1', '9', '('}
In [45]: for c in presenti:
...: testo.replace(c, ' ')
...:
In [46]: testo[:100]
Out[46]: "Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll\n\nThis eBook is for the use of"
In [47]: for c in presenti:
...: testo = testo.replace(c, ' ')
...:
In [48]: testo[:100]
Out[48]: 'Project Gutenberg s Alice s Adventures in Wonderland by Lewis Carroll\n\nThis eBook is for the use of'
In [49]: parole = testo.split()
In [50]: parole[:20]
Out[50]: ['Project', 'Gutenberg', 's', 'Alice', 's', 'Adventures', 'in', 'Wonderland', 'by', 'Lewis', 'Carroll', 'This', 'eBook', 'is', 'for', 'the', 'use', 'of', 'anyone', 'anywhere']
In [51]: parole.count('any')
Out[51]: 72
In [52]: testo.count('any')
Out[52]: 115
In [53]: import os
In [54]: os.listdir('files')
Out[54]: ['testo.txt', 'holmes.txt', 'alice.txt', 'testo2.txt', 'words.py', 'results.txt', 'logo.png', 'log.log', 'frankenstein.txt', 'alice_it.txt', 'prince.txt']
In [55]: os.listdir('..')
Out[55]: ['lezione01-6-10-20', 'lezione04-16-10-20', 'lezione03-13-10-20', 'lezione05-20-10-20', 'lezione02-9-10-20', 'lezione06-23-10-20', 'lezione08-30-10-20', 'AA1920-HW4obb', 'lezione07-27-10-20', 'lezione09-3-11-20']
In [56]: os.listdir('files')
Out[56]: ['testo.txt', 'holmes.txt', 'alice.txt', 'testo2.txt', 'words.py', 'results.txt', 'logo.png', 'log.log', 'frankenstein.txt', 'alice_it.txt', 'prince.txt']
In [57]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20/lezione09-3-11-20.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20')
Traceback (most recent call last):
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20/lezione09-3-11-20.py", line 87, in <module>
print(conta_frequenza_parole(nomi_file, parole_da_cercare))
NameError: name 'parole_da_cercare' is not defined
In [58]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20/lezione09-3-11-20.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20')
Traceback (most recent call last):
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20/lezione09-3-11-20.py", line 87, in <module>
print(conta_frequenza_parole(nomi_file, parole_da_cercare))
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20/lezione09-3-11-20.py", line 50, in conta_frequenza_parole
testo = leggi_file()
TypeError: leggi_file() missing 1 required positional argument: 'filename'
In [59]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20/lezione09-3-11-20.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione09-3-11-20')
{'files/holmes.txt': {'monster': 0.0, 'blood': 0.0001376247798003523, 'alice': 0.00011009982384028186, 'turtle': 0.0, 'cappellaio': 0.0}, 'files/alice.txt': {'monster': 0.0, 'blood': 0.0, 'alice': 0.01324829876064302, 'turtle': 0.0019395772379105164, 'cappellaio': 0.0}, 'files/frankenstein.txt': {'monster': 0.0003957665743211327, 'blood': 0.00024256661006779098, 'alice': 0.0, 'turtle': 0.0, 'cappellaio': 0.0}, 'files/alice_it.txt': {'monster': 0.0, 'blood': 0.0, 'alice': 0.017395842393667912, 'turtle': 0.0, 'cappellaio': 0.0024789075410976778}, 'files/prince.txt': {'monster': 0.0, 'blood': 7.601816834223379e-05, 'alice': 0.0, 'turtle': 0.0, 'cappellaio': 0.0}}
In [60]: R = _
In [61]: import copy
In [62]: A = [ [1, 2, 3], [4, 5, 6]]
In [63]: B = A.copy()
In [64]: B
Out[64]: [[1, 2, 3], [4, 5, 6]]
In [65]: A
Out[65]: [[1, 2, 3], [4, 5, 6]]
In [66]: A is B
Out[66]: False
In [67]: A[0] is B[0]
Out[67]: True
In [68]: B[0][2] = 42
In [69]: A
Out[69]: [[1, 2, 42], [4, 5, 6]]
In [70]: B
Out[70]: [[1, 2, 42], [4, 5, 6]]
In [71]: