-
Notifications
You must be signed in to change notification settings - Fork 1
/
turkish_word _derivator.py
215 lines (179 loc) · 9.31 KB
/
turkish_word _derivator.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-
import jpype as jp
import pandas as pd
from pandas import DataFrame
from bs4 import BeautifulSoup
import requests
import openpyxl as op
import xlrd
## Zemberek: Word Generation Example
# Documentation: https://github.com/ahmetaa/zemberek-nlp/tree/master/morphology#word-generation
# Java Code Example: https://github.com/ahmetaa/zemberek-nlp/blob/master/examples/src/main/java/zemberek/examples/morphology/GenerateWords.java
# Relative path to Zemberek .jar
ZEMBEREK_PATH = '/home/busra/System_Programming_HWS/src/0.17.1-20190726T121643Z-001/0.17.1/zemberek-full.jar'
# Start the JVM
jp.startJVM(jp.getDefaultJVMPath(), '-ea', '-Djava.class.path=%s' % (ZEMBEREK_PATH))
# Import the required Java classes
TurkishMorphology = jp.JClass('zemberek.morphology.TurkishMorphology')
TurkishSpellChecker = jp.JClass('zemberek.normalization.TurkishSpellChecker')
TurkishSentenceNormalizer = jp.JClass('zemberek.normalization.TurkishSentenceNormalizer')
Paths = jp.JClass('java.nio.file.Paths')
morphology = TurkishMorphology.createWithDefaults()
# Instantiate the spell checker class using the morphology instance
spell = TurkishSpellChecker(morphology)
df = pd.read_excel ('busra_sentiwordnet_rule_derivation_with_pos_tags.xlsx')
liste = []
text_list = []
word_list= []
for word in df['stemmed_version']:
word = str(word)
if 'ç' in word or 'รง' in word or 'Ç' in word:
word = word.replace('ç', 'ç')
word = word.replace('รง', 'ç')
word = word.replace('Ç', 'ç')
if 'ı' in word:
word = word.replace('ı', 'ı')
if 'ÄŸ' in word:
word = word.replace('ÄŸ', 'ğ')
if 'ö' in word or 'รถ':
word = word.replace('ö', 'ö')
word = word.replace('รถ', 'ö')
if 'ÅŸ' in word:
word = word.replace('ÅŸ', 'ş')
if 'ü' in word:
word = word.replace('ü', 'ü')
text_list.append(word)
analysis = morphology.analyzeSentence(word)
sonuclar = morphology.disambiguate(word, analysis).bestAnalysis()
liste.append(str(sonuclar))
k = 0
for list_word in liste:
splitted = list_word.split("]")
text = text_list[k]
if text.find(' ') != -1:#if text contains words more than 1
kelime = text.split(" ")
ilk_kelime = kelime[0]
#if "Verb" in splitted[0]: # suffixation if text contains two words with first word is verb
# if "Adj" in splitted[1] or "Noun" in splitted[1]: # add suffix for first word
#ikinci_kelime = text[text.find(' '):]
#for i in ilk_kelime:
# if i in 'eiöü':
# ilk_kelime = ilk_kelime + "mek"
# break
# elif i in 'aıou':
# ilk_kelime = ilk_kelime + "mak"
# break
# ilk_kelime = ilk_kelime.replace(" ", "+")
#source = requests.get('https://cooljugator.com/tr/' + ilk_kelime)
#soup = BeautifulSoup(source.content, "lxml")
# ana = soup.find('body')
# alt = ana.findAll('div', attrs={"class": "conjugation-cell conjugation-cell-our"})
#new = ana.findAll('div', attrs={"class": "meta-form"})
#for i in new:
# text = i.text + ikinci_kelime
# word_list.append(text)
# elif "Verb" in splitted[1]: # add suffix for second word
# ikinci_kelime = text[text.find(' '):]
# for i in ilk_kelime:
# if i in 'eiöü':
# ilk_kelime = ilk_kelime + "mek"
# break
# elif i in 'aıou':
# ilk_kelime = ilk_kelime + "mak"
# break
# for i in ikinci_kelime:
# if i in 'eiöü':
# ikinci_kelime = ikinci_kelime + "mek"
# break
# elif i in 'aıou':
# ikinci_kelime = ikinci_kelime + "mak"
# break
# ilk_kelime = ilk_kelime.replace(" ", "+")
# source = requests.get('https://cooljugator.com/tr/' + ilk_kelime)
# soup = BeautifulSoup(source.content, "lxml")
# ana = soup.find('body')
#alt = ana.findAll('div', attrs={"class": "conjugation-cell conjugation-cell-our"})
#new = ana.findAll('div', attrs={"class": "meta-form"})
#for i in new:
# text = i.text + ikinci_kelime
# word_list.append(text)
#ikinci_kelime = ikinci_kelime.replace(" ", "+")
#source = requests.get('https://cooljugator.com/tr/' + ikinci_kelime)
#soup = BeautifulSoup(source.content, "lxml")
# ana = soup.find('body')
# alt = ana.findAll('div', attrs={"class": "conjugation-cell conjugation-cell-our"})
# new = ana.findAll('div', attrs={"class": "meta-form"})
# for i in new:
# text = ilk_kelime + i.text
# word_list.append(text)
if "Adj" in splitted[0] or "Noun" in splitted[0]: # suffixation if text is verb and text contains two words
if "Verb" in splitted[1]: # add suffix for first word
ikinci_kelime = text[text.find(' '):]
for i in ikinci_kelime:
if i in 'eiöü':
ikinci_kelime = ikinci_kelime + "mek"
break
elif i in 'aıou':
ikinci_kelime = ikinci_kelime + "mak"
break
ikinci_kelime = ikinci_kelime.replace(" ", "+")
source = requests.get('https://cooljugator.com/tr/' + ikinci_kelime)
soup = BeautifulSoup(source.content, "lxml")
ana = soup.find('body')
alt = ana.findAll('div', attrs={"class": "conjugation-cell conjugation-cell-our"})
new = ana.findAll('div', attrs={"class": "meta-form"})
for i in new:
text = ilk_kelime + i.text
word_list.append(text)
else:#if text contains just 1 word
if "Noun" in splitted[0]:#if word is noun
# Disabling the cache and building using the word as the lexicon itself
morphology = TurkishMorphology.builder().setLexicon(text).disableCache().build()
# Getting the dictionary item
dictionary_item = morphology.getLexicon().getMatchingItems(text).get(0)
# Possessive and case suffix combinations will
# be used for generating inflections of the word
number = ['A3sg', 'A3pl']
possessives = ['P1sg', 'P2sg', 'P3sg']
cases = ['Dat', 'Loc', 'Abl', 'Gen', 'Acc', 'Inst', 'Nom']
suffixes = {"With", "Past", "A3sg"}
# tenses = ['Fut', 'Past']
# Iterating the Result class instance to to access
# the generated word and the analysis
for numberM in number:
for possessiveM in possessives:
for caseM in cases:
results = morphology.getWordGenerator().generate(dictionary_item, numberM, possessiveM,
caseM)
for result in results:
#print('Surface Form: %s' % result.surface)
#print('Analysis: %s\n' % result.analysis)
text = str(result).split("-")
if spell.check(text[0]):#if noun is correct
word_list.append(text[0])
#print(text[0])
if "Verb" in splitted[0]: # if word is verb
for i in text:
if i in 'eiöü':
text = text + "mek"
break
elif i in 'aıou':
text = text + "mak"
break
text = text.replace(" ", "+")
source = requests.get('https://cooljugator.com/tr/' + text)
soup = BeautifulSoup(source.content, "lxml")
ana = soup.find('body')
alt = ana.findAll('div', attrs={"class": "conjugation-cell conjugation-cell-our"})
new = ana.findAll('div', attrs={"class": "meta-form"})
for i in new:
word_list.append(i.text)
#print(text)
k = k+1
# Do basic spell checking and print the results
#for word in word_list:
#print('%s -> Correct' % (word) if spell.check(word) else '%s -> Wrong' % (word))
dafram = DataFrame({'Word List': word_list})
dafram.to_excel('New_Word_Generator_2.xlsx', index=True)
# Shutting down the JVM
jp.shutdownJVM()