Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "util/task_helper.h"
  5. #include <stdlib.h>
  6. #define CMD_MAX_LNGTH 100
  7. #define DELIMITERS " .,;?!:"
  8. void replace(char *poezie,char * new,char *old)
  9. {
  10. char *copie=(char *)malloc(4000*sizeof(char));
  11. strcpy(copie,poezie);
  12. char *poz=strstr(poezie,old);
  13. int flag=poz-poezie;
  14. poezie[flag]='\0';
  15. strcat(poezie,new);
  16. strcat(poezie,copie+flag+strlen(old));
  17. free(copie);
  18. }
  19. char to_low(char s)
  20. {
  21. if(s>='A'&&s<='Z')
  22. {
  23. s+=('a'-'A');
  24. return s;
  25. }else
  26. {
  27. return s;
  28. }
  29. }
  30. void rhyme_alg(char *cuv1, char *cuv2, char *dest)
  31. {
  32. char **lista;
  33. char *locatie=(char*)malloc(100*sizeof(char *)) ;
  34. int lngth1, lngth2,n,gasit=0,i,lngth_crt;
  35. lngth1=strlen(cuv1);
  36. lngth2=strlen(cuv2);
  37. //printf("%d %d ",lngth1,lngth2);
  38. //printf("%c %c ", cuv1[lngth1-1],cuv2[lngth2-1]);
  39.  
  40. if(cuv1[lngth1-1]!=cuv2[lngth2-1])
  41. {
  42. get_synonym(cuv1,&n,&lista);
  43. //printf("%d", n);
  44. if(n==-1)
  45. {
  46. gasit=0;
  47. }else
  48. {
  49. for(i=0;i<n&&gasit==0;i++)
  50. {
  51. lngth_crt=strlen(lista[i]);
  52. //printf("%d\n", lngth_crt);
  53. //printf("%d ", gasit );
  54. //printf("%c ", lista[i][lngth_crt-1]);
  55.  
  56. if((lista[i][lngth_crt-1]==cuv2[lngth2-1])||(lista[i][lngth_crt-1]+32==cuv2[lngth2-1]))
  57. {
  58. gasit=i;
  59. }
  60. }
  61. if(gasit!=0)
  62. {
  63. strcpy(locatie,lista[gasit]);
  64. }
  65. //printf("%d\n", gasit);
  66.  
  67.  
  68. }
  69. if(gasit!=0)
  70. {printf("*");
  71. replace(dest,locatie,cuv1);
  72. }
  73. if(gasit==0)
  74. {
  75. get_synonym(cuv2,&n,&lista);
  76. if(n==-1)
  77. {
  78. gasit=0;
  79. }else
  80. {
  81. for(i=0;i<n&&gasit==0;i++)
  82. {
  83. lngth_crt=strlen(lista[i]);
  84. if((lista[i][lngth_crt-1]==cuv1[lngth2-1])||(lista[i][lngth_crt-1]+32==cuv1[lngth2-1]))
  85. {
  86. gasit=i;
  87. }
  88. }
  89. if(gasit!=0)
  90. {
  91. strcpy(locatie,lista[gasit]);
  92. }
  93.  
  94.  
  95. if(gasit!=0)
  96. {printf("*");printf("%s\n",locatie);
  97. printf("%s",cuv2);
  98. replace(dest,locatie,cuv2);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. int is_letter(char s)
  105. {
  106. if((s>='a'&&s<='z')||(s>='A'&&s<='Z'))
  107. {
  108. return 1;
  109. }
  110. return 0;
  111. }
  112. int is_valid(char s)
  113. {
  114. if(is_letter(s)==1)
  115. {
  116. return 1;
  117. }
  118. else
  119. {
  120. if(strstr(DELIMITERS,&s)==NULL)
  121. {
  122. return 1;
  123. }
  124. }
  125. return 0;
  126. }
  127. void upper_vers(char *vers)
  128. {
  129. int lngth_vers=strlen(vers),i;
  130. if(vers[0]>='a'&& vers[0]<='z')
  131. {
  132. vers[0]-=('a'-'A');
  133. }
  134. for(i=1;i<lngth_vers;i++)
  135. {
  136. if(vers[i]>='A'&& vers[i]<='Z')
  137. {
  138. vers[i]+=('a'-'A');
  139. }
  140. }
  141. }
  142. void uppercase(char *poezie)
  143. {
  144. char *vers,*copie=poezie;
  145. int lngth_poezie=strlen(poezie), i;
  146. vers=strtok(copie,"\n");
  147. upper_vers(vers);
  148. while(vers!=NULL)
  149. {
  150. vers=strtok(NULL,"\n");
  151. if(vers==NULL)
  152. {
  153. break;
  154. }
  155. upper_vers(vers);
  156. }
  157. for(i=0;i<lngth_poezie;i++)
  158. {
  159. if(poezie[i]=='\0')
  160. {
  161. poezie[i]='\n';
  162. }
  163. }
  164. }
  165. void trimming(char *poezie)
  166. {
  167. int lngth_poezie=strlen(poezie),i,poz=0;
  168. char *copie=(char *)malloc(lngth_poezie*sizeof(char));
  169. for(i=0;i<lngth_poezie;i++)
  170. {
  171. if(is_valid(poezie[i])==1)
  172. {
  173. copie[poz]=poezie[i];
  174. poz++;
  175. }
  176. else
  177. {
  178. if(poezie[i]==' '&& poezie[i-1]!=' ')
  179. {
  180. copie[poz]=poezie[i];
  181. poz++;
  182. }
  183. }
  184. }
  185. copie[poz]='\0';
  186. strcpy(poezie,copie);
  187. free(copie);
  188. }
  189. void silly(char *poezie,float prob)
  190. {
  191. float sample;
  192. int lngth_poezie=strlen(poezie),i;
  193. for(i=0;i<lngth_poezie;i++)
  194. {
  195. if(is_letter(poezie[i])==1)
  196. {
  197. sample = rand() % 100 / 99.0;
  198. if(sample<prob)
  199. {
  200. if(poezie[i]>='a'&&poezie[i]<='z')
  201. {
  202. poezie[i]-=('a'-'A');
  203. }
  204. else
  205. {
  206. if(poezie[i]>='A'&&poezie[i]<='Z')
  207. {
  208. poezie[i]+=('a'-'A');
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. void rhimy(char *poezie, char *rima)
  216. {
  217. char *copie, *line, **cuv;
  218. int lngth_poezie=strlen(poezie);
  219. int lngth_line,i, j;
  220. int nr_cuv=0, l_cuv;
  221. cuv=(char **)malloc(80*sizeof(char *));
  222. for(i=0;i<80;i++)
  223. {
  224. cuv[i]=(char *)malloc(100*sizeof(char));
  225. }
  226. copie=(char *)malloc(lngth_poezie*sizeof(char));
  227. strcpy(copie,poezie);
  228. line=strtok(copie,"\n");
  229. lngth_line=strlen(line);
  230. for(i=lngth_line-1;i>=0;i--)
  231. {
  232. if(*(line+i)==' ')
  233. {
  234. strcpy(cuv[nr_cuv],line+i+1);
  235. break;
  236. }
  237. }
  238. nr_cuv++;
  239. while(line!=NULL)
  240. {
  241. line=strtok(NULL,"\n");
  242. if(line==NULL)
  243. {
  244. break;
  245. }
  246. lngth_line=strlen(line);
  247. for(i=lngth_line-1;i>=0;i--)
  248. {
  249. if(*(line+i)==' ')
  250. {
  251. strcpy(cuv[nr_cuv],line+i+1);
  252. break;
  253. }
  254. }
  255. nr_cuv++;
  256. }
  257. free(copie);
  258. for(i=0;i<nr_cuv;i++)
  259. {
  260. for(j=0;cuv[i][j]!='\0';j++)
  261. {
  262. if(is_letter(cuv[i][j])!=1)
  263. {
  264. cuv[i][j]='\0';
  265. }
  266. printf("%c", cuv[i][j]);
  267. }
  268. printf("\n");
  269. }
  270. if(strcmp(rima,"imperecheata")==0)
  271. {
  272. for(i=0;i<nr_cuv;i+=4)
  273. {
  274. rhyme_alg(cuv[i],cuv[i+1],poezie);
  275. rhyme_alg(cuv[i+2],cuv[i+3],poezie);
  276. }
  277. }
  278. }
  279.  
  280. void friendly(char *poezie)
  281. {
  282.  
  283. char *cuv, *poz,*copie;
  284. char **dim;
  285. int lngth_poezie=strlen(poezie);
  286. int lngth_dim, lngth_cuv;
  287. copie=(char *)malloc(lngth_poezie*sizeof(char));
  288. int lngth_diff=0;
  289. strcpy(copie,poezie);
  290. cuv=strtok(copie," \n.,:;?!");
  291. get_friendly_word(cuv,dim);
  292. if(*dim!=NULL)
  293. {
  294. lngth_cuv=strlen(cuv);
  295. lngth_dim=strlen(*dim);
  296. lngth_diff+=(lngth_dim- lngth_cuv);
  297. replace(poezie,*dim,cuv);
  298. }
  299. while(cuv!=NULL)
  300. {
  301. if(cuv==NULL)
  302. {
  303. break;
  304. }
  305. cuv=strtok(NULL," \n.,:;?!");
  306. get_friendly_word(cuv,dim);
  307. if(*dim!=NULL)
  308. {
  309. replace(poezie+(cuv-copie+lngth_diff),*dim,cuv);
  310. lngth_cuv=strlen(cuv);
  311. lngth_dim=strlen(*dim);
  312. lngth_diff+=(lngth_dim- lngth_cuv);
  313. }
  314. }
  315.  
  316. }
  317. void print(char *poezie)
  318. {
  319. printf("%s\n\n",poezie);
  320. }
  321.  
  322. int main(void)
  323. {
  324. srand(42);
  325. char cmd[CMD_MAX_LNGTH];
  326. int lngth,lngth_buff;
  327. char *buffer,*copie;
  328. buffer=(char *)malloc(3000*sizeof(char));
  329. while(1)
  330. {
  331. fgets(cmd,CMD_MAX_LNGTH,stdin);
  332. lngth=strlen(cmd);
  333. cmd[lngth-1]='\0';
  334. if(cmd[0]=='l')
  335. {
  336. load_file(cmd+5,buffer);
  337. }
  338. if(cmd[0]=='q')
  339. {
  340. break;
  341. }
  342. if(cmd[0]=='p')
  343. {
  344. print(buffer);
  345. }
  346. if(cmd[0]=='u')
  347. {
  348. uppercase(buffer);
  349. }
  350. if(cmd[0]=='t')
  351. {
  352. trimming(buffer);
  353. }
  354. if(strstr(cmd,"make_it_silly")!=NULL)
  355. {
  356. float prob=atof(cmd+13);
  357. silly(buffer,prob);
  358. }
  359. if(strstr(cmd,"make_it_friendlier")!=NULL)
  360. {
  361. friendly(buffer);
  362. }
  363. if(strstr(cmd,"make_it_rhyme")!=NULL)
  364. {
  365. rhimy(buffer,cmd+14);
  366.  
  367.  
  368. }
  369. }
  370. return 0;
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement