-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
149 lines (135 loc) · 4.94 KB
/
main.py
File metadata and controls
149 lines (135 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#imports
from multiprocessing.pool import ThreadPool
import difflib
import threading
import time
english = set([line.rstrip('\n').lower() for line in open('english.txt')])
spanish = set([line.rstrip('\n').lower() for line in open('spanish.txt')])
german = set([line.rstrip('\n').lower() for line in open('german.txt')])
french = set([line.rstrip('\n').lower() for line in open('french.txt')])
symbols = "!$%&/()=?|@#'\\\",[]{-_;.:}1234567890<>"
class englishThread (threading.Thread):
def __init__(self, sentence):
threading.Thread.__init__(self)
self.sentence= sentence
self.words_in_language = 0.0
self.words = sentence.split()
self.word_count = len(self.words)
def run(self):
words = self.sentence.split()
for word in words:
for i in range(0, len(symbols)):
word = word.replace(symbols[i], "")
if word in english:
self.words_in_language += 1.0
probability = (self.words_in_language / self.word_count) * 100
print "It is " + str(probability) + "% English\n"
print("--- %s seconds ---" % (time.time() - start_time))
class spanishThread (threading.Thread):
def __init__(self, sentence):
threading.Thread.__init__(self)
self.sentence= sentence
self.words_in_language = 0.0
self.words = sentence.split()
self.word_count = len(self.words)
def run(self):
for word in words:
for i in range(0, len(symbols)):
word = word.replace(symbols[i], "")
if word in spanish:
self.words_in_language += 1.0
probability = (self.words_in_language / self.word_count) * 100
print "It is " + str(probability) + "% Spanish\n"
print("--- %s seconds ---" % (time.time() - start_time))
class germanThread (threading.Thread):
def __init__(self, sentence):
threading.Thread.__init__(self)
self.sentence= sentence
self.words_in_language = 0.0
self.words = sentence.split()
self.word_count = len(self.words)
def run(self):
words = self.sentence.split()
for word in words:
for i in range(0, len(symbols)):
word = word.replace(symbols[i], "")
if word in german:
self.words_in_language += 1.0
probability = (self.words_in_language / self.word_count) * 100
print "It is " + str(probability) + "% German\n"
print("--- %s seconds ---" % (time.time() - start_time))
class frenchThread (threading.Thread):
def __init__(self, sentence):
threading.Thread.__init__(self)
self.sentence= sentence
self.words_in_language = 0.0
self.words = sentence.split()
self.word_count = len(self.words)
def run(self):
words = self.sentence.split()
for word in words:
for i in range(0, len(symbols)):
word = word.replace(symbols[i], "")
if word in french:
self.words_in_language += 1.0
probability = (self.words_in_language / self.word_count) * 100
print "It is " + str(probability) + "% French\n"
print("--- %s ms ---" % ((time.time() - start_time) * 1000))
def is_english(sentence):
sentence= sentence
words_in_language = 0.0
words = sentence.split()
word_count = len(words)
for word in words:
if word in english:
words_in_language += 1.0
probability = (words_in_language / word_count)
return probability
def is_spanish(sentence):
sentence= sentence
words_in_language = 0.0
words = sentence.split()
word_count = len(words)
for word in words:
if word in spanish:
words_in_language += 1.0
probability = (words_in_language / word_count)
return probability
def is_german(sentence):
words_in_language = 0.0
words = sentence.split()
word_count = len(words)
for word in words:
if word in german:
words_in_language += 1.0
probability = (words_in_language / word_count)
return probability
def is_french(sentence):
sentence= sentence
words_in_language = 0.0
words = sentence.split()
word_count = len(words)
for word in words:
if word in french:
words_in_language += 1.0
probability = (words_in_language / word_count)
return probability
def languages(sentence):
for i in range(0, len(symbols)):
sentence = sentence.replace(symbols[i], "")
return (("english", is_english(sentence)), ("spanish", is_spanish(sentence)), ("german", is_german(sentence)), ("french", is_french(sentence)))
# Get sentence
sentence = raw_input('Write your text\n')
start_time = time.time()
print languages(sentence.lower())
print("--- %s ms ---" % ((time.time() - start_time) * 1000))
# Create new threads
# thread1 = englishThread(sentence.lower())
# thread2 = spanishThread(sentence.lower())
# thread3 = germanThread(sentence.lower())
# thread4 = frenchThread(sentence.lower())
# # Start new Threads
# thread1.start()
# thread2.start()
# thread3.start()
# thread4.start()