public function doUnKnowUpdate(PhrasesEntity $word) { // invalid setting if ($word->getStatus() != $this->marker->getStatus()) { throw new EngRuntimeException("status doesn't match!"); } $newFailureCount = $word->getFailure() + 1; if ($newFailureCount >= $this->marker->getFailure()) { $newStatus = $this->getNextStatusWhenFail($word->getStatus()); if ($newStatus != $word->getStatus()) { $word->setStatus($newStatus); $word->setSuccess(0); $word->setFailure(0); } else { $word->setFailure($newFailureCount); } } else { $word->setFailure($newFailureCount); } }
public function addNewAction() { $json = array('status' => 0, 'errorPhrases' => ''); $key = trim($this->request->request->get('key')); $phrases = preg_split("/[\r\n]+/", $key); if (empty($phrases)) { return new JsonResponse($json); } $errorPhrases = array(); $successPhrases = array(); $updatePhrases = array(); foreach ($phrases as $phrase_line) { if (empty($phrase_line)) { continue; } $all = array(); preg_match_all('|^([a-z\\.\\t,\'\\\\?!\\-’ /]+)(.*)$|i', $phrase_line, $all); if (!isset($all[1][0])) { continue; } $phrase = trim(str_replace('’', '\'', $all[1][0])); $phrase = str_replace(',', ',', $all[1][0]); $mean = isset($all[2][0]) ? $all[2][0] : ''; $entity = $this->em->getRepository('Eng:PhrasesEntity')->findOneBy(array('name' => $phrase)); if ($entity === null) { $entity = new PhrasesEntity(); $entity->setName($phrase); $entity->setCreateTime(new \Datetime('now')); } try { $entity->setMeans($mean); $voiceName = $this->container['phraseVoiceDownloader']->download($entity->getName()); $entity->setVoice($voiceName); $entity->setStatus(PhrasesEntity::NEWONE); } catch (\Exception $e) { $this->log->addWarning(sprintf("Can not find this word's voice:%s, info:%s", $entity->getName(), $e->getMessage())); $errorPhrases[] = $entity->getName(); continue; } if ($entity->getId() !== null) { $updatePhrases[] = $entity->getName(); } else { $successPhrases[] = $entity->getName(); } $this->em->persist($entity); $this->em->flush(); } $message = array(); if (!empty($successPhrases)) { $message[] = 'new phrases: ' . count($successPhrases); } if (!empty($updatePhrases)) { $message[] = 'update phrases: ' . count($updatePhrases); } if (!empty($errorPhrases)) { if (empty($successPhrases) && empty($updatePhrases)) { $json['status'] = 1; } $message[] = 'error phrases: ' . count($errorPhrases); $json['errorPhrases'] = implode("\n", $errorPhrases); } $json['message'] = implode(', ', $message); return new JsonResponse($json); }
private function downloadPhrasesVoice($name, $force) { $params = array(); if ($force) { $sql = "SELECT * FROM phrases WHERE 1 = 1"; } else { $sql = "SELECT * FROM phrases WHERE (voice IS NULL or voice = '')"; } if ($name != null) { $sql .= ' AND name = :name'; $params['name'] = $name; } $sql .= " AND voice not like '%/%'"; $handle = $this->db->executeQuery($sql, $params); while ($row = $handle->fetch()) { $entity = new PhrasesEntity(); $entity->fromArray($row); try { $this->updatePhrase($entity, $force); } catch (\Exception $e) { $this->output->writeln(sprintf("phrase [%s] update failed, error info: %s", $entity->getName(), $e->getMessage())); } } }